fix: `@IsDateString()` decorator is not validating dates like 2019-02-31
See original GitHub issueDescription
The decorator @IsDateString()
is not validating dates like 2019-02-31
Minimal code-snippet showcasing the problem
Here the original source code, please see the big arrow indicating where the error is.
export function isDateString(value: unknown, options?: ValidatorJS.IsISO8601Options): boolean {
return isISO8601(value, options);
}
/**
* Alias for IsISO8601 validator
*/
export function IsDateString(
options?: ValidatorJS.IsISO8601Options,
validationOptions?: ValidationOptions
): PropertyDecorator {
return ValidateBy(
{
name: IS_DATE_STRING,
constraints: [options],
validator: {
validate: (value, args): boolean => isDateString(value), //<============ HERE THE options argument IS MISSING.
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must be a valid ISO 8601 date string',
validationOptions
),
},
},
validationOptions
);
}
Expected behavior
Invalidates dates like 2019-02-31
.
Actual behavior
It doesn’t invalidate dates like 2019-02-31
.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Nest class-validator minDate throws error even when the date ...
To validate with @MinDate export class SchemaDto { @IsNotEmpty() @Transform( ({ value }) => new Date(value)) @IsDate() @MinDate(new Date()) ...
Read more >class-validator - npm
Decorator -based property validation for classes. ... password wil be validated not only against IsString, but against MinLength as well.
Read more >Combining Validators and Transformers in NestJS
For example, the class-validator package has the @IsNumber() decorator to perform runtime validation that a field is a valid number, while the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@braaar
The arg
options
can contain params likestrict
andstrictSeparator
The following declared function:
Receives the
options
that will be passed to the functionisISO8601(value, options);
Since that function (
isDateString
) it is called as follow:We can see that
isDateString
is called with thevalue
only, so at the end, the functionisISO8601
won’t receive the necessary options likestrick
andstrictSeparator
.Because of that, the nested validator lib won’t validate wrong dates like
2019-02-31
This was fixed in
0.14.0
. The below snippet now correctly returns a validation error.