Reputation: 775
I have an array of dates in a post request body that I want to validate:
{
"meals": [...],
"dates": [
"2022-03-06T11:00:00.000Z",
"2022-03-07T11:00:00.000Z"
]
}
This is my dto class:
export class CopyMealsPlanDto {
...// Another array
@IsArray()
@ValidateNested({ each: true })
@IsDate()
@Type(() => Date)
dates: Date[];
}
But I'm getting this error:
{
"statusCode": 400,
"message": [
"dates must be a Date instance"
],
"error": "Bad Request"
}
Upvotes: 5
Views: 21227
Reputation: 99
Try this one:
export class CopyMealsPlanDto {
...// Another array
@IsDateString({}, { each: true })
dates: Date[];
}
You can read more about how to validate an array here.
Upvotes: 7
Reputation: 116
You can use @IsDateString() decorator
https://github.com/typestack/class-validator#validation-decorators
Upvotes: 0