Skip to content

feature: Enhance Message Support for Array Input in @IsEnum() Decorator #2536

Open
@leemhoon00

Description

@leemhoon00

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.

image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions