Skip to content
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Release Notes

## [2.2.1 (2025-02-03)](https://github.com/axe-api/axe-api/compare/2.2.1...2.2.0)

🎉 Big thanks to our contributors for this release!
Special shoutout to: @christoph-kluge 🚀👏

Your help makes this project better—cheers! 🎩✨

- Fixed: Argument parsing issue on different functions [#70](https://github.com/axe-api/validator/issues/70)
- Fixed: Internal server error: DEFINED_RULES.includes is not a function [#68](https://github.com/axe-api/validator/issues/68)

## [2.2.0 (2025-01-13)](https://github.com/axe-api/axe-api/compare/2.2.0...2.2.1)

- Array and object validation [#54](https://github.com/axe-api/validator/issues/54)
Expand Down
9 changes: 5 additions & 4 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"typescript": "^5.2.2",
"vite": "^5.4.6"
"vite": "^5.4.14"
}
}
7 changes: 4 additions & 3 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "robust-validator",
"version": "2.2.0",
"version": "2.2.1",
"description": "Rule-based data validation library",
"type": "module",
"main": "dist/index.cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/ruleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const DEFINED_RULES: Record<string, RuleFunction> = {
};

export const isRegistered = (name: string) => {
return DEFINED_RULES.includes(name);
return Object.keys(DEFINED_RULES).includes(name);
};

export const register = (
Expand Down
14 changes: 12 additions & 2 deletions src/rules/isIn.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down
48 changes: 48 additions & 0 deletions tests/ruleManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeAll, describe, test, expect } from "vitest";
import {
setLocales,
en,
ILocale,
isRegistered,
register,
validate,
} from "../index";

describe("isRegistered()", () => {
beforeAll(async () => {
setLocales(en as ILocale);
});

test("should check the unregistered rules", async () => {
const result = isRegistered("myRule");
expect(result).toBe(false);
});

test("should check the registered rules", async () => {
register(
"myRule",
() => {
return false;
},
{
en: "My rule throws an error!",
}
);
const result = isRegistered("myRule");
expect(result).toBe(true);
});

test("should be able use custom rule", async () => {
const data = {
email: "foo@bar.com",
};
const rules = {
email: "myRule",
};

const result = await validate(data, rules);
expect(result.isValid).toBe(false);
expect(result.errors.email[0].rule).toBe("myRule");
expect(result.errors.email[0].message).toBe("My rule throws an error!");
});
});
13 changes: 13 additions & 0 deletions tests/rules/in.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});