Validate middleware generator using Joi
Easily create Koa middleware for validate with Joi.
$ npm i koa-joi-validate-middleware
const validateMiddleware = require('koa-joi-validate-middleware');
const schema = {
// Request headers Joi validation object
headers: Joi.obejct({
}),
// URL query string Joi validation object
query: Joi.obejct({
}),
// URL path parameters Joi validation object
params: Joi.obejct({
id: Joi.string().required(),
}).required(),
// POST body Joi validation object
body: Joi.obejct({
}),
};
const validator = validateMiddleware.create(schema, errorCallback);
function errorCallback(ctx, error) {
}
router.get('/user', validator, async (ctx) => {
const { id } = ctx.params;
const response = await getUserInfo(id);
ctx.body = response;
});