Skip to content

Commit ff3c565

Browse files
committed
Fixed #68
1 parent 60ef76a commit ff3c565

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/ruleManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const DEFINED_RULES: Record<string, RuleFunction> = {
77
};
88

99
export const isRegistered = (name: string) => {
10-
return DEFINED_RULES.includes(name);
10+
return Object.keys(DEFINED_RULES).includes(name);
1111
};
1212

1313
export const register = (

tests/ruleManager.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { beforeAll, describe, test, expect } from "vitest";
2+
import {
3+
setLocales,
4+
en,
5+
ILocale,
6+
isRegistered,
7+
register,
8+
validate,
9+
} from "../index";
10+
11+
describe("isRegistered()", () => {
12+
beforeAll(async () => {
13+
setLocales(en as ILocale);
14+
});
15+
16+
test("should check the unregistered rules", async () => {
17+
const result = isRegistered("myRule");
18+
expect(result).toBe(false);
19+
});
20+
21+
test("should check the registered rules", async () => {
22+
register(
23+
"myRule",
24+
() => {
25+
return false;
26+
},
27+
{
28+
en: "My rule throws an error!",
29+
}
30+
);
31+
const result = isRegistered("myRule");
32+
expect(result).toBe(true);
33+
});
34+
35+
test("should be able use custom rule", async () => {
36+
const data = {
37+
38+
};
39+
const rules = {
40+
email: "myRule",
41+
};
42+
43+
const result = await validate(data, rules);
44+
expect(result.isValid).toBe(false);
45+
expect(result.errors.email[0].rule).toBe("myRule");
46+
expect(result.errors.email[0].message).toBe("My rule throws an error!");
47+
});
48+
});

0 commit comments

Comments
 (0)