Last active
July 19, 2020 06:15
-
-
Save Sarav-S/896ef6fb4b7db7d42741519c1fcf1463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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