fix: Correct validate middleware to use standard express-validator pattern
Some checks are pending
CI/CD / lint-and-build (push) Waiting to run
Some checks are pending
CI/CD / lint-and-build (push) Waiting to run
This commit is contained in:
parent
a0d2fe235e
commit
0fe27033b5
@ -1,23 +1,19 @@
|
|||||||
// Middleware для валидации запросов с express-validator
|
// Middleware для валидации запросов с express-validator
|
||||||
|
// Используется после применения валидаций как middleware
|
||||||
|
|
||||||
const { validationResult } = require('express-validator');
|
const { validationResult } = require('express-validator');
|
||||||
|
|
||||||
const validate = (validations) => {
|
const validate = (req, res, next) => {
|
||||||
return async (req, res, next) => {
|
const errors = validationResult(req);
|
||||||
// Выполняем все валидации
|
if (errors.isEmpty()) {
|
||||||
await Promise.all(validations.map(validation => validation.run(req)));
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
const errors = validationResult(req);
|
res.status(400).json({
|
||||||
if (errors.isEmpty()) {
|
success: false,
|
||||||
return next();
|
error: 'Validation failed',
|
||||||
}
|
errors: errors.array(),
|
||||||
|
});
|
||||||
res.status(400).json({
|
|
||||||
success: false,
|
|
||||||
error: 'Validation failed',
|
|
||||||
errors: errors.array(),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = validate;
|
module.exports = validate;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user