Javascript 如何使用express validator(版本6.6.1)按条件验证字段
我需要根据参数验证各个字段。因此,在我的节点js路由中,如果一张幻灯片等于一(在参数中),则只验证姓名、电子邮件和电话号码。如果幻灯片等于2,则仅验证城市和州。如果幻灯片等于三,则验证用户地址是否存在。我想在一条路线上进行验证。如果可能的话,请帮忙。谢谢好的,我得到了解决方案,如果幻灯片值为零,则验证地址、状态、邮政编码等。如果幻灯片值为1,则相同,依此类推Javascript 如何使用express validator(版本6.6.1)按条件验证字段,javascript,node.js,validation,backend,express-validator,Javascript,Node.js,Validation,Backend,Express Validator,我需要根据参数验证各个字段。因此,在我的节点js路由中,如果一张幻灯片等于一(在参数中),则只验证姓名、电子邮件和电话号码。如果幻灯片等于2,则仅验证城市和州。如果幻灯片等于三,则验证用户地址是否存在。我想在一条路线上进行验证。如果可能的话,请帮忙。谢谢好的,我得到了解决方案,如果幻灯片值为零,则验证地址、状态、邮政编码等。如果幻灯片值为1,则相同,依此类推 `router.post("/updateUser", [ //first step body(&qu
`router.post("/updateUser",
[
//first step
body("slide").not().isEmpty().withMessage('please provide slide number.'),
body("birthDate").if(body('slide').custom((value, {
req
}) => value == 0)).not().isEmpty().withMessage('BirthDate is required.'),
body("address").if(body('slide').custom((value, {
req
}) => value == 0)).not().isEmpty().withMessage('address is required.'),
body("state").if(body('slide').custom((value, {
req
}) => value == 0)).not().isEmpty().withMessage('state is required.'),
body("postal_code").if(body('slide').custom((value, {
req
}) => value == 0)).not().isEmpty().withMessage('postal code is required.'),
//second step
body("interests").if(body('slide').custom((value, {
req
}) => value == 1)).isArray({
min: 1
}).withMessage('at least one interests is required.'),
//third step
body("home").if(body('slide').custom((value, {
req
}) => value == 2)).not().isEmpty().withMessage('homeOwner is required.'),
//four step
body("driveVehicle").if(body('slide').custom((value, {
req
}) => value == 3)).not().isEmpty().withMessage('driveVehicle is required.'),
//fifth step
body("vehicleDetails.model").if(body('slide').custom((value, {
req
}) => value == 4)).not().isEmpty().withMessage('vehicle model is required.'),
body("vehicleDetails.mileage").if(body('slide').custom((value, {
req
}) => value == 4)).not().isEmpty().withMessage('vehicle mileage required.')
],async function (req, res, next) {`
如果您共享一个您可能尝试过的代码,这样用户就可以查看并建议您可能出了什么问题,这会更好