我想在对象构造函数中使用validateSync,但我不能将其用于继承。
我得到了类似这样的东西:
import { IsNotEmpty, IsString, validateSync, validate } from 'class-validator';
class Animal {
@IsNotEmpty()
@IsString()
public name: string;
constructor(name: string) {
this.name = name;
this.validate() // this method calls validate of class Dog, since this is an instance of Dog
}
protected validate() {
const errors = validateSync(this);
if (errors.length > 0) {
console.log("Animal validation error: ", errors)
}
}
}
class Dog extends Animal {
@IsNotEmpty()
@IsString()
public breed: string;
constructor(name: string, breed: string) {
super(name);
this.breed = breed
this.validate()
}
protected validate() {
const errors = validateSync(this);
if (errors.length > 0) {
console.log("Dog validation error: ", errors)
}
}
}
const dog = new Dog('Aaron', 'Golden Retriever')结果是:
Dog validation error: [ ValidationError {
target: Dog { name: 'Aaron' },
value: undefined,
property: 'breed',
children: [],
constraints:
{ isString: 'breed must be a string',
isNotEmpty: 'breed should not be empty' } } ]当我调用Dog构造函数时,代码先运行super(),然后运行Animal类的this.validate()。这个方法验证了Dog的一个实例,因为Animal没有breed属性,所以代码抛出了上面的错误。
我不知道如何解决这个问题,也许将验证放在构造函数中不是一个好主意。
有没有变通的方法或者更好的方法呢?
发布于 2018-12-24 20:55:40
出现错误的原因不是因为Animal没有breed属性(实际上它有,因为实例实际上是Dog),而是因为您在设置所有要验证的值之前调用了验证(其中之一)。另一件事是,你做了两次验证,这听起来不太对。
class Dog extends Animal {
constructor(name: string, breed: string) {
super(name); // <-- validation gets called in the parent but you did't set the breed property yet
this.breed = breed;
this.validate(); // <-- validation gets called for the second time, but this time it passes
}
}由于super()必须是构造函数中的第一个语句,因此您无法在当前模式中避免这一点。
解决方案之一是避免在基类中调用validate(),让子类正确设置所有字段,然后才进行验证。
我不认为在构造函数中验证必需的属性有任何错误,因为您正在阻止创建具有无效状态的对象。但是,如果在创建对象时不一定要拥有所有的值,我建议让调用者决定何时进行验证:
const dog = new Dog('Aaron');
// do some stuff like determine the breed...
dog.breed = 'Golden Retriever';
// do some more stuff
const validationResult = dog.validate(); // did I succeed?另请注意,您似乎不需要在父类和子类中声明完全相同的方法validate()。
https://stackoverflow.com/questions/53913344
复制相似问题