Using the @MinDate from class-validator results in TypeError: Cannot read property 'getTime' of undefined?
Using the @MinDate from class-validator results in TypeError: Cannot read property 'getTime' of undefined?
我有一个 class 这样的:
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");
}
});
});
});
想法?
。装饰器仅支持编译时值。无法传入运行时可用的值,因此我在上述场景中遇到了问题。
我有一个 class 这样的:
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");
}
});
});
});
想法?