Skip to content

Commit 2b60e0e

Browse files
fix: fail for non-array constraint in @IsIn decorator (#1844)
1 parent 0c30684 commit 2b60e0e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/decorator/common/IsIn.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { isIn } from './IsIn';
2+
3+
describe('@IsIn decorator implementation', () => {
4+
describe('isIn validator', () => {
5+
it('should accept valid values', () => {
6+
expect(isIn('A', ['A', 'B'])).toBe(true);
7+
expect(isIn('A', ['B', 'C'])).toBe(false);
8+
expect(isIn('A', [1, 2])).toBe(false);
9+
});
10+
11+
it('should not accept invalid values', () => {
12+
expect(isIn('A', 5 as any)).toBe(false);
13+
expect(isIn('A', 'ABC' as any)).toBe(false);
14+
expect(isIn('A', false as any)).toBe(false);
15+
});
16+
});
17+
});

src/decorator/common/IsIn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const IS_IN = 'isIn';
77
* Checks if given value is in a array of allowed values.
88
*/
99
export function isIn(value: unknown, possibleValues: readonly unknown[]): boolean {
10-
return !Array.isArray(possibleValues) || possibleValues.some(possibleValue => possibleValue === value);
10+
return Array.isArray(possibleValues) && possibleValues.some(possibleValue => possibleValue === value);
1111
}
1212

1313
/**

0 commit comments

Comments
 (0)