繁体   English   中英

Nest.JS DTO 验证

[英]Nest.JS DTO Validation

我的 DTO 是

 @Expose()
 @IsNotEmpty()
 @IsJSON({ each: true })
 filesRole: string

filesRole 是这样的: [{"file": "14125.png", "role": "bg"}, {"file": "x12.png", "role": "cover"}]

我想验证rolebg还是cover

你可以用枚举试试:

export enum Role {
  bg = 'bg',
  cover = 'cover',
}
@IsEnum(Role)
@Expose()
@IsNotEmpty()
@IsJSON({ each: true })
filesRole: Role

更新您的主要 DTO:

@Expose()
@IsNotEmpty()
@IsArray()
@ValidateNested({ each: true })
filesRole: Data[]; 

数据 DTO:

export class Data {
    @IsNotEmpty()
    @IsString()
    file: string;

    @IsNotEmpty()
    @IsString()
    @IsIn(Object.values(roleEnum))
    role: roleEnum;
}

角色枚举:

export enum roleEnum {
    bg = 'bg',
    cover = 'cover',
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM