Skip to content

Commit

Permalink
Better json schema title
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Oct 11, 2023
1 parent f1d1a3b commit 1d3e7ed
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/util/zod-jsonschema-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import {jsonSchemaWalkNodes} from './jsonschema-nodewalker'
export function defaultTitleAsJsonPath<T = unknown>(jsonSchema: T) {
jsonSchemaWalkNodes(jsonSchema, (node, meta) => {
// Skip if we already have one..
if (node.title) {
return
}

// TODO: We can also handle json metadata here as desired
const title = [...(meta?.path ?? []), meta.name]
const jsonPath = [...(meta?.path ?? []), meta.name]
.filter((n) => !!n) // Filter out nesting from things like anyOf
.join('.')
if (title && !title.endsWith('.')) {
node.title = title

if (node.title) {
// @see https://share.cleanshot.com/16sDgL6D
node.title = `${jsonPath}: ${node.title}`
} else if (jsonPath && !jsonPath.endsWith('.')) {
node.title = jsonPath
}
})
return jsonSchema
Expand Down

0 comments on commit 1d3e7ed

Please sign in to comment.