basic-expressjs/utils/middleware/validationHandler.js

17 lines
406 B
JavaScript
Raw Normal View History

2021-09-15 18:47:11 +00:00
const boom = require('@hapi/boom');
const joi = require('@hapi/joi');
function validate(data, schema) {
const { error } = joi.object(schema).validate(data);
return error;
}
function validationHandler(schema, check = "") {
return function(req, res, next) {
const error = validate(req[check], schema);
error ? next(boom.badRequest(error)) : next();
}
}
module.exports = validationHandler;