Skip to content

Commit 3442228

Browse files
committed
add custom validator unit test
1 parent f4328e5 commit 3442228

File tree

3 files changed

+73
-39
lines changed

3 files changed

+73
-39
lines changed

README.md

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,15 @@ let v = new Validator({
802802
});
803803

804804
// Register a custom 'even' validator
805-
v.add("even", value => {
806-
if (value % 2 != 0)
807-
return v.makeError("evenNumber", null, value);
808-
809-
return true;
805+
v.add("even", function({ schema, messages }, path, context) {
806+
return {
807+
source: `
808+
if (value % 2 != 0)
809+
${this.makeError({ type: "evenNumber", actual: "value", messages })}
810+
811+
return value;
812+
`
813+
};
810814
});
811815

812816
const schema = {
@@ -832,37 +836,37 @@ console.log(v.validate({ name: "John", age: 19 }, schema));
832836
Or you can use the `custom` type with an inline checker function:
833837
```js
834838
let v = new Validator({
835-
messages: {
836-
// Register our new error message text
837-
weightMin: "The weight must be greater than {expected}! Actual: {actual}"
838-
}
839+
messages: {
840+
// Register our new error message text
841+
weightMin: "The weight must be greater than {expected}! Actual: {actual}"
842+
}
839843
});
840844

841845
const schema = {
842-
name: { type: "string", min: 3, max: 255 },
843-
weight: {
844-
type: "custom",
845-
minWeight: 10,
846-
check(value, schema) {
847-
return (value < schema.minWeight)
848-
? this.makeError("weightMin", schema.minWeight, value)
849-
: true;
850-
}
851-
}
846+
name: { type: "string", min: 3, max: 255 },
847+
weight: {
848+
type: "custom",
849+
minWeight: 10,
850+
check(value, schema) {
851+
return (value < schema.minWeight)
852+
? this.makeError("weightMin", schema.minWeight, value)
853+
: true;
854+
}
855+
}
852856
};
853857

854858
console.log(v.validate({ name: "John", weight: 50 }, schema));
855859
// Returns: true
856860

857861
console.log(v.validate({ name: "John", weight: 8 }, schema));
858862
/* Returns an array with errors:
859-
[{
860-
type: 'weightMin',
861-
expected: 10,
862-
actual: 8,
863-
field: 'weight',
864-
message: 'The weight must be greater than 10! Actual: 8'
865-
}]
863+
[{
864+
type: 'weightMin',
865+
expected: 10,
866+
actual: 8,
867+
field: 'weight',
868+
message: 'The weight must be greater than 10! Actual: 8'
869+
}]
866870
*/
867871
```
868872

examples/next.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@ let Validator = require("../index");
33
let v = new Validator({
44
debug: true,
55
messages: {
6-
stringMin: "A(z) '{field}' mező túl rövid. Minimum: {expected}, Jelenleg: {actual}"
6+
stringMin: "A(z) '{field}' mező túl rövid. Minimum: {expected}, Jelenleg: {actual}",
7+
evenNumber: "The '{field}' field must be an even number! Actual: {actual}"
78
}
89
});
910

11+
v.add("even", function({ schema, messages }, path, context) {
12+
return {
13+
source: `
14+
if (value % 2 != 0)
15+
${this.makeError({ type: "evenNumber", actual: "value", messages })}
16+
17+
return value;
18+
`
19+
};
20+
});
21+
1022
const schema = {
1123
id: "number|positive|integer|convert",
1224
name: "string|min: 3|max: 255|padStart: 5",
@@ -47,6 +59,7 @@ const schema = {
4759
weightMin: "The '${field}' must be greater than {expected}! Actual: {actual}"
4860
}
4961
}*/
62+
num: { type: "even" }
5063
};
5164

5265
const check = v.compile(schema);
@@ -78,6 +91,7 @@ const obj = {
7891
createdAt: Date.now(),
7992

8093
weight: 10,
94+
num: 2
8195
};
8296
console.log(check(obj), obj);
8397

test/validator.spec.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,57 @@ describe("Test validate", () => {
7878
});
7979

8080
describe("Test add", () => {
81+
let check;
8182

82-
const v = new Validator();
83-
const validFn = jest.fn(() => true);
83+
const v = new Validator({
84+
messages: {
85+
// Register our new error message text
86+
evenNumber: "The '{field}' field must be an even number! Actual: {actual}"
87+
}
88+
});
89+
90+
const validFn = jest.fn(function({ schema, messages }, path, context) {
91+
return {
92+
source: `
93+
if (value % 2 != 0)
94+
${this.makeError({ type: "evenNumber", actual: "value", messages })}
95+
96+
return value;
97+
`
98+
};
99+
});
84100

85101
it("should not contain the new validator", () => {
86-
expect(v.rules.myValidator).toBeUndefined();
102+
expect(v.rules.even).toBeUndefined();
87103
});
88104

89105
it("should contain the new validator", () => {
90-
v.add("myValidator", validFn);
91-
expect(v.rules.myValidator).toBeDefined();
106+
v.add("even", validFn);
107+
expect(v.rules.even).toBeDefined();
92108
});
93109

94110
it("should call the new validator", () => {
95111
const schema = {
96-
a: { type: "myValidator" }
112+
a: { type: "even" }
97113
};
98114

99-
const obj = { a: 5 };
100-
101-
v.validate(obj, schema);
115+
check = v.compile(schema);
102116

103117
const context = {
104118
customs: {},
105119
rules: expect.any(Array),
106120
fn: expect.any(Array),
107-
index: 2,
108-
data: { a: 5 }
121+
index: 2
109122
};
110123

111124
expect(validFn).toHaveBeenCalledTimes(1);
112125
expect(validFn).toHaveBeenCalledWith(expect.any(Object), "a", context);
113126
});
114127

115-
// TODO: add a real example
128+
it("should check the new rule", () => {
129+
expect(check({ a: 5 })).toEqual([{"type": "evenNumber", "field": "a", "actual": 5, "message": "The 'a' field must be an even number! Actual: 5"}]);
130+
expect(check({ a: 6 })).toEqual(true);
131+
});
116132

117133
});
118134

0 commit comments

Comments
 (0)