class-验证器无法通过“必须是 ISOString 但记录的值显然是 ISO 字符串?”对 IsDateString 进行验证?
class-validator fails validation for IsDateString with 'must be ISOString but value logged is clearly an ISO String?
确切错误:
"value": "2019-10-09T10:36:40.791Z",
"property": "createdAt",
"children": [],
"constraints": {
"isDateString": "createdAt must be a ISOString"
}
class中的字段:
@prop({ default: new Date(Date.now()) })
@IsDefined()
@IsDateString()
createdAt: Date;
正在获取来自数据库的文档:
const order = await this.order.findOne({
_id: body._id,
clientId: user.clientId,
});
调用验证器:
let orderData = order.toJSON();
let finalOrder = plainToClass(FinishedOrder, orderData);
let validation = await validateOrReject(finalOrder);
我也试过调用order.toObject()
,但效果一样。
错误中记录的不是对象本身,事实证明 mongoose 会将日期字符串转换为日期对象。如果值是日期,我最终检查了 属性 可选。
确切错误:
"value": "2019-10-09T10:36:40.791Z",
"property": "createdAt",
"children": [],
"constraints": {
"isDateString": "createdAt must be a ISOString"
}
class中的字段:
@prop({ default: new Date(Date.now()) })
@IsDefined()
@IsDateString()
createdAt: Date;
正在获取来自数据库的文档:
const order = await this.order.findOne({
_id: body._id,
clientId: user.clientId,
});
调用验证器:
let orderData = order.toJSON();
let finalOrder = plainToClass(FinishedOrder, orderData);
let validation = await validateOrReject(finalOrder);
我也试过调用order.toObject()
,但效果一样。
错误中记录的不是对象本身,事实证明 mongoose 会将日期字符串转换为日期对象。如果值是日期,我最终检查了 属性 可选。