Skip to content

Commit 76828eb

Browse files
olzraitin1k0
authored andcommitted
Don't pass consumed error to child schema (#381)
1 parent 6551d5e commit 76828eb

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/components/fields/SchemaField.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,23 @@ function SchemaField(props) {
157157
displayLabel = false;
158158
}
159159

160+
const {__errors, ...fieldErrorSchema} = errorSchema;
161+
160162
const field = (
161163
<FieldComponent {...props}
162164
schema={schema}
163165
disabled={disabled}
164166
readonly={readonly}
165167
autofocus={autofocus}
168+
errorSchema={fieldErrorSchema}
166169
formContext={formContext}/>
167170
);
168171

169172
const {type} = schema;
170173
const id = idSchema.$id;
171174
const label = props.schema.title || schema.title || name;
172175
const description = props.schema.description || schema.description;
173-
const errors = errorSchema.__errors;
176+
const errors = __errors;
174177
const help = uiSchema["ui:help"];
175178
const hidden = uiSchema["ui:widget"] === "hidden";
176179
const classNames = [

test/SchemaField_test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import {expect} from "chai";
3+
import {Simulate} from "react-addons-test-utils";
34

45
import SchemaField from "../src/components/fields/SchemaField";
56
import TitleField from "../src/components/fields/TitleField";
@@ -195,4 +196,52 @@ describe("SchemaField", () => {
195196
.to.eql("A Foo field");
196197
});
197198
});
199+
200+
describe("errors", () => {
201+
const schema = {
202+
type: "object",
203+
properties: {
204+
foo: {type: "string"}
205+
}
206+
};
207+
208+
const uiSchema = {
209+
"ui:field": (props) => {
210+
const {uiSchema, ...fieldProps} = props; //eslint-disable-line
211+
return <SchemaField {...fieldProps}/>;
212+
}
213+
};
214+
215+
function validate(formData, errors) {
216+
errors.addError("container");
217+
errors.foo.addError("test");
218+
return errors;
219+
}
220+
221+
function submit(node) {
222+
try {
223+
Simulate.submit(node);
224+
} catch (e) {
225+
// Silencing error thrown as failure is expected here
226+
}
227+
}
228+
229+
it("should render it's own errors", () => {
230+
const {node} = createFormComponent({schema, uiSchema, validate});
231+
submit(node);
232+
233+
const matches = node.querySelectorAll("form > .form-group > div > .error-detail .text-danger");
234+
expect(matches).to.have.length.of(1);
235+
expect(matches[0].textContent).to.eql("container");
236+
});
237+
238+
it("should pass errors to child component", () => {
239+
const {node} = createFormComponent({schema, uiSchema, validate});
240+
submit(node);
241+
242+
const matches = node.querySelectorAll("form .form-group .form-group .text-danger");
243+
expect(matches).to.have.length.of(1);
244+
expect(matches[0].textContent).to.contain("test");
245+
});
246+
});
198247
});

0 commit comments

Comments
 (0)