class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: The decorator @IsDateString() is not validating dates like 2019-02-31

Open eleazar1595 opened this issue 4 years ago • 5 comments

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.

eleazar1595 avatar Mar 25 '22 21:03 eleazar1595

let dateString =  /\d{2,4}(\-\d{1,2}){2}/

jingyuexing avatar Mar 31 '22 20:03 jingyuexing

IsDateString is an alias for Is8601, which matches the ISO 8601 date format. Have you tried IsDate?

braaar avatar Apr 01 '22 05:04 braaar

IsDateString is an alias for Is8601, which matches the ISO 8601 date format. Have you tried IsDate?

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.

eleazar1595 avatar Apr 01 '22 12:04 eleazar1595

IsDateString is an alias for Is8601, which matches the ISO 8601 date format. Have you tried IsDate?

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.

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 avatar Apr 01 '22 13:04 braaar

@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

eleazar1595 avatar Apr 02 '22 14:04 eleazar1595

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);

NoNameProvided avatar Dec 09 '22 20:12 NoNameProvided

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.

github-actions[bot] avatar Jan 09 '23 00:01 github-actions[bot]