Skip to content

Commit 9b92f17

Browse files
committed
Better render config error in the form itself than throwing.
1 parent 6563fd2 commit 9b92f17

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

playground/app.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ class Editor extends Component {
4949
render() {
5050
const {title} = this.props;
5151
const icon = this.state.valid ? "ok" : "remove";
52+
const cls = this.state.valid ? "valid" : "invalid";
5253
return (
5354
<fieldset>
5455
<legend>
55-
<span className={`glyphicon glyphicon-${icon}`} />
56+
<span className={`${cls} glyphicon glyphicon-${icon}`} />
5657
{" "}
5758
{title}
5859
</legend>
@@ -99,7 +100,12 @@ class Selector extends Component {
99100
class App extends Component {
100101
constructor(props) {
101102
super(props);
102-
this.state = {form: false, schema: {}, uiSchema: {}, formData: {}};
103+
this.state = {
104+
form: false,
105+
schema: {},
106+
uiSchema: {},
107+
formData: {},
108+
};
103109
}
104110

105111
componentDidMount() {

playground/styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@
1919
.rjsf input[type=radio] {
2020
margin-right: .4em;
2121
}
22+
23+
.invalid {
24+
color: red;
25+
}
26+
27+
.valid {
28+
color: green;
29+
}

src/components/fields/ObjectField.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@ class ObjectField extends Component {
4040
const {schema, uiSchema, name} = this.props;
4141
const title = name || schema.title;
4242
const SchemaField = this.props.SchemaField;
43-
const orderedProperties = orderProperties(
44-
Object.keys(schema.properties), uiSchema.order);
43+
try {
44+
var orderedProperties = orderProperties(
45+
Object.keys(schema.properties), uiSchema.order);
46+
} catch(err) {
47+
const {message} = err;
48+
return (
49+
<p className="config-error" style={{color: "red"}}>
50+
Invalid {name || "root"} object field configuration: <em>{message}</em>.
51+
</p>
52+
);
53+
}
4554
return (
4655
<fieldset>
4756
{title ? <legend>{title}</legend> : null}

test/index_test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,21 @@ describe("Form", () => {
8181
});
8282

8383
it("should throw when order list length mismatches", () => {
84-
expect(() => {
85-
createComponent({schema, uiSchema: {
86-
order: ["bar", "foo", "baz?"]
87-
}});
88-
}).to.Throw(Error, /should match object properties length/);
84+
const {node} = createComponent({schema, uiSchema: {
85+
order: ["bar", "foo", "baz?"]
86+
}});
87+
88+
expect(node.querySelector(".config-error").textContent)
89+
.to.match(/should match object properties length/);
8990
});
9091

9192
it("should throw when order and properties lists differs", () => {
92-
expect(() => {
93-
createComponent({schema, uiSchema: {
94-
order: ["bar", "wut?"]
95-
}});
96-
}).to.Throw(Error, /does not match object properties list/);
93+
const {node} = createComponent({schema, uiSchema: {
94+
order: ["bar", "wut?"]
95+
}});
96+
97+
expect(node.querySelector(".config-error").textContent)
98+
.to.match(/does not match object properties list/);
9799
});
98100
});
99101

0 commit comments

Comments
 (0)