Skip to content

Commit 02114dc

Browse files
committed
Fixed #70
1 parent 2166204 commit 02114dc

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/rules/isIn.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
export default (value: any, options: any[] | string): boolean => {
2-
const list: any[] = Array.isArray(options) ? options : options.split(",");
1+
export default (value: any, ...args: any[]): boolean => {
2+
// Let's check the last item is IContext
3+
const lastItem: any = args.at(-1);
4+
if (lastItem.definition && lastItem.field) {
5+
args.pop();
6+
}
7+
8+
const [options] = args;
9+
10+
const list: any[] = Array.isArray(options)
11+
? options
12+
: (options as string).split(",");
313
const listAsString = list.map((item) => String(item).trim());
414

515
if (Array.isArray(value)) {

tests/rules/in.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,18 @@ describe("isIn() ", () => {
4343

4444
it("should be able to parse string values", async () => {
4545
expect(isIn("A", "A,B,B")).toBe(true);
46+
expect(isIn("B", "A,B,C")).toBe(true);
47+
expect(isIn("C", "A,B,C")).toBe(true);
48+
expect(isIn("D", "A,B,C")).toBe(false);
49+
50+
expect(isIn("a", ["a", "b", "c"])).toBe(true);
51+
expect(isIn("b", ["a", "b", "c"])).toBe(true);
52+
expect(isIn("c", ["a", "b", "c"])).toBe(true);
53+
expect(isIn("d", ["a", "b", "c"])).toBe(false);
54+
});
55+
56+
it("should be able to work with spread-string options", async () => {
57+
expect(isIn("A", ...["A,B,B"])).toBe(true);
58+
expect(isIn("A", ...["A", "B", "C"])).toBe(true);
4659
});
4760
});

0 commit comments

Comments
 (0)