我试图在Express验证程序中有条件地运行检查,因为我传递了req,但是校验部分将不会有条件地运行,因此可以轻松处理验证器的功能部分。请帮助
我尝试将检查部件转为功能,但无法正常工作。这是我想要达到的目标,但进修失败
const onewayCheck = body('tripType').equals('one-way') ? [
body('currentOfficeLocation')
.exists({ checkFalsy: true })
.withMessage('currentOfficeLocation Current Office Location is required')
.isInt()
.withMessage('currentOfficeLocation Current Office Location must be an integer'),
body('destination')
.exists({ checkFalsy: true })
.withMessage('destination Destination is required')
.isInt()
.withMessage('destination Destination must be an integer'),
body('departureDate')
.exists({ checkFalsy: true })
.withMessage('departureDate Departure date is required'),
body('travelreasons')
.exists({ checkFalsy: true })
.withMessage('travelReasons Travel Reasons is required')
.isString()
.withMessage('travelReasons Travel Reasons should be strings'),
body('accommodation')
.exists({ checkFalsy: true })
.withMessage('accommodation Accommodation is required')
.isInt()
.withMessage('accommodation Accommodation must be an integer'),
] : [];
我要确保检查锅
答案 0 :(得分:0)
在验证过程中无需运行检查。相反,您可以在控制器中检查它。
例如,假设您像这样
bcftools_filter
您可以通过这种方式检查是否返回错误
input
这样,验证逻辑将在任何情况下运行,但是仅当const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
的值为if (req.body.tripType === 'one-way') {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
}
时,您才返回验证错误。
答案 1 :(得分:0)
有人帮助我解决了问题 /// route.js
const router = express.Router();
const validate = (validations, tripType) => {
return async (req, res, next) => {
if (req.body.tripType === tripType) {
await Promise.all(validations.map(validation => validation.run(req)));
}
return next();
};
};
router.post('/request', authenticate, validateRequestType, validate(onewayCheck(), 'one-way'),
onewayValidateInput, multicityCheck, multicityValidateInput, tripRequest);
export default router;
///in the validation file
const onewayCheck = () => [
body('currentOfficeLocation')
.exists({ checkFalsy: true })
.withMessage('currentOfficeLocation Current Office Location is required')
.isInt()
.withMessage('currentOfficeLocation Current Office Location must be an integer'),
body('destination')
.exists({ checkFalsy: true })
.withMessage('destination Destination is required')
.isInt()
.withMessage('destination Destination must be an integer'),
body('departureDate')
.exists({ checkFalsy: true })
.withMessage('departureDate Departure date is required'),
body('travelreasons')
.exists({ checkFalsy: true })
.withMessage('travelReasons Travel Reasons is required')
.isString()
.withMessage('travelReasons Travel Reasons should be strings'),
body('accommodation')
.exists({ checkFalsy: true })
.withMessage('accommodation Accommodation is required')
.isInt()
.withMessage('accommodation Accommodation must be an integer'),
];
答案 2 :(得分:0)
我能够通过在路由中使用中间件来解决此问题
router
.post('/request', validate(onewayCheck(), 'one-way'),
onewayValidateInput,
validate(multicityCheck(), 'Multi-city'), multicityValidateInput, tripRequest)
validate函数检查请求的类型
const validate = (validations, tripType) => async (req, res, next) => {
if (req.body.tripType === tripType) {
await Promise.all(validations.map(validation => validation.run(req)));
}
return next();
};