Skip to content

Commit 17ed8ff

Browse files
Damian Osipiukedi9999
authored andcommitted
Fixed multiplicative errors on schema dependencies (rjsf-team#884)
1 parent 0baafa6 commit 17ed8ff

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

src/components/Form.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ export default class Form extends Component {
7070
return shouldRender(this, nextProps, nextState);
7171
}
7272

73-
validate(formData, schema) {
73+
validate(formData, schema = this.props.schema) {
7474
const { validate, transformErrors } = this.props;
75+
const { definitions } = this.getRegistry();
76+
const resolvedSchema = retrieveSchema(schema, definitions, formData);
7577
return validateFormData(
7678
formData,
77-
schema || this.props.schema,
79+
resolvedSchema,
7880
validate,
7981
transformErrors
8082
);

test/Form_test.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,103 @@ describe("Form", () => {
12291229
expect(errors).eql(["should NOT be shorter than 4 characters"]);
12301230
});
12311231
});
1232+
1233+
describe("schema dependencies", () => {
1234+
const schema = {
1235+
type: "object",
1236+
properties: {
1237+
branch: {
1238+
type: "number",
1239+
enum: [1, 2, 3],
1240+
default: 1,
1241+
},
1242+
},
1243+
required: ["branch"],
1244+
dependencies: {
1245+
branch: {
1246+
oneOf: [
1247+
{
1248+
properties: {
1249+
branch: {
1250+
enum: [1],
1251+
},
1252+
field1: {
1253+
type: "number",
1254+
},
1255+
},
1256+
required: ["field1"],
1257+
},
1258+
{
1259+
properties: {
1260+
branch: {
1261+
enum: [2],
1262+
},
1263+
field1: {
1264+
type: "number",
1265+
},
1266+
field2: {
1267+
type: "number",
1268+
},
1269+
},
1270+
required: ["field1", "field2"],
1271+
},
1272+
],
1273+
},
1274+
},
1275+
};
1276+
1277+
it("should only show error for property in selected branch", () => {
1278+
const { comp, node } = createFormComponent({
1279+
schema,
1280+
liveValidate: true,
1281+
});
1282+
1283+
Simulate.change(node.querySelector("input[type=text]"), {
1284+
target: { value: "not a number" },
1285+
});
1286+
1287+
expect(comp.state.errorSchema).eql({
1288+
field1: {
1289+
__errors: ["should be number"],
1290+
},
1291+
});
1292+
});
1293+
1294+
it("should only show errors for properties in selected branch", () => {
1295+
const { comp, node } = createFormComponent({
1296+
schema,
1297+
liveValidate: true,
1298+
formData: { branch: 2 },
1299+
});
1300+
1301+
Simulate.change(node.querySelector("input[type=text]"), {
1302+
target: { value: "not a number" },
1303+
});
1304+
1305+
expect(comp.state.errorSchema).eql({
1306+
field1: {
1307+
__errors: ["should be number"],
1308+
},
1309+
field2: {
1310+
__errors: ["is a required property"],
1311+
},
1312+
});
1313+
});
1314+
1315+
it("should not show any errors when branch is empty", () => {
1316+
const { comp, node } = createFormComponent({
1317+
schema,
1318+
liveValidate: true,
1319+
formData: { branch: 3 },
1320+
});
1321+
1322+
Simulate.change(node.querySelector("select"), {
1323+
target: { value: 3 },
1324+
});
1325+
1326+
expect(comp.state.errorSchema).eql({});
1327+
});
1328+
});
12321329
});
12331330

12341331
describe("Schema and formData updates", () => {

0 commit comments

Comments
 (0)