20 lines
527 B
JavaScript
20 lines
527 B
JavaScript
module.exports = validateRequest;
|
|
|
|
function validateRequest(req, next, schema) {
|
|
console.log('validateRequest');
|
|
|
|
const options = {
|
|
abortEarly: false, // include all errors
|
|
allowUnknown: true, // ignore unknown props
|
|
stripUnknown: true, // remove unknown props
|
|
};
|
|
const { error, value } = schema.validate(req.body, options);
|
|
if (error) {
|
|
console.log('error: ', error);
|
|
next(`Validation error: ${error.details.map((x) => x.message).join(', ')}`);
|
|
} else {
|
|
req.body = value;
|
|
next();
|
|
}
|
|
}
|