Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/JSONSchema/JSONSchema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,43 @@ it("renders empty with empty schema", () => {
expect(div.innerHTML).toBe("");
ReactDOM.unmountComponentAtNode(div);
});

it("renders oneOf schema", () => {
const div = document.createElement("div");
const s = {
oneOf: [
{
type: "string",
},
{
type: "number",
},
],
} as JSONSchema4;
ReactDOM.render(<JSONSchema schema={s}/>, div);
expect(div.innerHTML.includes("string")).toBe(true);
expect(div.innerHTML.includes("number")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});

it("renders with a nested schema object", () => {
const div = document.createElement("div");
const schema = {
properties: {
name: {
properties: {
foo: {
type: "string",
},
},
type: "object",
},
},
type: "object",
} as JSONSchema4;
ReactDOM.render(<JSONSchema schema={schema}/>, div);
expect(div.innerHTML.includes("foo")).toBe(true);
expect(div.innerHTML.includes("string")).toBe(true);
expect(div.innerHTML.includes("object")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});
10 changes: 5 additions & 5 deletions src/JSONSchema/JSONSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class JSONSchema extends Component<IProps> {
const { schema } = this.props;
if (!schema) { return null; }
if (_.isEmpty(schema)) { return null; }
if (schema && schema.type && !schema.properties && schema.oneOf) {
if (schema && !schema.properties && schema.oneOf) {
return (
<>
{schema.oneOf &&
<>
<Typography variant="body1">One Of</Typography>}
<Typography variant="body1">one of</Typography>
{schema.oneOf.map((item) => {
return (
<PrimitiveField schema={item} />
<JSONSchema schema={item} />
);
})}
</>
Expand All @@ -35,11 +35,11 @@ class JSONSchema extends Component<IProps> {
arrayWithItems = _.isArray(arrayWithItems) ? arrayWithItems : [arrayWithItems];
return (
<>
<Typography variant="body1">Array Of</Typography>
<Typography variant="body1">array of</Typography>
<JSONSchemaFields schema={schema} />
{arrayWithItems.map((item: JSONSchema4) => {
return (
<PrimitiveField schema={item} />
<JSONSchema schema={item} />
);
})}
</>
Expand Down
22 changes: 22 additions & 0 deletions src/JSONSchema/fields/JSONSchemaFields.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,25 @@ it("renders with a schema without required", () => {

ReactDOM.unmountComponentAtNode(div);
});

it("renders with a nested schema object", () => {
const div = document.createElement("div");
const schema = {
properties: {
name: {
type: "object",
properties: {
foo: {
type: "string"
}
}
},
},
} as JSONSchema4;
ReactDOM.render(<JSONSchemaFields schema={schema}/>, div);
console.log('div', div.innerHTML);
expect(div.innerHTML.includes("foo")).toBe(true);
expect(div.innerHTML.includes("string")).toBe(true);
expect(div.innerHTML.includes("object")).toBe(true);
ReactDOM.unmountComponentAtNode(div);
});
19 changes: 18 additions & 1 deletion src/JSONSchema/fields/JSONSchemaFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ class JSONSchemaFields extends Component<IProps> {
</TableHead>
<TableBody>
{schema.properties && _.map(schema.properties, (prop, name) => {
// support nested objects
if (prop.type === "object") {
return (
<TableRow>
<TableCell colSpan={1}>
{name}
</TableCell>
<TableCell colSpan={1}>
<Typography variant="body1" color="primary">object</Typography>
</TableCell>
<TableCell colSpan={5}>
<WrappedJSONSchemaFields schema={prop}/>
</TableCell>
</TableRow>
);
}
if (prop.oneOf) {
return (
<TableRow>
Expand Down Expand Up @@ -77,5 +93,6 @@ class JSONSchemaFields extends Component<IProps> {
);
}
}
const WrappedJSONSchemaFields = withStyles(styles)(JSONSchemaFields);

export default withStyles(styles)(JSONSchemaFields);
export default WrappedJSONSchemaFields;