@json-layout/vocabulary 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@json-layout/vocabulary",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Main JSON Layout vocabulary as JSON schemas and Typescript types. Also contains some small utility functions to validate and normalize annotations.",
5
5
  "type": "module",
6
6
  "exports": {
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /** @typedef {import("./types.js").SchemaFragment} SchemaFragment */
2
2
  export * from './layout-keyword/index.js'
3
3
  export * from './normalized-layout/index.js'
4
- export { normalizeLayoutFragment } from './normalize.js'
4
+ export { normalizeLayoutFragment, getSchemaFragmentType } from './normalize.js'
package/src/normalize.js CHANGED
@@ -18,7 +18,7 @@ import { validateNormalizedLayout, compositeCompNames } from './normalized-layou
18
18
  * @returns {Children}
19
19
  */
20
20
  function getDefaultChildren (schemaFragment) {
21
- const { type } = getType(schemaFragment)
21
+ const { type } = getSchemaFragmentType(schemaFragment)
22
22
  /** @type {Children} */
23
23
  const children = []
24
24
  if (type === 'object') {
@@ -87,8 +87,8 @@ function getChildren (defaultChildren, partialChildren) {
87
87
  * @returns {import('./index.js').ComponentName}
88
88
  */
89
89
  function getDefaultComp (partial, schemaFragment, arrayChild) {
90
- const { type } = getType(schemaFragment)
91
- const hasSimpleType = ['string', 'integer', 'number'].includes(type)
90
+ const { type } = getSchemaFragmentType(schemaFragment)
91
+ const hasSimpleType = type && ['string', 'integer', 'number'].includes(type)
92
92
  if (arrayChild === 'oneOf') return 'one-of-select'
93
93
  if (hasSimpleType && schemaFragment.enum) return schemaFragment.enum.length > 20 ? 'autocomplete' : 'select'
94
94
  if (hasSimpleType && schemaFragment.oneOf) return schemaFragment.oneOf.length > 20 ? 'autocomplete' : 'select'
@@ -162,8 +162,8 @@ function normalizeExpression (expression, defaultType = 'js-eval') {
162
162
  */
163
163
  function getItemsFromSchema (schemaFragment) {
164
164
  if (!schemaFragment) return null
165
- const { type } = getType(schemaFragment)
166
- const hasSimpleType = ['string', 'integer', 'number'].includes(type)
165
+ const { type } = getSchemaFragmentType(schemaFragment)
166
+ const hasSimpleType = type && ['string', 'integer', 'number'].includes(type)
167
167
  if (schemaFragment.enum && hasSimpleType) {
168
168
  return schemaFragment.enum.map((/** @type {string} */ value) => ({ key: value + '', title: value + '', value }))
169
169
  }
@@ -194,13 +194,36 @@ function getItemsFromSchema (schemaFragment) {
194
194
 
195
195
  /**
196
196
  * @param {SchemaFragment} schemaFragment
197
- * @returns {{type: string, nullable: boolean}}
197
+ * @returns {{type: string | undefined, nullable: boolean}}
198
198
  */
199
- const getType = (schemaFragment) => {
199
+ export const getSchemaFragmentType = (schemaFragment) => {
200
200
  if (Array.isArray(schemaFragment.type) && schemaFragment.type.length === 2 && schemaFragment.type.includes('null')) {
201
201
  const type = schemaFragment.type.find(t => t !== 'null')
202
202
  return { type, nullable: true }
203
203
  }
204
+
205
+ if (!schemaFragment.type && schemaFragment.properties) {
206
+ return { type: 'object', nullable: false }
207
+ }
208
+
209
+ // case where type is not defined but can be deduced from children oneOf/allOf/anyOf
210
+ /** @type {any[]} */
211
+ if (!schemaFragment.type) {
212
+ /** @type {string[]} */
213
+ const combinationTypes = []
214
+ for (const combinationKey of ['allOf', 'anyOf', 'oneOf']) {
215
+ // @ts-ignore
216
+ if (schemaFragment[combinationKey]) {
217
+ // @ts-ignore
218
+ for (const subSchema of schemaFragment[combinationKey]) {
219
+ const { type: subType } = getSchemaFragmentType(subSchema)
220
+ if (subType && !combinationTypes.includes(subType)) combinationTypes.push(subType)
221
+ }
222
+ }
223
+ }
224
+ if (combinationTypes.length === 1) return { type: combinationTypes[0], nullable: false }
225
+ }
226
+
204
227
  return { type: schemaFragment.type, nullable: false }
205
228
  }
206
229
 
@@ -218,7 +241,7 @@ function getCompObject (layoutKeyword, schemaFragment, schemaPath, markdown, opt
218
241
  const errors = []
219
242
  const key = schemaPath.slice(schemaPath.lastIndexOf('/') + 1)
220
243
 
221
- const { type, nullable } = getType(schemaFragment)
244
+ const { type, nullable } = getSchemaFragmentType(schemaFragment)
222
245
 
223
246
  if ('const' in schemaFragment) return { normalized: { comp: 'none' }, errors }
224
247
  if (!type) return { normalized: { comp: 'none' }, errors }
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from "./layout-keyword/index.js";
2
2
  export * from "./normalized-layout/index.js";
3
- export { normalizeLayoutFragment } from "./normalize.js";
4
3
  export type SchemaFragment = import("./types.js").SchemaFragment;
4
+ export { normalizeLayoutFragment, getSchemaFragmentType } from "./normalize.js";
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;6BAAc,OAAO,YAAY,EAAE,cAAc"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;6BAAc,OAAO,YAAY,EAAE,cAAc"}
@@ -10,6 +10,10 @@ export function normalizeLayoutFragment(schemaFragment: SchemaFragment, schemaPa
10
10
  layout: NormalizedLayout;
11
11
  errors: string[];
12
12
  };
13
+ export function getSchemaFragmentType(schemaFragment: SchemaFragment): {
14
+ type: string | undefined;
15
+ nullable: boolean;
16
+ };
13
17
  export type Child = import('./index.js').Child;
14
18
  export type Children = import('./index.js').Children;
15
19
  export type CompObject = import('./index.js').CompObject;
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.js"],"names":[],"mappings":"AA2bA;;;;;;;GAOG;AACH,wDAPW,cAAc,cACd,MAAM,oBACC,MAAM,KAAK,MAAM,yEAGtB;IAAC,MAAM,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAC,CA2BxD;oBAxdY,OAAO,YAAY,EAAE,KAAK;uBAC1B,OAAO,YAAY,EAAE,QAAQ;yBAC7B,OAAO,YAAY,EAAE,UAAU;yBAC/B,OAAO,YAAY,EAAE,UAAU;+BAC/B,OAAO,YAAY,EAAE,gBAAgB;4BACrC,OAAO,YAAY,EAAE,aAAa;8BAClC,OAAO,YAAY,EAAE,eAAe;gCACpC,OAAO,YAAY,EAAE,iBAAiB;6BACtC,OAAO,YAAY,EAAE,cAAc"}
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.js"],"names":[],"mappings":"AAkdA;;;;;;;GAOG;AACH,wDAPW,cAAc,cACd,MAAM,oBACC,MAAM,KAAK,MAAM,yEAGtB;IAAC,MAAM,EAAE,gBAAgB,CAAC;IAAC,MAAM,EAAE,MAAM,EAAE,CAAA;CAAC,CA2BxD;AA7SM,sDAHI,cAAc,GACZ;IAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAC,CA+BzD;oBA/NY,OAAO,YAAY,EAAE,KAAK;uBAC1B,OAAO,YAAY,EAAE,QAAQ;yBAC7B,OAAO,YAAY,EAAE,UAAU;yBAC/B,OAAO,YAAY,EAAE,UAAU;+BAC/B,OAAO,YAAY,EAAE,gBAAgB;4BACrC,OAAO,YAAY,EAAE,aAAa;8BAClC,OAAO,YAAY,EAAE,eAAe;gCACpC,OAAO,YAAY,EAAE,iBAAiB;6BACtC,OAAO,YAAY,EAAE,cAAc"}