Skip to content

Commit a746f93

Browse files
authored
Merge pull request #290 from 0x0a0d/multi_rule_with_custom_checker
fix(multi): item rule has custom checker will throw error if validate…
2 parents 3d41029 + a4a7945 commit a746f93

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

lib/rules/multi.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = function({ schema, messages }, path, context) {
99
var hasValid = false;
1010
var newVal = value;
1111
var checkErrors = [];
12+
var errorsSize = errors.length;
1213
`);
1314

1415
for (let i = 0; i < schema.rules.length; i++) {
@@ -20,11 +21,11 @@ module.exports = function({ schema, messages }, path, context) {
2021
const rule = this.getRuleFromSchema(schema.rules[i]);
2122
src.push(this.compileRule(rule, context, path, `var tmpVal = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field, parent, _errors, context);`, "tmpVal"));
2223
src.push(`
23-
if (_errors.length == 0) {
24+
if (errors.length == errorsSize && _errors.length == 0) {
2425
hasValid = true;
2526
newVal = tmpVal;
2627
} else {
27-
Array.prototype.push.apply(checkErrors, _errors);
28+
Array.prototype.push.apply(checkErrors, [].concat(_errors, errors.splice(errorsSize)));
2829
}
2930
}
3031
`);

test/rules/multi.spec.js

+70
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,74 @@ describe("Test rule: multi", () => {
8888
expect(check({d: 4})).toEqual([{"actual": undefined, "field": "a", "message": "The 'a' field is required.", "type": "required"}, {"actual": undefined, "field": "b", "message": "The 'b' field is required.", "type": "required"}, {"actual": undefined, "field": "c", "message": "The 'c' field is required.", "type": "required"}]);
8989
});
9090
});
91+
92+
describe("should work with custom validator", () => {
93+
const checkerFn = jest.fn(() => {});
94+
95+
const v = new Validator({
96+
useNewCustomCheckerFunction: true,
97+
aliases: {
98+
strOK: {
99+
type: "string",
100+
custom: (value, errors) => {
101+
checkerFn();
102+
if (value !== "OK") {
103+
errors.push({type: "strOK"});
104+
return;
105+
}
106+
return value;
107+
}
108+
},
109+
num99: {
110+
type: "number",
111+
custom: (value, errors) => {
112+
checkerFn();
113+
if (value !== 99) {
114+
errors.push({type: "num99"});
115+
return;
116+
}
117+
return value;
118+
}
119+
}
120+
}
121+
});
122+
123+
const schema = {
124+
a: {
125+
type: "multi",
126+
rules: ["strOK", "num99"]
127+
}
128+
};
129+
const check = v.compile(schema);
130+
131+
it("test strOK", () => {
132+
{
133+
const o = { a: "OK" };
134+
expect(check(o)).toBe(true);
135+
expect(o).toStrictEqual({ a: "OK" });
136+
expect(checkerFn).toBeCalledTimes(1);
137+
}
138+
{
139+
const o = { a: "not-OK" };
140+
expect(check(o)).toStrictEqual([{"field": "a", "message": undefined, "type": "strOK"}, {"actual": "not-OK", "field": "a", "message": "The 'a' field must be a number.", "type": "number"}, {"field": "a", "message": undefined, "type": "num99"}]);
141+
expect(o).toStrictEqual({ a: "not-OK" });
142+
expect(checkerFn).toBeCalledTimes(3);
143+
}
144+
});
145+
146+
it("test num99", () => {
147+
{
148+
const o = { a: 99 };
149+
expect(check(o)).toBe(true);
150+
expect(o).toStrictEqual({ a: 99 });
151+
expect(checkerFn).toBeCalledTimes(5);
152+
}
153+
{
154+
const o = { a: 1199 };
155+
expect(check(o)).toStrictEqual([{"actual": 1199, "field": "a", "message": "The 'a' field must be a string.", "type": "string"}, {"field": "a", "message": undefined, "type": "strOK"}, {"field": "a", "message": undefined, "type": "num99"}]);
156+
expect(o).toStrictEqual({ a: 1199 });
157+
expect(checkerFn).toBeCalledTimes(7);
158+
}
159+
});
160+
});
91161
});

0 commit comments

Comments
 (0)