1

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 1

3

@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.

Sign up to request clarification or add additional context in comments.

1 Comment

2021-04-19 is also reported as an error for me. I am using IsISO8601 from @nestjs/class-validator

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.