koa-joi-validate-middleware
v1.1.7
Published
Validate middleware generator using Joi
Downloads
32
Readme
Koa Joi Validate Middleware
Validate middleware generator using Joi
Easily create Koa middleware for validate with Joi.
Install
$ npm i koa-joi-validate-middleware
Usage
Import
const validateMiddleware = require('koa-joi-validate-middleware');
Create 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);
errorCallback
function errorCallback(ctx, error) {
}
Use Validate Middleware
router.get('/user', validator, async (ctx) => {
const { id } = ctx.params;
const response = await getUserInfo(id);
ctx.body = response;
});