JSPM

koa-joi-mw

0.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q28432F
  • License MIT

Joi validation middleware for Koa.

Package Exports

  • koa-joi-mw

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-joi-mw) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

koa-joi-mw

Koa middleware to validate requests against joi schemas.

API

const validate = require('koa-joi-mw');

validate(options)

A koa middleware which will validate and transform the request

param type required description
options object true
options.failCode number false The error code to throw in case of validation error, defaults to 400
options.options object false Options passed to Joi validator, such as allowUnknown
options.body Joi.object false A joi schema validated against the request body
options.params Joi.object false A joi schema validated against the request params
options.headers Joi.object false A joi schema validated against the request headers
options.query Joi.object false A joi schema validated against the request query

Usage

const joi = require('joi'),
  validate = require('koa-joi-mw');

router.post('/:number/:string/:date',
  validate({
    params: joi.object({
      number: joi.number().required(),
      string: joi.string().required(),
      date: joi.date().required()
    }),
    body: joi.object({
      number: joi.number().required(),
      string: joi.string().required(),
      date: joi.date().required()
    }),
    headers: joi.object({
      number: joi.number().required(),
      string: joi.string().required(),
      date: joi.date().required()
    }),
    query: joi.object({
      number: joi.number().required(),
      string: joi.string().required(),
      date: joi.date().required()
    }),
    options: { allowUnknown: true }
  }),
  function * () {
    this.assert(typeof this.params.number === 'number');
    this.assert(typeof this.params.string === 'string');
    this.assert(this.params.date instanceof Date);

    ['body', 'headers', 'query'].forEach(el => {
      this.assert(typeof this.request[el].number === 'number');
      this.assert(typeof this.request[el].string === 'string');
      this.assert(this.request[el].date instanceof Date);
    });

    this.status = 204;
  }
);