Isdatestring Class Validator Nestjs

I have an endpoint that is responsible for bulk inserting some data into my postgres DB

  //Bulk insert
  @httpPost("/profile/bulk-create", 
    body()
      .customSanitizer(sanitizeToClass(BulkCreateDto))
      .custom(validateObject),
    checkValidation)
  .........
  .............

In my dto class i have a field that deals with a date value

export class CreateAccountProfileDto{
    .....
    .......

    @Expose()
    @IsDateString()
    @IsOptional()
    OpenDate: Date
 
    .......
    .......
}

The date value received is in the following format 2021/04/19 and every time i try to bulk insert into DB, the following error shows up

OpenDate must be a valid ISO 8601 date string

Does the @IsDateString class-validator not work with the above date format?

1

1 Answer

@IsDateString() is an alias for @IsISO8601(options?: IsISO8601Options), which checks using validator's isISO8601 function that checks if the string is a valid ISO 8601 date.

From the documentation of validator:

options is an object which defaults to { strict: false, strictSeparator: false }. If strict is true, date strings with invalid dates like 2009-02-29 will be invalid. If strictSeparator is true, date strings with date and time separated by anything other than a T will be invalid.

So @IsISO8601() considers 2021/04/19 to be invalid, but 2021-04-19 to be valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest