Initial commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
const { ValidationError } = require('../shared/errors');
|
||||
|
||||
const validate = (schema) => {
|
||||
return (req, res, next) => {
|
||||
const { error, value } = schema.validate(req.body, {
|
||||
abortEarly: false,
|
||||
stripUnknown: true,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
const errors = error.details.map((detail) => ({
|
||||
field: detail.path.join('.'),
|
||||
message: detail.message,
|
||||
}));
|
||||
return next(new ValidationError('Validation failed', errors));
|
||||
}
|
||||
|
||||
req.body = value;
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = { validate };
|
||||
Reference in New Issue
Block a user