I have exact problem as TLmaK0 here: https://github.com/nestjs/nest/issues/9076
Was his question ever answered?
12 messages · Page 1 of 1 (latest)
I have exact problem as TLmaK0 here: https://github.com/nestjs/nest/issues/9076
Was his question ever answered?
Not sure if you are referring to export class FooDto from related thread but it will still accept both IETF-compliant RFC 2822 timestamps, and also ISO8601 due to the nature of IsDate().
have you tried the given example with @IsISO8601({ strict: true }) option?
@IsISO8601(options?: IsISO8601Options)
Checks if the string is a valid ISO 8601 date.
If given value is not a string, then it returns false.
Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
It does not matter. The problem is how NestJS works:
I was hoping for some workound as we want to accept ISO8601 dates only.
I guess we will stay with
@Type(() => Date)
@IsDate()
date: Date
😦
There should be something like this in class-validator:
@Type(() => Date)
@IsDate({ acceptFormat: ['iso8601'] })
date: Date
import { IsISO8601 } from 'class-validator';
export class DateDto {
@IsISO8601({ strict: true })
date: Date;
}
I just tried that and it works perfectly, throws error on IETF-compliant RFC 2822 timestamps:
"Mon, 03 Jun 2023 09:30:00 +0000"
"Tue, 04 Jun 2023 14:45:30 -0400"
"Wed, 05 Jun 2023 20:15:10 +0800"
"Thu, 06 Jun 2023 03:22:45 -0700"
Are you sure your date: Date is really a Date type without @Type(() => Date) ?
I am having:
date.getTime is not a function
The order of transformation and then validation is kind of unfortunate, but there's no other way, class validator only works on classes, so it must be first transformed into a class. This causes problems like you describe. The "solution" is to create custom transform functions which also do the validation in the same step and skip the class-validator decorator.