Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/decorator/typechecker/IsEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down