question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eleazar1595commented, Apr 2, 2022

@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

0reactions
NoNameProvidedcommented, Dec 9, 2022

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);
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found