Skip to content

feat: display valid enum values for "isEnum" errors #887

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/decorator/typechecker/IsEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export function isEnum(value: unknown, entity: any): boolean {
return enumValues.includes(value);
}

/**
* Returns an array of all the possible values for a given enum
*/
export function validEnumValues(entity: any): any[] {
return Object.entries(entity)
.filter(entry => !parseInt(entry[0]))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implicitly defined enums actually start with 0 and due to type coercion this check is not reliable.

If you define

  enum MyEnum {
    First,
    Second,
  }

one of the entries is ['0', 'First'], and !parseInt('0') === !0 === true
and the return value for MyEnum will be ['First', '0', 1'] instead of the expected ['0', '1'].

It would be more reliable to check whether the key is not a number after all, like:

Suggested change
.filter(entry => !parseInt(entry[0]))
.filter(entry => isNaN(parseInt(entry[0])))

.map(entry => entry[1]);
}

/**
* Checks if a given value is an enum
*/
Expand All @@ -22,7 +31,8 @@ export function IsEnum(entity: object, validationOptions?: ValidationOptions): P
validator: {
validate: (value, args): boolean => isEnum(value, args?.constraints[0]),
defaultMessage: buildMessage(
eachPrefix => eachPrefix + '$property must be a valid enum value',
eachPrefix =>
eachPrefix + '$property must be a valid enum value. Valid values: ' + validEnumValues(entity).join(', '),
validationOptions
),
},
Expand Down
6 changes: 3 additions & 3 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,14 +942,14 @@ describe('IsEnum', () => {

it('should return error object with proper data', () => {
const validationType = 'isEnum';
const message = 'someProperty must be a valid enum value';
const message = 'someProperty must be a valid enum value. Valid values: 1, 999';
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
});

it('should return error object with proper data (string enum)', () => {
const validationType = 'isEnum';
const message = 'someProperty must be a valid enum value';
checkReturnedError(new MyClass2(), invalidValues, validationType, message);
const message = 'someProperty must be a valid enum value. Valid values: first, second';
return checkReturnedError(new MyClass2(), invalidValues, validationType, message);
});
});

Expand Down