From d109960395740cf5d58204406021be1d6dcbe854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Ol=C3=A1h?= Date: Fri, 9 Dec 2022 20:41:29 +0000 Subject: [PATCH] fix: fail for non-array constraint in `IsIn` decorator --- src/decorator/common/IsIn.spec.ts | 17 +++++++++++++++++ src/decorator/common/IsIn.ts | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/decorator/common/IsIn.spec.ts diff --git a/src/decorator/common/IsIn.spec.ts b/src/decorator/common/IsIn.spec.ts new file mode 100644 index 0000000000..c9027eecc5 --- /dev/null +++ b/src/decorator/common/IsIn.spec.ts @@ -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); + }); + }); +}); diff --git a/src/decorator/common/IsIn.ts b/src/decorator/common/IsIn.ts index cd9499e5e8..d074dcca5e 100644 --- a/src/decorator/common/IsIn.ts +++ b/src/decorator/common/IsIn.ts @@ -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); } /**