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