假设我有一个基于文档中示例的类(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 } });
}
}我创建了这个类的一个实例,并为这些字段赋值。
const post = new Post()
post.body = 'body'
post.title = 'title'
// ... assign all the other fields我想验证post,跳过对除title之外的所有字段的验证。除了将组分配给所有字段之外,似乎没有一种方法可以做到这一点,这是我不想做的。有没有办法只验证这个字段?
发布于 2020-05-18 23:51:08
不,不幸的是,没有一种方法可以在不分配组的情况下只验证单个字段。
发布于 2021-03-19 14:55:38
我注意到了this,它对我很有效。
一种解决方法是只传递一个字段,并使用“skipMissingProperties”选项进行验证。
const exampleModel = new ExampleModel();
exampleModel.password = '123abc'
const errors = await validate(exampleModel, { skipMissingProperties: true })https://stackoverflow.com/questions/61395176
复制相似问题