Skip to content

Commit

Permalink
fix: fail for non-array constraint in @IsIn decorator (#1844)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNameProvided authored Dec 9, 2022
1 parent 0c30684 commit 2b60e0e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/decorator/common/IsIn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { isIn } from './IsIn';

describe('@IsIn decorator implementation', () => {
describe('isIn validator', () => {
it('should accept valid values', () => {
expect(isIn('A', ['A', 'B'])).toBe(true);
expect(isIn('A', ['B', 'C'])).toBe(false);
expect(isIn('A', [1, 2])).toBe(false);
});

it('should not accept invalid values', () => {
expect(isIn('A', 5 as any)).toBe(false);
expect(isIn('A', 'ABC' as any)).toBe(false);
expect(isIn('A', false as any)).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion src/decorator/common/IsIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const IS_IN = 'isIn';
* Checks if given value is in a array of allowed values.
*/
export function isIn(value: unknown, possibleValues: readonly unknown[]): boolean {
return !Array.isArray(possibleValues) || possibleValues.some(possibleValue => possibleValue === value);
return Array.isArray(possibleValues) && possibleValues.some(possibleValue => possibleValue === value);
}

/**
Expand Down

0 comments on commit 2b60e0e

Please sign in to comment.