diff --git a/src/components/Form.js b/src/components/Form.js index e32a7b239c..baef35c9b8 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -70,11 +70,13 @@ export default class Form extends Component { return shouldRender(this, nextProps, nextState); } - validate(formData, schema) { + validate(formData, schema = this.props.schema) { const { validate, transformErrors } = this.props; + const { definitions } = this.getRegistry(); + const resolvedSchema = retrieveSchema(schema, definitions, formData); return validateFormData( formData, - schema || this.props.schema, + resolvedSchema, validate, transformErrors ); diff --git a/test/Form_test.js b/test/Form_test.js index ec26583948..fc960daa4d 100644 --- a/test/Form_test.js +++ b/test/Form_test.js @@ -1229,6 +1229,103 @@ describe("Form", () => { expect(errors).eql(["should NOT be shorter than 4 characters"]); }); }); + + describe("schema dependencies", () => { + const schema = { + type: "object", + properties: { + branch: { + type: "number", + enum: [1, 2, 3], + default: 1, + }, + }, + required: ["branch"], + dependencies: { + branch: { + oneOf: [ + { + properties: { + branch: { + enum: [1], + }, + field1: { + type: "number", + }, + }, + required: ["field1"], + }, + { + properties: { + branch: { + enum: [2], + }, + field1: { + type: "number", + }, + field2: { + type: "number", + }, + }, + required: ["field1", "field2"], + }, + ], + }, + }, + }; + + it("should only show error for property in selected branch", () => { + const { comp, node } = createFormComponent({ + schema, + liveValidate: true, + }); + + Simulate.change(node.querySelector("input[type=text]"), { + target: { value: "not a number" }, + }); + + expect(comp.state.errorSchema).eql({ + field1: { + __errors: ["should be number"], + }, + }); + }); + + it("should only show errors for properties in selected branch", () => { + const { comp, node } = createFormComponent({ + schema, + liveValidate: true, + formData: { branch: 2 }, + }); + + Simulate.change(node.querySelector("input[type=text]"), { + target: { value: "not a number" }, + }); + + expect(comp.state.errorSchema).eql({ + field1: { + __errors: ["should be number"], + }, + field2: { + __errors: ["is a required property"], + }, + }); + }); + + it("should not show any errors when branch is empty", () => { + const { comp, node } = createFormComponent({ + schema, + liveValidate: true, + formData: { branch: 3 }, + }); + + Simulate.change(node.querySelector("select"), { + target: { value: 3 }, + }); + + expect(comp.state.errorSchema).eql({}); + }); + }); }); describe("Schema and formData updates", () => {