schmty / koa-yup-validator

Koa middleware for validating and coercing request data.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

koa-yup-validator CircleCI Greenkeeper badge

Koa middleware for validating and coercing request data.

Usage

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!";
});

About

Koa middleware for validating and coercing request data.

License:MIT License


Languages

Language:TypeScript 93.8%Language:JavaScript 6.2%