Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Next Routes | 2,439 | 686 | 78 | a month ago | 46 | May 21, 2018 | mit | JavaScript | ||
Universal dynamic routes for Next.js | ||||||||||
Next Connect | 1,140 | 5 | 8 months ago | 33 | July 06, 2022 | 15 | mit | TypeScript | ||
The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2 | ||||||||||
Nextjs Sitemap Generator | 493 | 3 | 2 | 4 months ago | 27 | March 01, 2021 | 12 | mit | TypeScript | |
Generate sitemap.xml from nextjs pages | ||||||||||
Kimia Ui | 285 | 9 months ago | 1 | mit | TypeScript | |||||
A collection of UI Components for React built with Tailwind CSS 3 | ||||||||||
Generouted | 221 | 2 | 7 days ago | 33 | February 05, 2023 | 5 | mit | TypeScript | ||
Generated file-based routes for Vite | ||||||||||
Routes Gen | 207 | 3 | 2 months ago | 16 | January 08, 2023 | mit | TypeScript | |||
Framework agnostic routes typings generator. Remix ☑️ SolidStart ☑️ | ||||||||||
Nextjs Vercel Firebase | 180 | 4 months ago | TypeScript | |||||||
Next.js app using API Routes to connect with Firestore. | ||||||||||
Next Test Api Route Handler | 176 | 11 | 7 days ago | 68 | January 03, 2023 | 6 | mit | TypeScript | ||
:rocket::sparkles: Confidently unit test your Next.js API routes/handlers in an isolated Next.js-like environment | ||||||||||
Next Boilerplate | 148 | 5 years ago | 3 | JavaScript | ||||||
Minimal Next.js boilerplate | ||||||||||
Next Crud | 146 | 5 months ago | 10 | May 20, 2022 | 4 | mit | TypeScript | |||
Full-featured CRUD routes for Next.js |
Validate NEXT.js API Routes with joi
yarn add next-joi
This package does not bundle with next.js
or joi
, so you will need to install them separately.
The validation function will check the incoming request against the defined validation schemas. If the request does not comply with the schemas, it will be aborted immediately, and (by default) a 400 BAD REQUEST
response will be returned. It is possible to customize this error handling by passing a custom onValidationError
function to the primary factory function.
lib/middlewares/validation.ts
import withJoi from "next-joi";
export default withJoi({
onValidationError: (_, res) => {
res.status(400).end();
},
});
If you are using standard NEXT.js API Routes, you may use the validation function to wrap your route definition and pass along the validation schema:
import Joi from "joi";
import validate from "/lib/middlewares/validation";
const schema = Joi.object({
birthdate: Joi.date().iso(),
email: Joi.string().email().required(),
name: Joi.string().required(),
});
export default validate({ body: schema }, (req, res) => {
// This function will be only executed if the incoming request complies
// with the validation schema defined above.
});
connect
-like middlewaresIf your routes are powered by using a package such as next-connect
, you can still use next-joi
!
The middleware function is ready to work with connect
just out-of-the-box:
import Joi from "joi";
import connect from "next-connect";
import validate from "/lib/middlewares/validation";
const schema = Joi.object({
birthdate: Joi.date().iso(),
email: Joi.string().email().required(),
name: Joi.string().required(),
});
export default connect().post(validate({ body: schema }), (req, res) => {
// This function will be only executed if the incoming request complies
// with the validation schema defined above.
}))
withJoi(config?) => validate
This factory function may optionally receive a configuration object. It will return the actual validation function (validate
) that can be used as API route middleware.
config
Optional
If omitted, next-joi
will use a default configuration.
config.onValidationError
Required
Custom error function to handle validation errors. It will receive the API request, response, and validation error.
import withJoi from "next-joi";
export default withJoi({
onValidationError: (req, res, error) => {
res.status(400).end();
},
});
validate(schemas, handler)
The validate
function has support to check the following request fields: body
, headers
and query
. The first argument for this function should always be an object with the desired validation schemas.
schemas
Required
Even if empty, this argument is required.
schemas.body
Optional
A valid joi
schema.
schemas.headers
Optional
Note: since most of the time, you may receive more headers than expected, it is a good practice to make this schema always support
unknown
keys. Otherwise, the validation will fail.
A valid joi
schema.
schemas.query
Optional
A valid joi
schema.
handler
Optional
A valid next
API Route handler. If you are using the validate
function without a connect
-like middleware engine, this argument becomes mandatory.
Example:
const handler = function (req: NextApiRequest, res: NextApiResponse) {
// implementation
};
export default validate({}, handler);