0

我有这样的课:

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

想法?

4

1 回答 1

1

我在这里提出了不同的问题。装饰器仅支持编译时间值。无法传入运行时可用的值,因此我在上述场景中遇到了问题。

于 2017-05-02T19:06:24.697 回答