使用 class-validator 验证类中的一个字段

yal*_*man 5 typescript class-validator

假设我有一个基于文档中示例的类(https://github.com/typestack/class-validator#usage

import {MinLength, MaxLength, validate} from "class-validator";

export class Post {

    @IsString()
    body: strong;

    @IsString()
    title: string;

    //...many more fields

    public async validate(){
        validate(this, { forbidUnknownValues: true, validationError: { target: false } });
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建此类的一个实例并向字段分配值。

const post = new Post()
post.body = 'body'
post.title = 'title'
// ... assign all the other fields
Run Code Online (Sandbox Code Playgroud)

我想验证post,跳过除 之外的所有字段的验证title。除了将组分配给所有字段之外,似乎没有其他方法可以做到这一点,而我不想这样做。有没有办法只验证这个单一字段?

sat*_*ime 3

不,不幸的是,没有办法在不分配组的情况下仅验证单个字段。