2021-12-06 13:04:22 +01:00
|
|
|
export const convertJSONSchemaToTableSchema = (
|
|
|
|
jsonSchema,
|
|
|
|
squashObjects = false
|
|
|
|
) => {
|
2021-12-06 12:41:17 +01:00
|
|
|
if (!jsonSchema) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (jsonSchema.schema) {
|
|
|
|
jsonSchema = jsonSchema.schema
|
|
|
|
}
|
2021-12-06 13:04:22 +01:00
|
|
|
const keys = extractJSONSchemaKeys(jsonSchema, squashObjects)
|
2021-12-06 12:41:17 +01:00
|
|
|
let schema = {}
|
|
|
|
keys.forEach(({ key, type }) => {
|
|
|
|
schema[key] = { type, name: key }
|
|
|
|
})
|
|
|
|
return schema
|
|
|
|
}
|
|
|
|
|
|
|
|
const extractJSONSchemaKeys = (jsonSchema, squashObjects = false) => {
|
|
|
|
if (!jsonSchema || !Object.keys(jsonSchema).length) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let keys = []
|
|
|
|
Object.keys(jsonSchema).forEach(key => {
|
|
|
|
const type = jsonSchema[key].type
|
|
|
|
if (type === "json" && squashObjects) {
|
|
|
|
const childKeys = extractJSONSchemaKeys(jsonSchema[key].schema)
|
|
|
|
keys = keys.concat(
|
|
|
|
childKeys.map(childKey => ({
|
|
|
|
key: `${key}.${childKey.key}`,
|
|
|
|
type: childKey.type,
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
} else if (type !== "array") {
|
|
|
|
keys.push({ key, type })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return keys
|
|
|
|
}
|