Package Exports
- @sigfox/koa-joi-validate
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 (@sigfox/koa-joi-validate) 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-validate
Joi validation middleware for Koa using Boom to format errors.
Features
- Allows easy and declarative validation of request's
body
,headers
,params
andquery
- 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.