@json-layout/vocabulary
Advanced tools
Comparing version 0.17.0 to 0.18.0
{ | ||
"name": "@json-layout/vocabulary", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"description": "Main JSON Layout vocabulary as JSON schemas and Typescript types. Also contains some small utility functions to validate and normalize annotations.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -19,22 +19,38 @@ import { validateLayoutKeyword, isComponentName, isPartialCompObject, isPartialChildren, isPartialSwitch, isPartialGetItemsExpr, isPartialGetItemsObj, isPartialSlotMarkdown, isPartialGetItemsFetch, isPartialChildComposite } from './layout-keyword/index.js' | ||
* @param {import('./types.js').SchemaFragment} schemaFragment | ||
* @param {string} type | ||
* @returns {Children} | ||
*/ | ||
function getDefaultChildren (schemaFragment) { | ||
const { type } = getSchemaFragmentType(schemaFragment) | ||
function getDefaultChildren (schemaFragment, type) { | ||
/** @type {Children} */ | ||
const children = [] | ||
if (type === 'object') { | ||
if (schemaFragment.properties) { | ||
for (const key of Object.keys(schemaFragment.properties)) { | ||
children.push({ key }) | ||
for (const key of Object.keys(schemaFragment)) { | ||
if (key === 'properties') { | ||
for (const key of Object.keys(schemaFragment.properties ?? {})) { | ||
children.push({ key }) | ||
if (schemaFragment.dependencies?.[key] && !Array.isArray(schemaFragment.dependencies[key])) { | ||
children.push({ key: `$deps-${key}` }) | ||
} | ||
if (schemaFragment.dependentSchemas && schemaFragment.dependentSchemas[key]) { | ||
children.push({ key: `$deps-${key}` }) | ||
} | ||
} | ||
} | ||
} | ||
if (schemaFragment.allOf?.length) { | ||
for (let i = 0; i < schemaFragment.allOf.length; i++) { | ||
children.push({ key: `$allOf-${i}` }) | ||
if (key === 'allOf') { | ||
if (schemaFragment.allOf?.length) { | ||
for (let i = 0; i < schemaFragment.allOf.length; i++) { | ||
children.push({ key: `$allOf-${i}` }) | ||
} | ||
} | ||
} | ||
if (key === 'oneOf') { | ||
children.push({ key: '$oneOf' }) | ||
} | ||
if (key === 'then' && schemaFragment.if) { | ||
children.push({ key: '$then' }) | ||
} | ||
if (key === 'else' && schemaFragment.if) { | ||
children.push({ key: '$else' }) | ||
} | ||
} | ||
if (schemaFragment.oneOf) { | ||
children.push({ key: '$oneOf' }) | ||
} | ||
} | ||
@@ -88,7 +104,7 @@ if (type === 'array' && Array.isArray(schemaFragment.items)) { | ||
* @param {SchemaFragment} schemaFragment | ||
* @param {string | undefined} type | ||
* @param {'oneOf'} [arrayChild] | ||
* @returns {import('./index.js').ComponentName} | ||
*/ | ||
function getDefaultComp (partial, schemaFragment, arrayChild) { | ||
const { type } = getSchemaFragmentType(schemaFragment) | ||
function getDefaultComp (partial, schemaFragment, type, arrayChild) { | ||
const hasSimpleType = type && ['string', 'integer', 'number'].includes(type) | ||
@@ -237,2 +253,4 @@ if (arrayChild === 'oneOf') return 'one-of-select' | ||
* @param {SchemaFragment} schemaFragment | ||
* @param {string | undefined} type | ||
* @param {boolean} nullable | ||
* @param {string} schemaPath | ||
@@ -245,7 +263,5 @@ * @param {Record<string, import('./types.js').ComponentInfo>} components | ||
*/ | ||
function getCompObject (layoutKeyword, schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
function getCompObject (layoutKeyword, schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
const key = schemaPath.slice(schemaPath.lastIndexOf('/') + 1) | ||
const { type, nullable } = getSchemaFragmentType(schemaFragment) | ||
if ('const' in schemaFragment) return { comp: 'none' } | ||
@@ -262,3 +278,3 @@ if (!type) return { comp: 'none' } | ||
if (!partial.comp) { | ||
partial.comp = getDefaultComp(partial, schemaFragment, arrayChild) | ||
partial.comp = getDefaultComp(partial, schemaFragment, type, arrayChild) | ||
} | ||
@@ -275,3 +291,3 @@ const component = components[partial.comp] | ||
if (!('title' in partial)) partial.title = schemaFragment.title ?? null | ||
partial.children = getChildren(getDefaultChildren(schemaFragment), partial.children) | ||
partial.children = getChildren(getDefaultChildren(schemaFragment, type), partial.children) | ||
} else if (partial.comp === 'list') { | ||
@@ -423,2 +439,4 @@ if (!('title' in partial)) partial.title = schemaFragment.title ?? key | ||
* @param {SchemaFragment} schemaFragment | ||
* @param {string | undefined} type | ||
* @param {boolean} nullable | ||
* @param {string} schemaPath | ||
@@ -431,3 +449,3 @@ * @param {Record<string, import('./types.js').ComponentInfo>} components | ||
*/ | ||
function getNormalizedLayout (layoutKeyword, schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
function getNormalizedLayout (layoutKeyword, schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
if (isPartialSwitch(layoutKeyword)) { | ||
@@ -442,3 +460,3 @@ /** @type {BaseCompObject[]} */ | ||
const switchCase = switchCases[i] | ||
const compObjectResult = getCompObject(switchCase, schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
const compObjectResult = getCompObject(switchCase, schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
normalizedSwitchCases.push(compObjectResult) | ||
@@ -448,3 +466,3 @@ } | ||
} else { | ||
return getCompObject(layoutKeyword, schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
return getCompObject(layoutKeyword, schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
} | ||
@@ -492,2 +510,4 @@ } | ||
* @param {SchemaFragment} schemaFragment | ||
* @param {string | undefined} type | ||
* @param {boolean} nullable | ||
* @param {string} schemaPath | ||
@@ -500,3 +520,3 @@ * @param {Record<string, import('./types.js').ComponentInfo>} components | ||
*/ | ||
function normalizeValidLayoutFragment (schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
function normalizeValidLayoutFragment (schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) { | ||
optionsKeys = optionsKeys ? optionsKeys.concat(defaultOptionsKeys) : defaultOptionsKeys | ||
@@ -514,3 +534,3 @@ let layoutKeyword | ||
} | ||
const normalizedLayout = getNormalizedLayout(layoutKeyword, schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
const normalizedLayout = getNormalizedLayout(layoutKeyword, schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
@@ -532,9 +552,12 @@ if (!validateNormalizedLayout(normalizedLayout)) { | ||
* @param {'oneOf'} [arrayChild] | ||
* @param {string | undefined} [knownType] | ||
* @param {boolean} [knownNullable] | ||
* @returns {{layout: NormalizedLayout, errors: string[]}} | ||
*/ | ||
export function normalizeLayoutFragment (schemaFragment, schemaPath, components, markdown = (src) => src, optionsKeys, arrayChild) { | ||
export function normalizeLayoutFragment (schemaFragment, schemaPath, components, markdown = (src) => src, optionsKeys, arrayChild, knownType, knownNullable) { | ||
const { type, nullable } = knownType ? { type: knownType, nullable: knownNullable ?? false } : getSchemaFragmentType(schemaFragment) | ||
/** @type {string[]} */ | ||
const errors = [] | ||
try { | ||
const layout = normalizeValidLayoutFragment(schemaFragment, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
const layout = normalizeValidLayoutFragment(schemaFragment, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
return { layout, errors } | ||
@@ -546,3 +569,3 @@ } catch (/** @type {any} */err) { | ||
errors.push('failed to normalize layout, use default component') | ||
const layout = normalizeValidLayoutFragment({ ...schemaFragment, layout: {} }, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
const layout = normalizeValidLayoutFragment({ ...schemaFragment, layout: {} }, type, nullable, schemaPath, components, markdown, optionsKeys, arrayChild) | ||
return { layout, errors } | ||
@@ -549,0 +572,0 @@ } catch (/** @type {any} */err) { |
@@ -22,2 +22,7 @@ import { type LayoutKeyword } from './layout-keyword/types.js' | ||
readOnly?: boolean | ||
if?: any | ||
then?: any | ||
else?: any | ||
dependencies?: Record<string, any> | ||
dependentSchemas?: Record<string, any> | ||
} | ||
@@ -24,0 +29,0 @@ |
@@ -8,5 +8,7 @@ /** | ||
* @param {'oneOf'} [arrayChild] | ||
* @param {string | undefined} [knownType] | ||
* @param {boolean} [knownNullable] | ||
* @returns {{layout: NormalizedLayout, errors: string[]}} | ||
*/ | ||
export function normalizeLayoutFragment(schemaFragment: SchemaFragment, schemaPath: string, components: Record<string, import('./types.js').ComponentInfo>, markdown?: (text: string) => string, optionsKeys?: string[] | undefined, arrayChild?: "oneOf" | undefined): { | ||
export function normalizeLayoutFragment(schemaFragment: SchemaFragment, schemaPath: string, components: Record<string, import('./types.js').ComponentInfo>, markdown?: (text: string) => string, optionsKeys?: string[] | undefined, arrayChild?: "oneOf" | undefined, knownType?: string | undefined, knownNullable?: boolean | undefined): { | ||
layout: NormalizedLayout; | ||
@@ -13,0 +15,0 @@ errors: string[]; |
@@ -21,2 +21,7 @@ import { type LayoutKeyword } from './layout-keyword/types.js'; | ||
readOnly?: boolean; | ||
if?: any; | ||
then?: any; | ||
else?: any; | ||
dependencies?: Record<string, any>; | ||
dependentSchemas?: Record<string, any>; | ||
} | ||
@@ -23,0 +28,0 @@ export interface ComponentInfo { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
190153
5472