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.
});If 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.
}))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.
Optional
If omitted, next-joi will use a default configuration.
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();
},
});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.
Required
Even if empty, this argument is required.
Optional
A valid joi schema.
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
unknownkeys. Otherwise, the validation will fail.
A valid joi schema.
Optional
A valid joi schema.
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);