mirror of
https://github.com/sergeyyarkov/modbus-logger.git
synced 2026-10-11 10:28:51 +03:00
25 lines
565 B
JavaScript
25 lines
565 B
JavaScript
/**
|
|
* Validate request body
|
|
* @param {import('yup').Schema} schema
|
|
*/
|
|
export function validateBodyMiddleware(schema) {
|
|
/**
|
|
* @param {import('express').Request} req
|
|
* @param {import('express').Response} res
|
|
* @param {import('express').NextFunction} next
|
|
*/
|
|
return async (req, res, next) => {
|
|
try {
|
|
await schema.validate(req.body);
|
|
next();
|
|
} catch (error) {
|
|
return res.status(400).json({
|
|
error: {
|
|
status: 400,
|
|
message: error.errors[0],
|
|
},
|
|
});
|
|
}
|
|
};
|
|
}
|