Is there a way to validate an array of enums in a DTO without getting misleading error messages?
Here is an example of my 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: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8959980d6958d8b8c9d8a95999696b88b999588949dd69b9795">[email protected]</a>',
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([UserModule.Dashboard, UserModule.AccountManagement, UserModule.ReportManagement])
@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([UserAction.Manage, UserAction.Create, UserAction.Read, UserAction.Update, UserAction.Delete])
@IsEnum(UserAction, { each: true })
@IsArray()
@IsNotEmpty()
userActions: UserAction[];
}
This is how the POSTMAN request body looks like:
{
"name": "David",
"email": "<a href=\"/cdn-cgi/l/email-protection\" class=\"__cf_email__\" data-cfemail=\"2e4a4f58474a6e49434f4742004d41443\">[email protected]</a>",
"password": "123456",
"userRole": "admin",
"userModules": [ "dashboard", "account-management" ],
"userActions": [ "manage" ]
}
But when I make this POSTMAN request, I receive the following response:
{
"statusCode": 400,
"message": [
"userModules must be one of the following values: dashboard, account-management, report-management",
"userActions must be one of the following values: manage, create, read, update, delete"
],
"error": "Bad Request"
}
I don't understand why it's asking for specific values when they are clearly defined. I even tried implementing my own ValidatorConstraintInterface, but with no luck.