How to check if the DOB is smaller than DOD in nodejs express validator

30 views Asked by At

I am using the express validator for the validation in nodejs. I need to show error message if the user select date of birth greater than date of dead.

check('dob').notEmpty().bail().withMessage('Please enter the DOB').toDate().bail(); Currently I am using the above code for dob error but not sure how to add the another check in it.

1

There are 1 answers

0
Federico Ciardi On

This is a way to achieve what you are asking. Be aware that if dob fails validation, dod still tries to validate.

body('dob').isDate().bail().toDate()
body('dod').isDate().bail().toDate().custom((dod, { req }) => {
  return dod > req.body.dob
})