24 lines
540 B
JavaScript
24 lines
540 B
JavaScript
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 };
|