gpt4 book ai didi

javascript - 使用类验证器中的 @MinDate 会导致 TypeError : Cannot read property 'getTime' of undefined?

转载 作者:行者123 更新时间:2023-12-03 04:25:57 25 4
gpt4 key购买 nike

我有一堂这样的课:

    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");
}
});
});
});

想法?

最佳答案

I asked the question differently here 。装饰器仅支持编译时值。运行时可用的值无法传入,因此我在上述场景中遇到了问题。

关于javascript - 使用类验证器中的 @MinDate 会导致 TypeError : Cannot read property 'getTime' of undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com