作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一堂这样的课:
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/
我是一名优秀的程序员,十分优秀!