1

我想在对象构造函数中使用 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 构造函数时,代码会运行 Animal 类的 super() 和 this.validate()。此方法验证 Dog 的实例,并且由于 Animal 没有品种属性,因此代码会抛出上面的错误。

我不知道如何解决这个问题,也许将验证放在构造函数中并不是一个好主意。

是否有解决方法或更好的方法来做到这一点?

4

1 回答 1

5

您收到错误的原因不是因为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()must 是构造函数中的第一条语句,因此您无法使用当前模式避免这种情况。

解决方案之一是避免调用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()在父类和子类中声明相同的确切方法。

于 2018-12-24T12:55:40.173 回答