Code Monkey home page Code Monkey logo

koa-yup-validator's Introduction

koa-yup-validator CircleCI Greenkeeper badge

Koa middleware for validating and coercing request data.

Usage

yarn add yup koa-yup-validator

import createValidator from "koa-yup-validator";
const validator = createValidator(validators, options); /* see below */

Arguments

validators

{
  body: schema; // validates request body
  headers: schema; // validates request headers
  params: schema; // validates path params
  query: schema; // validates query params
}

options:

Specify options for each validator ie.

{
  body: 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.
yup Options to be passed to yup validate.

Error handling

There is an optional error middleware you may use for handling errors thrown by the validation middleware:

import { createErrorMiddleware } from "koa-yup-validator";
app.use(createErrorMiddleware());
//...your routes

Examples

Validate headers and body

import * as yup from "yup";
import validator from "koa-yup-validator";

const Pizza = yup.object().shape({
  name: yup.string().required(),
  toppings: yup.array().of(yup.string()),
});
const RequiredHeaders = yup.object().shape({
  Authorization: yup.string().required(),
  "x-pizza-maker": yup.string(),
});

router.post(
  "/pizza",
  validator({ body: Pizza, headers: RequiredHeaders }),
  (ctx) => {
    console.log(ctx.request.headers["x-pizza-maker"]);
    ctx.response.status = 200;
    ctx.response.body = "Valid pizza!";
  }
);

// Pass yup options for body
router.post(
  "/strict-pizza",
  validator(
    { body: Pizza, headers: RequiredHeaders },
    { body: { yup: { strict: true } } }
  ),
  (ctx) => {
    console.log(ctx.request.headers["x-pizza-maker"]);
    ctx.response.status = 200;
    ctx.response.body = "Valid pizza!";
  }
);

koa-yup-validator's People

Contributors

greenkeeper[bot] avatar turee avatar dependabot[bot] avatar hixus avatar willianfalbo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.