使用class-validator在TypeScript中验证枚举数组遇误导性报错
解决DTO枚举数组验证的错误提示问题
你遇到的问题根源是**@IsIn装饰器的使用方式不对**。对于数组类型的字段,@IsIn默认会把整个数组当作单个值,去和你指定的枚举列表做匹配——比如你的userModules传了["dashboard", "account-management"],它会检查这个数组本身是否等于"dashboard"、"account-management"或"report-management",显然不成立,所以抛出错误。
而你已经给数组字段加了@IsEnum(xxx, { each: true }),这个装饰器已经会逐个验证数组里的每个元素是否属于对应的枚举,完全不需要再用@IsIn画蛇添足。
修改后的DTO代码
import { IsArray, IsEmail, IsEnum, IsIn, IsNotEmpty, IsString } from "class-validator"; import { UserAction, UserModule, UserRole } from "../enums"; import { ApiProperty } from "@nestjs/swagger"; export class CreateUserDto { @ApiProperty({ example: 'e5c082ae-6760-4dbf-a69b-e01e94108c63', description: 'The unique identifier of an user' }) @IsString() @IsNotEmpty() name: string; @ApiProperty({ example: 'max.mustermann@sample.com', description: 'The email of an user' }) @IsString() @IsEmail() @IsNotEmpty() email: string; @ApiProperty({ example: '!s3cr3t!', description: 'The password of an user' }) @IsString() @IsNotEmpty() password: string; @ApiProperty({ example: [UserRole.Admin, UserRole.User], description: 'The role of an user', enum: UserRole, isArray: false }) @IsIn([UserRole.Admin, UserRole.User]) @IsEnum(UserRole) @IsNotEmpty() userRole: UserRole; @ApiProperty({ example: [UserModule.Dashboard, UserModule.AccountManagement, UserModule.ReportManagement], description: 'The module of an user', enum: UserModule, isArray: true }) // 去掉@IsIn,保留@IsEnum并开启each模式验证数组元素 @IsEnum(UserModule, { each: true }) @IsNotEmpty() userModules: UserModule[]; @ApiProperty({ example: [UserAction.Manage, UserAction.Create, UserAction.Read, UserAction.Update, UserAction.Delete], description: 'The action of an user', enum: UserAction, }) // 去掉@IsIn,保留@IsEnum和数组验证装饰器 @IsEnum(UserAction, { each: true }) @IsArray() @IsNotEmpty() userActions: UserAction[]; }
补充说明
- 对于单个枚举值的字段(比如
userRole),@IsIn和@IsEnum可以共存,但其实@IsEnum已经能验证值是否属于枚举,@IsIn在这里属于冗余配置,若要精简代码也可以直接去掉。 - 确保你的枚举定义和请求体里的字符串值完全匹配,比如
UserModule.Dashboard对应字符串"dashboard"、UserAction.Manage对应"manage",这样@IsEnum才能正确完成匹配验证。
内容的提问来源于stack exchange,提问作者DavidSch
相关产品推荐
相关产品推荐

