Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leemhoon00 opened this issue Sep 25, 2024 · 0 comments · May be fixed by #2537
Open

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

leemhoon00 opened this issue Sep 25, 2024 · 0 comments · May be fixed by #2537
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@leemhoon00
Copy link

leemhoon00 commented Sep 25, 2024

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.

@leemhoon00 leemhoon00 added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Sep 25, 2024
@leemhoon00 leemhoon00 changed the title feature: Support for Array as Valid Values in @IsEnum() Decorator feature: Enhance Message Support for Array Input in @IsEnum() Decorator Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

Successfully merging a pull request may close this issue.

1 participant