Description
Context
The @IsEnum()
decorator in class-validator is officially designed to accept an enum object, ensuring that the validated value is part of a specified TypeScript enum like this:
import { IsEnum } from 'class-validator';
enum Provider {
kakao = 'kakao',
google = 'google',
}
export class LoginDto {
@IsEnum(Provider)
provider: Provider;
}
However, many users unofficially use arrays as an alternative to enums when working with this decorator like this:
import { IsEnum } from 'class-validator';
export class LoginDto {
@IsEnum(['kakao', 'google'])
provider: 'kakao' | 'google';
}
Problem
When passing an array directly to @IsEnum(), the default error messages get truncated or don't display the expected values correctly. Specifically, the $constraint2
placeholder does not behave as intended, causing incomplete or incorrect validation messages.
Proposed Feature
Rather than considering this a bug, it would be better to officially support arrays as valid input for the @IsEnum() decorator. This would allow developers to pass arrays of valid values, and the decorator would handle them as enums internally, without causing issues with error messages. This would also align with how many developers are already using this decorator in their projects.