@sigfox/koa-joi-validate
v1.1.0
Published
Joi validation middleware for Koa.
Downloads
9
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.