Ede*_*dio 0 class-validator nestjs
我在验证嵌套对象时遇到困难。使用类验证器运行nestJs。顶级字段(名字、姓氏等)验证正常。Profile 对象在顶层验证正常,即如果我作为数组提交,我会得到正确的错误,它应该是一个对象。
然而,Profile 的内容尚未得到验证。我已遵循文档上的建议,但也许我只是错过了一些东西。
有谁知道如何验证嵌套对象字段?
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;
}
Run Code Online (Sandbox Code Playgroud)
当我发送此有效负载时,我预计它会失败,因为性别不在枚举或字符串中。但它并没有失败
{
"first_name":"A",
"last_name":"B",
"profile":{
"gender":1
}
}
Run Code Online (Sandbox Code Playgroud)
这将有助于:
export enum GenderType {
Male = "male",
Female = "female",
}
export class Profile {
@IsEnum(GenderType)
gender: GenderType;
}
export class CreateClientDto {
@IsObject()
@ValidateNested()
@Type(() => Profile)
profile: Profile;
}
Run Code Online (Sandbox Code Playgroud)
PS:你不需要,{each: true}
因为它是一个对象而不是数组
归档时间: |
|
查看次数: |
9779 次 |
最近记录: |