I'm using express-validator for validation for mandatory fields. But some fields are not mandatory like date of birth, price. I want to validate if these fields contains a value, like if I provide the date of birth value, then it must be of date type. I've checked every tutorials, docs & tried almost everything, but failed. How to validate fields when it contains value. Custom method is the last option to try. can i make it without custom method?
You can do it with optional
and checkFalsy. This will only run the isISO8601()
validation on the dateOfBirth
field if not is null, undefined, 0, false, or empty string
check('dateOfBirth').optional({ checkFalsy: true }).isISO8601()