diff --git a/src/rules/isIn.ts b/src/rules/isIn.ts index a0ad8d1..7e032c6 100644 --- a/src/rules/isIn.ts +++ b/src/rules/isIn.ts @@ -1,5 +1,15 @@ -export default (value: any, options: any[] | string): boolean => { - const list: any[] = Array.isArray(options) ? options : options.split(","); +export default (value: any, ...args: any[]): boolean => { + // Let's check the last item is IContext + const lastItem: any = args.at(-1); + if (lastItem.definition && lastItem.field) { + args.pop(); + } + + const [options] = args; + + const list: any[] = Array.isArray(options) + ? options + : (options as string).split(","); const listAsString = list.map((item) => String(item).trim()); if (Array.isArray(value)) { diff --git a/tests/rules/in.test.ts b/tests/rules/in.test.ts index 73e514f..69189f9 100644 --- a/tests/rules/in.test.ts +++ b/tests/rules/in.test.ts @@ -43,5 +43,18 @@ describe("isIn() ", () => { it("should be able to parse string values", async () => { expect(isIn("A", "A,B,B")).toBe(true); + expect(isIn("B", "A,B,C")).toBe(true); + expect(isIn("C", "A,B,C")).toBe(true); + expect(isIn("D", "A,B,C")).toBe(false); + + expect(isIn("a", ["a", "b", "c"])).toBe(true); + expect(isIn("b", ["a", "b", "c"])).toBe(true); + expect(isIn("c", ["a", "b", "c"])).toBe(true); + expect(isIn("d", ["a", "b", "c"])).toBe(false); + }); + + it("should be able to work with spread-string options", async () => { + expect(isIn("A", ...["A,B,B"])).toBe(true); + expect(isIn("A", ...["A", "B", "C"])).toBe(true); }); });