Skip to content

Commit

Permalink
fix: proper handle arrays in schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Elessar1802 committed Oct 3, 2024
1 parent 9d9ed98 commit 77c64c1
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,30 @@ function hasCorrespondingValueInYaml(json: object, path: string) {
}
}

function getArrayItems(items: Record<'type' | 'properties', unknown>) {
if (items.type === 'object') {
return items.properties
}
return items
}

function _constructTree(key: string, path: string, json: RJSFFormSchema): NodeType {
if (!json) {
// Maybe throw error ?
throw new ViewError('Problem constructing the tree', 'In traversal found an undefined json object')
}

const isArray = json.type === 'array' && json.items && typeof json.items === 'object'
const isArray =
json.type === 'array' &&
json.items &&
typeof json.items === 'object' &&
!Array.isArray(json.items) &&
(json.items as Record<'type', unknown>).type === 'object'
const isObject = json.type === 'object' && json.properties && typeof json.properties === 'object'

if (isArray || isObject) {
// @ts-ignore
const children = Object.entries(isArray ? json.items.properties : json.properties).map(
const children = Object.entries(isArray ? getArrayItems(json.items) : json.properties).map(
([childKey, child]: [string, RJSFFormSchema]) =>
_constructTree.call(this, childKey, isArray ? `${path}/*/${childKey}` : `${path}/${childKey}`, child),
)
Expand Down

0 comments on commit 77c64c1

Please sign in to comment.