Koa 2 middleware for validating Joi schemas
Problem:
Solution:
const Koa = require('koa');
const Joi = require('joi');
class koaJoiValidator = require('koa-joi-validator-middleware');
const schema = Joi.object({...});
const config = {...};
const app = new Koa();
app.use(koaJoiValidator(schema, config));
app.use(async ctx => {
// Here you can be sure that the specified validation passed
});
By default, only the schema parameter is needed. In this case the middleware will validate the ctx.request.body
object and throws an Error with a default message if it fails.
...
const schema = Joi.object({...});
const app = new Koa();
app.use(koaJoiValidator(schema));
...
You can specify an onError
key in the second, config
parameter. If this is a string it overwrites the default error message thrown.
...
const schema = Joi.object({...});
const config = {
onError: 'Hey You, send me a proper request!'
};
const app = new Koa();
app.use(koaJoiValidator(schema, config));
...
The onError
parameter can be a function as well which is called when the validation fails with the following parameters: ctx
, next
, error
. Here ctx
and next
are the standard Koa middleware parameters and error
is the ValidationError
returned by Joi.
...
const schema = Joi.object({...});
const config = {
onError: ctx => ctx.render('errorPage')
};
const app = new Koa();
app.use(koaJoiValidator(schema, config));
...
By default, the middleware validates the ctx.request.body
object but this can be overwritten with the getSubject
function. It takes ctx
as the input parameter and is expected to return the object that should be validated.
...
const schema = Joi.object({...});
const config = {
getSubject: ctx => ctx.session
};
const app = new Koa();
app.use(koaJoiValidator(schema, config));
...
Parameter | Optional | Type |
---|---|---|
schema | no | Joi schema |
config | yes | Object |
config.onError | yes | String or Function(ctx, next, error) |
config.getSubject | yes | Function(ctx) |
Note: This middleware does not contain Joi as an inner dependency, only as peer dependency. This means it can be updated individually when a new version comes out but it also means you need to install it separately.