Skip to content

Instantly share code, notes, and snippets.

@nikosantis
Created April 28, 2020 22:49
Show Gist options
  • Save nikosantis/8fa4c143c3a2d5c8820818937950b417 to your computer and use it in GitHub Desktop.
Save nikosantis/8fa4c143c3a2d5c8820818937950b417 to your computer and use it in GitHub Desktop.
const boom = require('@hapi/boom');
const joi = require('@hapi/joi');
function validate(data, schema) {
const { error } = joi.validate(data, schema);
return error;
}
function validationHandler(schema, check = 'body') {
return function(req, res, next) {
const error = validate(req[check], schema);
error ? next(boom.badRequest(error)) : next();
};
}
module.exports = validationHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment