Skip to content

Commit

Permalink
Fixed #68
Browse files Browse the repository at this point in the history
  • Loading branch information
ozziest committed Feb 3, 2025
1 parent 60ef76a commit ff3c565
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
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: "[email protected]",
};
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!");
});
});

0 comments on commit ff3c565

Please sign in to comment.