diff --git a/src/decorator/typechecker/IsEnum.ts b/src/decorator/typechecker/IsEnum.ts index eb3d6b064b..e3e0402fb5 100644 --- a/src/decorator/typechecker/IsEnum.ts +++ b/src/decorator/typechecker/IsEnum.ts @@ -15,6 +15,9 @@ export function isEnum(value: unknown, entity: any): boolean { * Returns the possible values from an enum (both simple number indexed and string indexed enums). */ function validEnumValues(entity: any): string[] { + if (Array.isArray(entity)) { + return entity; + } return Object.entries(entity) .filter(([key, value]) => isNaN(parseInt(key))) .map(([key, value]) => value as string); diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 9f938616c4..a4cfac4ebb 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -918,6 +918,11 @@ describe('IsEnum', () => { someProperty: MyStringEnum; } + class MyClassFour { + @IsEnum(['first', 'second']) + someProperty: 'first' | 'second'; + } + it('should not fail if validator.validate said that its valid', () => { return checkValidValues(new MyClassTwo(), validValues); }); @@ -967,6 +972,20 @@ describe('IsEnum', () => { const message = 'someProperty must be one of the following values: first, second'; return checkReturnedError(new MyClassThree(), invalidValues, validationType, message); }); + + it('should not fail if validator.validate said that its valid (array of strings)', () => { + return checkValidValues(new MyClassFour(), validStringValues); + }); + + it('should fail if validator.validate said that its invalid (array of strings)', () => { + return checkInvalidValues(new MyClassFour(), invalidValues); + }); + + it('should return error with proper message for array of strings', () => { + const validationType = 'isEnum'; + const message = 'someProperty must be one of the following values: first, second'; + return checkReturnedError(new MyClassFour(), invalidValues, validationType, message); + }); }); describe('IsDivisibleBy', () => {