@sigfox/koa-joi-validate
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

koa-joi-validate

npm version code style license

Joi validation middleware for Koa using Boom to format errors.

Features

  • Allows easy and declarative validation of request's body, headers, params and query
  • Uses Joi, a robust and popular validation library
  • Formats errors using Boom to format errors. together with @sigfox/koa-boom.

Install

npm install @sigfox/koa-joi-validate

Usage

koaJoiValidate(schemasOrFunc, joiValidateOptions)

  • schemasOrFunc (Object | Function) (mandatory)

    • Object: The Joi schemas that will be passed to Joi.validate
    • Function: A custom function taking ctx as a parameter. Use this is you want to build the schema from the context
  • joiValidateOptions (Object) (default: {}): Additonal options that will be spread as Joi.validate() options.

const Joi = require('joi');
const Koa = require('koa');
const koaJoiValidate = require('@sigfox/koa-joi-validate');

const app = new Koa()
  .use(
    koaJoiValidate({
      body: Joi.object().keys({
        firstname: Joi.string(),
        lastname: Joi.string()
      })
    })
  )
  .use(
    koaJoiValidate(ctx => ({
      query: ctx.state.joiSchema
    }))
  )
  .listen();

Test

npm test

Licence

This project is licensed under the MIT License - see the LICENSE file for details.

Package Sidebar

Install

npm i @sigfox/koa-joi-validate

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

9.17 kB

Total Files

6

Last publish

Collaborators

  • robnriks
  • cgrimal
  • damien-s
  • lbdremy