Skip to content

Commit cd0387d

Browse files
committed
fix: add color map for json schema types
1 parent ca9ce9d commit cd0387d

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed
16.1 KB
Binary file not shown.

src/JSONSchema/SchemaRenderer.tsx

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { JSONSchema4 } from "json-schema";
33
import { TableRow, TableCell, Typography } from "@material-ui/core";
44
import JSONSchemaFields from "./fields/JSONSchemaFields";
55
import _ from "lodash";
6+
import { grey, green, purple, yellow, blue } from "@material-ui/core/colors";
67

78
interface IProps {
89
schema: JSONSchema4;
@@ -12,38 +13,11 @@ interface IProps {
1213

1314
const styles = {
1415
cellWidth: {
15-
margin: "5px",
16-
padding: "5px",
1716
width: "70px",
1817
},
1918
};
2019

2120
const SchemaRenderer: React.FC<IProps> = ({ schema, required, name }) => {
22-
if (schema.type === "object" || schema.properties) {
23-
return (
24-
<TableRow>
25-
<TableCell colSpan={1} style={styles.cellWidth}>
26-
{schema.title || name}
27-
</TableCell>
28-
<TableCell colSpan={1} style={styles.cellWidth}>
29-
<Typography variant="body1" color="primary">object</Typography>
30-
</TableCell>
31-
<TableCell colSpan={5}>
32-
{schema.properties && Object.entries(schema.properties).map(([n, prop]: [string, JSONSchema4], i: number) => {
33-
return (
34-
<JSONSchemaFields
35-
key={n}
36-
schema={prop}
37-
name={n}
38-
hideHeader={i !== 0}
39-
required={schema.required && schema.required.includes(n)}
40-
/>
41-
);
42-
})}
43-
</TableCell>
44-
</TableRow>
45-
);
46-
}
4721
if (schema.anyOf) {
4822
return (
4923
<TableRow>
@@ -89,7 +63,7 @@ const SchemaRenderer: React.FC<IProps> = ({ schema, required, name }) => {
8963
</TableRow>
9064
);
9165
}
92-
if (schema.type === "array" && schema.items instanceof Array) {
66+
if (schema.items instanceof Array) {
9367
return (
9468
<TableRow>
9569
<TableCell colSpan={1} style={styles.cellWidth}>
@@ -104,7 +78,7 @@ const SchemaRenderer: React.FC<IProps> = ({ schema, required, name }) => {
10478
</TableRow>
10579
);
10680
}
107-
if (schema.type === "array" && schema.items) {
81+
if (schema.items) {
10882
return (
10983
<TableRow>
11084
<TableCell colSpan={1} style={styles.cellWidth}>
@@ -119,14 +93,54 @@ const SchemaRenderer: React.FC<IProps> = ({ schema, required, name }) => {
11993
</TableRow>
12094
);
12195
}
96+
97+
if (schema.properties) {
98+
return (
99+
<TableRow>
100+
<TableCell colSpan={1} style={styles.cellWidth}>
101+
{schema.title || name}
102+
</TableCell>
103+
<TableCell colSpan={1} style={styles.cellWidth}>
104+
<Typography variant="body1" color="primary">object</Typography>
105+
</TableCell>
106+
<TableCell colSpan={5}>
107+
{schema.properties && Object.entries(schema.properties).map(([n, prop]: [string, JSONSchema4], i: number) => {
108+
return (
109+
<JSONSchemaFields
110+
key={n}
111+
schema={prop}
112+
name={n}
113+
hideHeader={i !== 0}
114+
required={schema.required && schema.required.includes(n)}
115+
/>
116+
);
117+
})}
118+
</TableCell>
119+
</TableRow>
120+
);
121+
}
122+
123+
const colorMap: {[k: string]: string} = {
124+
any: grey[500],
125+
array: blue[300],
126+
boolean: blue[500],
127+
integer: purple[800],
128+
null: yellow[900],
129+
number: purple[500],
130+
string: green[500],
131+
undefined: grey[500],
132+
};
122133
return (
123134
<TableRow key={schema.title}>
124135
<TableCell component="th" scope="row" style={styles.cellWidth}>
125136
{schema.title || name}
126137
</TableCell>
127-
<TableCell style={styles.cellWidth}>{schema.type}</TableCell>
138+
<TableCell style={{
139+
...styles.cellWidth,
140+
color: colorMap[schema.type as any],
141+
}}>{schema.type}</TableCell>
128142
<TableCell style={styles.cellWidth}>{schema.pattern}</TableCell>
129-
<TableCell style={styles.cellWidth}>{required ? "required" : "optional"}</TableCell>
143+
<TableCell style={styles.cellWidth}>{required ? "yes" : ""}</TableCell>
130144
<TableCell style={styles.cellWidth}>{schema.description}</TableCell>
131145
</TableRow>
132146
);

0 commit comments

Comments
 (0)