Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(multi): item rule has custom checker will throw error if validate… #290

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions lib/rules/multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function({ schema, messages }, path, context) {
var hasValid = false;
var newVal = value;
var checkErrors = [];
var errorsSize = errors.length;
`);

for (let i = 0; i < schema.rules.length; i++) {
Expand All @@ -20,11 +21,11 @@ module.exports = function({ schema, messages }, path, context) {
const rule = this.getRuleFromSchema(schema.rules[i]);
src.push(this.compileRule(rule, context, path, `var tmpVal = ${context.async ? "await " : ""}context.fn[%%INDEX%%](value, field, parent, _errors, context);`, "tmpVal"));
src.push(`
if (_errors.length == 0) {
if (errors.length == errorsSize && _errors.length == 0) {
hasValid = true;
newVal = tmpVal;
} else {
Array.prototype.push.apply(checkErrors, _errors);
Array.prototype.push.apply(checkErrors, _errors, errors.splice(errorsSize));
}
}
`);
Expand Down
70 changes: 70 additions & 0 deletions test/rules/multi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,74 @@ describe("Test rule: multi", () => {
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"}]);
});
});

describe("should work with custom validator", () => {
const checkerFn = jest.fn(() => {});

const v = new Validator({
useNewCustomCheckerFunction: true,
aliases: {
strOK: {
type: "string",
custom: (value, errors) => {
checkerFn();
if (value !== "OK") {
errors.push({type: "strOK"});
return;
}
return value;
}
},
num99: {
type: "number",
custom: (value, errors) => {
checkerFn();
if (value !== 99) {
errors.push({type: "num99"});
return;
}
return value;
}
}
}
});

const schema = {
a: {
type: "multi",
rules: ["strOK", "num99"]
}
};
const check = v.compile(schema);

it("test strOK", () => {
{
const o = { a: "OK" };
expect(check(o)).toBe(true);
expect(o).toStrictEqual({ a: "OK" });
expect(checkerFn).toBeCalledTimes(1);
}
{
const o = { a: "not-OK" };
expect(check(o)).not.toBe(true);
0x0a0d marked this conversation as resolved.
Show resolved Hide resolved
expect(o).toStrictEqual({ a: "not-OK" });
expect(checkerFn).toBeCalledTimes(3);
}
});

it("test num99", () => {
{
const o = { a: 99 };
expect(check(o)).toBe(true);
expect(o).toStrictEqual({ a: 99 });
expect(checkerFn).toBeCalledTimes(5);
}
{
const o = { a: 1199 };
expect(check(o)).not.toBe(true);
0x0a0d marked this conversation as resolved.
Show resolved Hide resolved
expect(o).toStrictEqual({ a: 1199 });
expect(checkerFn).toBeCalledTimes(7);
}
});
});
});