我需要根据params验证各种字段。因此,在我的nodejs路由中,如果幻灯片等于1(在params中(,则仅验证姓名、电子邮件和电话号码。如果幻灯片等于2,则仅验证城市和州。如果幻灯片等于三,则验证是否存在用户地址。我想用一种方法进行验证。如果可能的话,请帮忙。感谢
好的,我得到了解决方案,如果幻灯片值为零,则验证地址、状态、postal_code等。如果幻灯片值是1,则相同,依此类推。
`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) {`