Skip to content

Commit 634a06e

Browse files
committed
made unwrapErrorHandler more generic & updated test to use a custom validate function
1 parent cbed614 commit 634a06e

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

packages/core/src/validate.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ function unwrapErrorHandler(errorHandler) {
131131
return Object.keys(errorHandler).reduce((acc, key) => {
132132
if (key === "addError") {
133133
return acc;
134-
} else if (key === "__errors") {
134+
} else if (isPlainObject(errorHandler[key])) {
135+
return { ...acc, [key]: unwrapErrorHandler(errorHandler[key]) };
136+
} else {
135137
return { ...acc, [key]: errorHandler[key] };
136138
}
137-
return { ...acc, [key]: unwrapErrorHandler(errorHandler[key]) };
138139
}, {});
139140
}
140141

packages/core/test/Form_test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,20 +3422,31 @@ describe("Form omitExtraData and liveOmit", () => {
34223422
it("should ignore extra array properties passed within the errorSchema", () => {
34233423
const schema = {
34243424
type: "object",
3425+
required: ["foo", "bar"],
34253426
properties: {
3426-
foo: { type: "string" },
3427+
foo: { type: "string", minLength: 10 },
3428+
bar: { type: "string" },
34273429
},
34283430
};
34293431

3430-
const extraErrors = {
3431-
__warnings: ["a warning that should be ignored"],
3432-
foo: {
3433-
__errors: ["foo", "bar"],
3434-
__warnings: ["another warning"],
3435-
},
3432+
const formData = {
3433+
foo: "dummy",
34363434
};
34373435

3438-
const { node } = createFormComponent({ schema, extraErrors });
3436+
const validate = (formData, errors) => {
3437+
if (formData.foo === "dummy") {
3438+
errors.foo.__warnings = ["value might be too common"];
3439+
}
3440+
3441+
return errors;
3442+
};
3443+
3444+
const { node } = createFormComponent({
3445+
schema,
3446+
formData,
3447+
validate,
3448+
liveValidate: true,
3449+
});
34393450

34403451
expect(node.querySelectorAll(".error-detail li")).to.have.length.of(2);
34413452
});

0 commit comments

Comments
 (0)