2020-11-20 10:30:12 +00:00
|
|
|
const validateRequest = (schema) => (req, res, next) => {
|
2020-10-20 19:25:00 +00:00
|
|
|
console.log('validateRequest');
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
abortEarly: false, // include all errors
|
|
|
|
allowUnknown: true, // ignore unknown props
|
2020-11-20 10:02:30 +00:00
|
|
|
stripUnknown: true, // remove unknown props
|
2020-10-20 19:25:00 +00:00
|
|
|
};
|
|
|
|
const { error, value } = schema.validate(req.body, options);
|
|
|
|
if (error) {
|
2020-11-20 10:02:30 +00:00
|
|
|
console.log('error: ', error);
|
|
|
|
next(`Validation error: ${error.details.map((x) => x.message).join(', ')}`);
|
2020-10-20 19:25:00 +00:00
|
|
|
} else {
|
|
|
|
req.body = value;
|
|
|
|
next();
|
|
|
|
}
|
2020-11-20 10:30:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = validateRequest;
|