|
| 1 | +import React from "react"; |
| 2 | +import { JSONSchema4 } from "json-schema"; |
| 3 | +import { TableRow, TableCell, Typography } from "@material-ui/core"; |
| 4 | +import JSONSchemaFields from "./fields/JSONSchemaFields"; |
| 5 | +import _ from "lodash"; |
| 6 | + |
| 7 | +interface IProps { |
| 8 | + schema: JSONSchema4; |
| 9 | + required?: boolean; |
| 10 | + name?: string; |
| 11 | +} |
| 12 | + |
| 13 | +const styles = { |
| 14 | + cellWidth: { |
| 15 | + margin: "5px", |
| 16 | + padding: "5px", |
| 17 | + width: "70px", |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +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 | + } |
| 47 | + if (schema.anyOf) { |
| 48 | + return ( |
| 49 | + <TableRow> |
| 50 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 51 | + {schema.title || name} |
| 52 | + </TableCell> |
| 53 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 54 | + <Typography variant="body1" color="primary">any of</Typography> |
| 55 | + </TableCell> |
| 56 | + <TableCell colSpan={5}> |
| 57 | + {schema.anyOf.map((p, i) => <JSONSchemaFields schema={p} key={i} />)} |
| 58 | + </TableCell> |
| 59 | + </TableRow> |
| 60 | + ); |
| 61 | + } |
| 62 | + if (schema.allOf) { |
| 63 | + return ( |
| 64 | + <TableRow> |
| 65 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 66 | + {schema.title || name} |
| 67 | + </TableCell> |
| 68 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 69 | + <Typography variant="body1" color="primary">all of</Typography> |
| 70 | + </TableCell> |
| 71 | + <TableCell colSpan={5}> |
| 72 | + {schema.allOf.map((p, i) => <JSONSchemaFields schema={p} key={i} />)} |
| 73 | + </TableCell> |
| 74 | + </TableRow> |
| 75 | + ); |
| 76 | + } |
| 77 | + if (schema.oneOf) { |
| 78 | + return ( |
| 79 | + <TableRow> |
| 80 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 81 | + {schema.title || name} |
| 82 | + </TableCell> |
| 83 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 84 | + <Typography variant="body1" color="primary">one of</Typography> |
| 85 | + </TableCell> |
| 86 | + <TableCell colSpan={5}> |
| 87 | + {schema.oneOf.map((p, i) => <JSONSchemaFields schema={p} key={i} />)} |
| 88 | + </TableCell> |
| 89 | + </TableRow> |
| 90 | + ); |
| 91 | + } |
| 92 | + if (schema.type === "array" && schema.items instanceof Array) { |
| 93 | + return ( |
| 94 | + <TableRow> |
| 95 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 96 | + {schema.title || name} |
| 97 | + </TableCell> |
| 98 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 99 | + <Typography variant="body1" color="primary">array of</Typography> |
| 100 | + </TableCell> |
| 101 | + <TableCell colSpan={5}> |
| 102 | + {schema.items.map((p, i) => <JSONSchemaFields schema={p} key={i} />)} |
| 103 | + </TableCell> |
| 104 | + </TableRow> |
| 105 | + ); |
| 106 | + } |
| 107 | + if (schema.type === "array" && schema.items) { |
| 108 | + return ( |
| 109 | + <TableRow> |
| 110 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 111 | + {schema.title || name} |
| 112 | + </TableCell> |
| 113 | + <TableCell colSpan={1} style={styles.cellWidth}> |
| 114 | + <Typography variant="body1" color="primary">array of</Typography> |
| 115 | + </TableCell> |
| 116 | + <TableCell colSpan={5}> |
| 117 | + <JSONSchemaFields schema={schema.items} /> |
| 118 | + </TableCell> |
| 119 | + </TableRow> |
| 120 | + ); |
| 121 | + } |
| 122 | + return ( |
| 123 | + <TableRow key={schema.title}> |
| 124 | + <TableCell component="th" scope="row" style={styles.cellWidth}> |
| 125 | + {schema.title || name} |
| 126 | + </TableCell> |
| 127 | + <TableCell style={styles.cellWidth}>{schema.type}</TableCell> |
| 128 | + <TableCell style={styles.cellWidth}>{schema.pattern}</TableCell> |
| 129 | + <TableCell style={styles.cellWidth}>{required ? "required" : "optional"}</TableCell> |
| 130 | + <TableCell style={styles.cellWidth}>{schema.description}</TableCell> |
| 131 | + </TableRow> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +export default SchemaRenderer; |
0 commit comments