fix: The decorator @IsDateString() is not validating dates like 2019-02-31
Description
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.
let dateString = /\d{2,4}(\-\d{1,2}){2}/
IsDateString is an alias for Is8601, which matches the ISO 8601 date format. Have you tried IsDate?
IsDateStringis an alias forIs8601, which matches the ISO 8601 date format. Have you triedIsDate?
I had to change it to IsISO8601 while someone fixes this.
The fix is very simple, pass the arg options
validate: (value, args): boolean => isDateString(value), //<============ HERE THE options argument IS MISSING.
IsDateStringis an alias forIs8601, which matches the ISO 8601 date format. Have you triedIsDate?I had to change it to
IsISO8601while someone fixes this. The fix is very simple, pass the argoptionsvalidate: (value, args): boolean => isDateString(value), //<============ HERE THE options argument IS MISSING.
Sorry, I misread the behaviour and though it was validating the bad date. I had it backwards.
Why would adding the options parameter change the behaviour for invalid Iso8601 dates? Shouldn't this work as it is without any options?
@braaar
The arg options can contain params like strict and strictSeparator
The following declared function:
export function isDateString(value: unknown, options?: ValidatorJS.IsISO8601Options): boolean {
return isISO8601(value, options);
}
Receives the options that will be passed to the function isISO8601(value, options);
Since that function (isDateString) it is called as follow:
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
),
}
We can see that isDateString is called with the value only, so at the end, the function isISO8601 won't receive the necessary options like strick and strictSeparator.
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.
import { IsDateString, validate } from 'class-validator';
class MyPayload {
@IsDateString({ strict: true })
date: string;
constructor(date?: string) {
this.date = date;
}
}
// This will fail
validate(new MyPayload('2019-02-31')).then(console.log);
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.