Skip to content

Instantly share code, notes, and snippets.

@Sarav-S
Last active July 19, 2020 06:15
Show Gist options
  • Save Sarav-S/896ef6fb4b7db7d42741519c1fcf1463 to your computer and use it in GitHub Desktop.
Save Sarav-S/896ef6fb4b7db7d42741519c1fcf1463 to your computer and use it in GitHub Desktop.
const _ = require('lodash');
const Validator = require('validatorjs');
module.exports = (request, response, next, rules) => {
const validation = new Validator(request.body, rules);
if (validation.fails()) {
const errors = {};
_.each(validation.errors.errors, (error, key) => {
errors[key] = error[0];
});
return response.status(400)
.send({
message: "Uh ooh! Please check the errors",
errors: errors
});
}
next();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment