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()...
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 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:
optionsis an object which defaults to{ strict: false, strictSeparator: false }. Ifstrictistrue, date strings with invalid dates like 2009-02-29 will be invalid. IfstrictSeparatoris true, date strings with date and time separated by anything other than aTwill be invalid.
So @IsISO8601() considers 2021/04/19 to be invalid, but 2021-04-19 to be valid.