class-validator
class-validator copied to clipboard
fix: There might be a bug in IsISO8601/IsDateString in strict mode
Description
According to the type docs, when using IsDateString({ strict: true})
:
"If strict
is true
, performs additional checks for valid dates, e.g. invalidates dates like 2009-02-29
". But those dates appear to be passing the validation in my app
Minimal code-snippet showcasing the problem So I created a repo for bug reproduction here. In essence:
class ValidatorTest {
@IsDateString({ strict: true })
isoDate: string
}
const instance = new ValidatorTest()
instance.isoDate = '2009-02-29'
await assert.rejects(() => validateOrReject(instance), 'Class successfully validated') // <<<< Fails
Expected behavior
According to the docs, a date as "2009-02-29" should fail when using isoDate validation with strict = true; (taken directly from the docs)
Actual behavior
The validator passes
Version
class-validator: 0.13.1 node: 14
Same issue here. Seems to be an issue with class-validator
itself, not validator.js
it's using.
Following code works as expected with validator.js
:
const validator = require('validator');
validator.isISO8601('2009-02-29', {strict: true});
https://runkit.com/hakimio/60aca6995be746001af0ca45
Ok, found the bug here. Unlike IsISO8601, IsDateString
doesn't send the options object to the validator.
Line 27 should be changed to the following in IsDateString.ts
:
validate: (value, args): boolean => isDateString(value, args.constraints[0])
Current workaround would be to use IsISO8601
instead of IsDateString
.
@NoNameProvided do you want me to create a new PR or can you just fix this?
PR #1097
Related: https://github.com/validatorjs/validator.js/issues/2003
Merged in https://github.com/typestack/class-validator/pull/1720.
This feature/fix has been released in 0.14.0
.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.