我有这样的课:
export default class Order {
sku: string;
@IsDate()
purchaseDate: Date;
@IsDate()
@MinDate(this.purchaseDate)
receiptDate: Date;
}
我创建一个这样的订单实例:
const o = new Order();
o.sku = "sku1";
o.purchaseDate = new Date(2014, 11, 17);
o.receiptDate = new Date(2016, 11, 17);
并验证:
validate(o).then((errors:ValidationError[]) => { // errors is an array of validation errors
if (errors.length > 0) {
console.log("validation failed. errors: ", errors);
} else {
console.log("validation succeed");
}
});
这会导致以下错误:
TypeError: Cannot read property 'getTime' of undefined
at Validator.minDate (src/validation/Validator.ts:414:49)
完整的测试代码如下所示:
import { validate, ValidationError } from "class-validator";
import 'mocha';
describe('Order Validation', () => {
it('should be a valid order', () => {
const o = new Order();
o.sku = "sku1";
o.purchaseDate = new Date(2014, 11, 17);
o.receiptDate = new Date(2016, 11, 17);
validate(o).then((errors:ValidationError[]) => { // errors is an array of validation errors
if (errors.length > 0) {
console.log("validation failed. errors: ", errors);
} else {
console.log("validation succeed");
}
});
});
});
想法?