我将 Nest 与类验证器一起使用,并尝试使用 @isDateString()
验证 UI 提供的日期是否不早于今天。和 @MinDate()
export class SchemaDto {
@IsOptional()
@IsDateString()
@MinDate(new Date())
myDate?: Date;
}
我发送的数据是一个反对日期,日期如下:
Mon Oct 04 2021 00:00:00 GMT-0400 (Eastern Daylight Time)
我从类验证器得到的错误是:
minimal allowed date for myDate is Fri Oct 01 2021 15:57:18 GMT-0400 (Eastern Daylight Time)
问题:
如果我删除 minDate 装饰器,一切正常,但我宁愿验证这种常见情况。
信息: typescript :~4.1.4
节点:12.4.0
类验证器:0.13.1
巢:7.0.0
最佳答案
使用@MinDate 进行验证
export class SchemaDto {
@IsNotEmpty()
@Transform( ({ value }) => new Date(value))
@IsDate()
@MinDate(new Date())
myDate: Date;
}
但是如果值是可选的,它可能为 null 或未定义,所以:
export class SchemaDto {
@IsOptional()
@Transform( ({ value }) => value && new Date(value))
@IsDate()
@MinDate(new Date())
myDate?: Date;
}
https://stackoverflow.com/questions/69411838/