Skip to content
Closed
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
8 changes: 7 additions & 1 deletion src/rules/isIn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default (value: any, options: any[] | string): boolean => {
export default (value: any, ...options: any): boolean => {
// support calls by "validate()" which uses spreading to pass all options.
// This behavior is different compared to standalone usage of this function.
if (options.length === 1) {
options = options[0]
}

const list: any[] = Array.isArray(options) ? options : options.split(",");
const listAsString = list.map((item) => String(item).trim());

Expand Down
10 changes: 10 additions & 0 deletions tests/rules/in.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe("isIn() ", () => {
expect(isIn(["a", "b"], [])).toBe(false);
});

it("should return false for empty options", () => {
expect(isIn("a")).toBe(false);
expect(isIn(["a", "b"])).toBe(false);
});

it("should handle case sensitivity correctly", () => {
expect(isIn("A", ["a", "b", "c"])).toBe(false);
expect(isIn("A", ["A", "B", "C"])).toBe(true);
Expand All @@ -44,4 +49,9 @@ describe("isIn() ", () => {
it("should be able to parse string values", async () => {
expect(isIn("A", "A,B,B")).toBe(true);
});

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);
});
});