【发布时间】:2021-04-22 18:56:30
【问题描述】:
我在验证嵌套对象时遇到了困难。使用类验证器运行 nestJs。顶级字段(first_name、last_name 等)验证正常。 Profile 对象在顶层被验证 OK,即如果我作为数组提交,我会得到正确的错误,它应该是一个对象。
然而,个人资料的内容并未得到验证。我遵循了文档上的建议,但也许我只是遗漏了一些东西。
有人知道如何验证嵌套对象字段吗?
export enum GenderType {
Male,
Female,
}
export class Profile {
@IsEnum(GenderType) gender: string;
}
export class CreateClientDto {
@Length(1) first_name: string;
@Length(1) last_name: string;
@IsEmail() email: string;
@IsObject()
@ValidateNested({each: true})
@Type(() => Profile)
profile: Profile;
}
当我发送此有效负载时,我预计它会失败,因为性别不在枚举或字符串中。但它并没有失败
{
"first_name":"A",
"last_name":"B",
"profile":{
"gender":1
}
}
【问题讨论】: