Koa middleware for validating and coercing request data.
yarn install yup koa-yup-validator
require("koa-yup-validator")(schema /*yup schema*/, options /* see below */);
Options
partial | Allows data to satisfy schema partially at root level (if the data key exists it must satisfy schema). Useful for example patch operations. |
path | Context path to validate |
yup | Options to be passed to yup validate. |
errorHandler | Provide custom error handler. |
Validate body
const yup = require("yup");
const validator = require("koa-yup-validator");
const schema = yup.object().shape({
name: yup.string().required(),
toppings: yup.array().of(yup.string())
});
router.post("/pizza", validator(schema), ctx => {
ctx.response.status = 200;
ctx.response.body = "Valid pizza!";
});
Validate anything within context
const yup = require("yup");
const validator = require("koa-yup-validator");
const schema = yup.object().shape({
Authorization: yup.string().required()
});
//Validates headers
router.post("/pizza", validator(schema, { path: "request.headers" }), ctx => {
ctx.response.status = 200;
ctx.response.body = "Valid pizza!";
});