Skip to content

Commit a1e018f

Browse files
committed
fix(JSON Schema): nested items/objects and oneOf
fixes #71 fixes #72 fixes #73
1 parent 09f7b7d commit a1e018f

File tree

5 files changed

+96
-36
lines changed

5 files changed

+96
-36
lines changed

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/JSONSchema/JSONSchema.test.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,43 @@ it("renders empty with empty schema", () => {
1616
expect(div.innerHTML).toBe("");
1717
ReactDOM.unmountComponentAtNode(div);
1818
});
19+
20+
it("renders oneOf schema", () => {
21+
const div = document.createElement("div");
22+
const s = {
23+
oneOf: [
24+
{
25+
type: "string",
26+
},
27+
{
28+
type: "number",
29+
},
30+
],
31+
} as JSONSchema4;
32+
ReactDOM.render(<JSONSchema schema={s}/>, div);
33+
expect(div.innerHTML.includes("string")).toBe(true);
34+
expect(div.innerHTML.includes("number")).toBe(true);
35+
ReactDOM.unmountComponentAtNode(div);
36+
});
37+
38+
it("renders with a nested schema object", () => {
39+
const div = document.createElement("div");
40+
const schema = {
41+
properties: {
42+
name: {
43+
properties: {
44+
foo: {
45+
type: "string",
46+
},
47+
},
48+
type: "object",
49+
},
50+
},
51+
type: "object",
52+
} as JSONSchema4;
53+
ReactDOM.render(<JSONSchema schema={schema}/>, div);
54+
expect(div.innerHTML.includes("foo")).toBe(true);
55+
expect(div.innerHTML.includes("string")).toBe(true);
56+
expect(div.innerHTML.includes("object")).toBe(true);
57+
ReactDOM.unmountComponentAtNode(div);
58+
});

src/JSONSchema/JSONSchema.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class JSONSchema extends Component<IProps> {
1414
const { schema } = this.props;
1515
if (!schema) { return null; }
1616
if (_.isEmpty(schema)) { return null; }
17-
if (schema && schema.type && !schema.properties && schema.oneOf) {
17+
if (schema && !schema.properties && schema.oneOf) {
1818
return (
1919
<>
2020
{schema.oneOf &&
2121
<>
22-
<Typography variant="body1">One Of</Typography>}
22+
<Typography variant="body1">one of</Typography>
2323
{schema.oneOf.map((item) => {
2424
return (
25-
<PrimitiveField schema={item} />
25+
<JSONSchema schema={item} />
2626
);
2727
})}
2828
</>
@@ -35,11 +35,11 @@ class JSONSchema extends Component<IProps> {
3535
arrayWithItems = _.isArray(arrayWithItems) ? arrayWithItems : [arrayWithItems];
3636
return (
3737
<>
38-
<Typography variant="body1">Array Of</Typography>
38+
<Typography variant="body1">array of</Typography>
3939
<JSONSchemaFields schema={schema} />
4040
{arrayWithItems.map((item: JSONSchema4) => {
4141
return (
42-
<PrimitiveField schema={item} />
42+
<JSONSchema schema={item} />
4343
);
4444
})}
4545
</>

src/JSONSchema/fields/JSONSchemaFields.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,25 @@ it("renders with a schema without required", () => {
7575

7676
ReactDOM.unmountComponentAtNode(div);
7777
});
78+
79+
it("renders with a nested schema object", () => {
80+
const div = document.createElement("div");
81+
const schema = {
82+
properties: {
83+
name: {
84+
type: "object",
85+
properties: {
86+
foo: {
87+
type: "string"
88+
}
89+
}
90+
},
91+
},
92+
} as JSONSchema4;
93+
ReactDOM.render(<JSONSchemaFields schema={schema}/>, div);
94+
console.log('div', div.innerHTML);
95+
expect(div.innerHTML.includes("foo")).toBe(true);
96+
expect(div.innerHTML.includes("string")).toBe(true);
97+
expect(div.innerHTML.includes("object")).toBe(true);
98+
ReactDOM.unmountComponentAtNode(div);
99+
});

src/JSONSchema/fields/JSONSchemaFields.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ class JSONSchemaFields extends Component<IProps> {
4141
</TableHead>
4242
<TableBody>
4343
{schema.properties && _.map(schema.properties, (prop, name) => {
44+
// support nested objects
45+
if (prop.type === "object") {
46+
return (
47+
<TableRow>
48+
<TableCell colSpan={1}>
49+
{name}
50+
</TableCell>
51+
<TableCell colSpan={1}>
52+
<Typography variant="body1" color="primary">object</Typography>
53+
</TableCell>
54+
<TableCell colSpan={5}>
55+
<WrappedJSONSchemaFields schema={prop}/>
56+
</TableCell>
57+
</TableRow>
58+
);
59+
}
4460
if (prop.oneOf) {
4561
return (
4662
<TableRow>
@@ -77,5 +93,6 @@ class JSONSchemaFields extends Component<IProps> {
7793
);
7894
}
7995
}
96+
const WrappedJSONSchemaFields = withStyles(styles)(JSONSchemaFields);
8097

81-
export default withStyles(styles)(JSONSchemaFields);
98+
export default WrappedJSONSchemaFields;

0 commit comments

Comments
 (0)