Compare commits
No commits in common. "df50662d1835fe9bb86f54e36fc3d0b3d44f0877" and "d27c4a0946ad63cacc5f95a29704988fb7cc720c" have entirely different histories.
df50662d18
...
d27c4a0946
@ -1,9 +1,12 @@
|
|||||||
// Middleware для валидации запросов с express-validator
|
// Middleware для валидации запросов с express-validator
|
||||||
// Используется после применения валидаций как middleware
|
|
||||||
|
|
||||||
const { validationResult } = require('express-validator');
|
const { validationResult } = require('express-validator');
|
||||||
|
|
||||||
const validate = (req, res, next) => {
|
const validate = (validations) => {
|
||||||
|
return async (req, res, next) => {
|
||||||
|
// Выполняем все валидации
|
||||||
|
await Promise.all(validations.map(validation => validation.run(req)));
|
||||||
|
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
if (errors.isEmpty()) {
|
if (errors.isEmpty()) {
|
||||||
return next();
|
return next();
|
||||||
@ -14,6 +17,7 @@ const validate = (req, res, next) => {
|
|||||||
error: 'Validation failed',
|
error: 'Validation failed',
|
||||||
errors: errors.array(),
|
errors: errors.array(),
|
||||||
});
|
});
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = validate;
|
module.exports = validate;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user