I am using express-validator to do my server validation and I have come across a little trouble with my date validation. I am trying to validate that my Start Date is before my End Date. I am currently using this:
check('taskStartDate', { isBefore : ('taskEndDate' === undefined ) })
.isBefore('taskEndDate').withMessage('Start Date must be before End Date')
However, the problem with that is no matter what date I input into my form, whether the start date is before or after the end date, I get my implemented message 'Start Date must be before End Date'. I don't know how else to go about doing this besides a custom validator, which if that is the recommended way, I would appreciate any guidance on how to go about writing the custom validation. I also tried to include sanitization like so:
check('taskStartDate').isBefore(sanitizeBody('taskEndDate').toDate())
.withMessage( 'Start Date must be before End Date.')
but that did not work either. Any help would be greatly appreciated!
Yes, using a custom validator is the way to go when you want to check a value against another in the request.
You would be interested in the following combo of sanitizers and validator (sanitizers run before the validators, so you can get the value as
Dateobjects when using them):Look at the docs for further examples.