Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fail for non-array constraint in @IsIn decorator #1844

Merged
merged 1 commit into from
Dec 9, 2022
Merged
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
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