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。除了将组分配给所有字段之外,似乎没有其他方法可以做到这一点,而我不想这样做。有没有办法只验证这个单一字段?
| 归档时间: |
|
| 查看次数: |
4615 次 |
| 最近记录: |