diff --git a/src/decorator/typechecker/IsEnum.ts b/src/decorator/typechecker/IsEnum.ts index eb3d6b064b..1470da9aa0 100644 --- a/src/decorator/typechecker/IsEnum.ts +++ b/src/decorator/typechecker/IsEnum.ts @@ -7,8 +7,8 @@ export const IS_ENUM = 'isEnum'; * Checks if a given value is the member of the provided enum. */ export function isEnum(value: unknown, entity: any): boolean { - const enumValues = Object.keys(entity).map(k => entity[k]); - return enumValues.includes(value); + const enumValues = validEnumValues(entity); + return enumValues.includes(value as string); } /** diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 4c266f02ee..d6937d6a71 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -967,6 +967,25 @@ describe('IsEnum', () => { const message = 'someProperty must be one of the following values: first, second'; return checkReturnedError(new MyClassThree(), invalidValues, validationType, message); }); + + it('should not accept the enum member name for a custom indexed enum', () => { + expect(isEnum('First', MyCustomIndexedEnum)).toBeFalsy(); + expect(isEnum('Second', MyCustomIndexedEnum)).toBeFalsy(); + }); + + it('should not accept the enum member name for a default indexed enum', () => { + expect(isEnum('First', MyDefaultIndexedEnum)).toBeFalsy(); + expect(isEnum('Second', MyDefaultIndexedEnum)).toBeFalsy(); + }); + + it('should not accept the reverse-mapped numeric key string', () => { + expect(isEnum('1', MyCustomIndexedEnum)).toBeFalsy(); + expect(isEnum('999', MyCustomIndexedEnum)).toBeFalsy(); + }); + + it('should fail if the enum member name is passed instead of its value', () => { + return checkInvalidValues(new MyClassTwo(), ['First', 'Second']); + }); }); describe('IsDivisibleBy', () => {