@json-layout/core 1.10.9 → 1.10.11

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/core",
3
- "version": "1.10.9",
3
+ "version": "1.10.11",
4
4
  "description": "Compilation and state management utilities for JSON Layout.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -114,9 +114,9 @@ const produceStateNodeData = produce((draft, parentDataPath, children, additiona
114
114
  if (child.data === undefined || child.data === null) continue
115
115
  if (children.length > 1) {
116
116
  for (const key of Object.keys(child.data)) {
117
- if (child.skeleton.propertyKeys.includes(key) || !children.some(c => c.key !== child.key && c.skeleton.propertyKeys.includes(key))) {
118
- draft[key] = /** @type {any} */(child.data)[key]
119
- }
117
+ if (children.some(c => c.key === key)) continue
118
+ if (!child.skeleton.propertyKeys.includes(key) && children.some(c => c.key !== child.key && c.skeleton.propertyKeys.includes(key))) continue
119
+ draft[key] = /** @type {any} */(child.data)[key]
120
120
  }
121
121
  } else {
122
122
  Object.assign(draft, child.data)
@@ -187,7 +187,7 @@ const produceCompositeChildrenOptions = produce((draft, section) => {
187
187
  * @param {string | null} parentDataPath
188
188
  * @returns {boolean}
189
189
  */
190
- const matchError = (error, skeleton, dataPath, parentDataPath) => {
190
+ const matchLocalError = (error, skeleton, dataPath, parentDataPath) => {
191
191
  const originalError = error.params?.errors?.[0] ?? error
192
192
  if (parentDataPath === originalError.instancePath && originalError.params?.missingProperty === skeleton.key) {
193
193
  return true
@@ -204,22 +204,51 @@ const matchError = (error, skeleton, dataPath, parentDataPath) => {
204
204
 
205
205
  /**
206
206
  * should match if an error belongs to a child of the current node but the child was not displayed
207
+ * @param {import('../index.js').CompiledLayout} compiledLayout
207
208
  * @param {import('ajv').ErrorObject} error
208
209
  * @param {import('../index.js').SkeletonNode} skeleton
209
210
  * @param {string} dataPath
210
211
  * @param {string | null} parentDataPath
211
212
  * @returns {boolean}
212
213
  */
213
- const matchChildError = (error, skeleton, dataPath, parentDataPath) => {
214
+ const matchChildError = (compiledLayout, error, skeleton, dataPath, parentDataPath) => {
214
215
  const originalError = error.params?.errors?.[0] ?? error
215
- if (!(
216
- originalError.schemaPath === skeleton.pointer ||
217
- originalError.schemaPath.startsWith(skeleton.pointer + '/')
218
- ) && !(
219
- originalError.schemaPath === skeleton.refPointer ||
220
- originalError.schemaPath.startsWith(skeleton.refPointer + '/')
221
- )) return false
222
- if (originalError.instancePath.startsWith(dataPath)) return true
216
+ if (!originalError.instancePath.startsWith(dataPath)) return false
217
+ if (originalError.schemaPath === skeleton.pointer || originalError.schemaPath.startsWith(skeleton.pointer + '/') || originalError.schemaPath === skeleton.refPointer || originalError.schemaPath.startsWith(skeleton.refPointer + '/')) return true
218
+ if (skeleton.children) {
219
+ for (const c of skeleton.children) {
220
+ const childSkeleton = compiledLayout.skeletonNodes[c]
221
+ if (!childSkeleton) continue
222
+ if (matchPointerError(error, childSkeleton.pointer, childSkeleton.refPointer, dataPath, parentDataPath)) return true
223
+ }
224
+ }
225
+ if (skeleton.childrenTrees) {
226
+ for (const c of skeleton.childrenTrees) {
227
+ const childTree = compiledLayout.skeletonTrees[c]
228
+ if (!childTree) continue
229
+ if (matchPointerError(error, childTree.root, childTree.refPointer, dataPath, parentDataPath)) return true
230
+ }
231
+ }
232
+ return false
233
+ }
234
+
235
+ /**
236
+ * should match if an error belongs to a child of the current node but the child was not displayed
237
+ * @param {import('ajv').ErrorObject} error
238
+ * @param {string} pointer1
239
+ * @param {string} pointer2
240
+ * @param {string} dataPath
241
+ * @param {string | null} parentDataPath
242
+ * @returns {boolean}
243
+ */
244
+ const matchPointerError = (error, pointer1, pointer2, dataPath, parentDataPath) => {
245
+ const originalError = error.params?.errors?.[0] ?? error
246
+ if (!originalError.instancePath.startsWith(dataPath)) return false
247
+ if (originalError.schemaPath === pointer1 ||
248
+ originalError.schemaPath.startsWith(pointer2 + '/') ||
249
+ originalError.schemaPath === pointer2 ||
250
+ originalError.schemaPath.startsWith(pointer2 + '/')
251
+ ) return true
223
252
  return false
224
253
  }
225
254
 
@@ -442,25 +471,36 @@ export function createStateNode (
442
471
 
443
472
  if (key === '$oneOf' && skeleton.childrenTrees) {
444
473
  // find the oneOf child that was either previously selected, if none were selected select the child that is valid with current data
445
- const activeChildTreeIndex = /** @type {number} */(fullKey in context.activatedItems ? context.activatedItems[fullKey] : skeleton.childrenTrees?.findIndex((childTree) => compiledLayout.validates[compiledLayout.skeletonTrees[childTree].refPointer](data)))
474
+ const validChildTreeIndex = skeleton.childrenTrees?.findIndex((childTree) => compiledLayout.validates[compiledLayout.skeletonTrees[childTree].refPointer](data))
475
+ const activeChildTreeIndex = /** @type {number} */(fullKey in context.activatedItems ? context.activatedItems[fullKey] : validChildTreeIndex)
446
476
  if (activeChildTreeIndex !== -1) {
477
+ const activeChildTree = compiledLayout.skeletonTrees[skeleton.childrenTrees[activeChildTreeIndex]]
478
+ const activeChildNode = compiledLayout.skeletonNodes[activeChildTree.root]
447
479
  if (!(fullKey in context.activatedItems)) context.autoActivatedItems[fullKey] = activeChildTreeIndex
448
480
  context.errors = context.errors?.filter(error => {
449
- const originalError = error.params?.errors?.[0] ?? error
450
- // console.log(originalError.schemaPath, skeleton.pointer, skeleton.refPointer)
451
481
  // if an item was selected, remove the oneOf error
452
- if (matchError(error, skeleton, dataPath, parentDataPath)) return false
482
+ if (matchLocalError(error, skeleton, dataPath, parentDataPath)) {
483
+ return false
484
+ }
453
485
  // also remove the errors from other children of the oneOf
454
- if (matchChildError(error, skeleton, dataPath, parentDataPath)) {
455
- if (originalError.schemaPath.startsWith(skeleton.pointer) && !originalError.schemaPath.startsWith(skeleton.pointer + '/' + activeChildTreeIndex)) return false
456
- if (originalError.schemaPath.startsWith(skeleton.refPointer) && !originalError.schemaPath.startsWith(skeleton.refPointer + '/' + activeChildTreeIndex)) return false
486
+ if (matchChildError(compiledLayout, error, skeleton, dataPath, parentDataPath) && !matchPointerError(error, activeChildNode.pointer, activeChildNode.refPointer, dataPath, parentDataPath)) {
487
+ return false
457
488
  }
458
489
  return true
459
490
  })
460
- // console.log(context.errors?.map(error => error.params?.errors?.[0] ?? error))
491
+ if (context.additionalPropertiesErrors) {
492
+ // remove additional properties errors if the oneOf has no valid children
493
+ context.additionalPropertiesErrors = context.additionalPropertiesErrors?.filter(error => {
494
+ // also remove the additional errors from other children of the oneOf
495
+ if (matchChildError(compiledLayout, error, skeleton, dataPath, parentDataPath) && !matchPointerError(error, activeChildNode.pointer, activeChildNode.refPointer, dataPath, parentDataPath)) {
496
+ return false
497
+ }
498
+ return true
499
+ })
500
+ }
461
501
  const activeChildKey = `${fullKey}/${activeChildTreeIndex}`
462
502
  if (context.autofocusTarget === fullKey) context.autofocusTarget = activeChildKey
463
- const activeChildTree = compiledLayout.skeletonTrees[skeleton.childrenTrees[activeChildTreeIndex]]
503
+
464
504
  children = [
465
505
  createStateNode(
466
506
  context,
@@ -471,7 +511,7 @@ export function createStateNode (
471
511
  fullKey,
472
512
  dataPath,
473
513
  dataPath,
474
- compiledLayout.skeletonNodes[activeChildTree.root],
514
+ activeChildNode,
475
515
  null,
476
516
  display,
477
517
  data,
@@ -562,14 +602,14 @@ export function createStateNode (
562
602
  }
563
603
  }
564
604
 
565
- let error = context.errors?.find(e => matchError(e, skeleton, dataPath, parentDataPath))
605
+ let error = context.errors?.find(e => matchLocalError(e, skeleton, dataPath, parentDataPath))
566
606
 
567
607
  // findLast in following lines is important because we want to keep the error of the highest child (deepest errors are listed first)
568
608
  if (!error && !isCompositeLayout(layout, compiledLayout.components) && layout.comp !== 'slot') {
569
- error = context.errors?.findLast(e => matchChildError(e, skeleton, dataPath, parentDataPath))
609
+ error = context.errors?.findLast(e => matchChildError(compiledLayout, e, skeleton, dataPath, parentDataPath))
570
610
  }
571
611
  if (!error && context.rehydrate && context.rehydrateErrors) {
572
- error = context.rehydrateErrors?.findLast(e => matchChildError(e, skeleton, dataPath, parentDataPath))
612
+ error = context.rehydrateErrors?.findLast(e => matchChildError(compiledLayout, e, skeleton, dataPath, parentDataPath))
573
613
  }
574
614
 
575
615
  // capture errors so that they are not repeated in parent nodes
@@ -578,10 +618,10 @@ export function createStateNode (
578
618
  logValidation(`${fullKey} capture validation error on node`, error)
579
619
  context.errors = context.errors?.filter(e => error !== e)
580
620
  if (!isCompositeLayout(layout, compiledLayout.components) && layout.comp !== 'slot') {
581
- context.errors = context.errors?.filter(e => !matchChildError(e, skeleton, dataPath, parentDataPath))
621
+ context.errors = context.errors?.filter(e => !matchChildError(compiledLayout, e, skeleton, dataPath, parentDataPath))
582
622
  }
583
623
  if (context.rehydrate && context.rehydrateErrors) {
584
- context.rehydrateErrors = context.rehydrateErrors?.filter(e => !matchChildError(e, skeleton, dataPath, parentDataPath))
624
+ context.rehydrateErrors = context.rehydrateErrors?.filter(e => !matchChildError(compiledLayout, e, skeleton, dataPath, parentDataPath))
585
625
  }
586
626
  }
587
627
  }
@@ -1 +1 @@
1
- {"version":3,"file":"state-node.d.ts","sourceRoot":"","sources":["../../src/state/state-node.js"],"names":[],"mappings":"AAiOA;;;;;;;;;;;GAWG;AACH,4CAXW,OAAO,aAAa,EAAE,kBAAkB,EAAE,cAC1C,OAAO,yBAAyB,EAAE,UAAU,QAC5C,GAAG,WACH,OAAO,YAAY,EAAE,gBAAgB,WACrC,OAAO,oBAAoB,EAAE,OAAO,UACpC,OAAO,yBAAyB,EAAE,cAAc,aAChD,MAAM,CAAC,MAAM,EAAE,OAAO,KAAK,EAAE,gBAAgB,CAAC,YAC9C,OAAO,iBACP,OAAO,qBAAqB,EAAE,uBAAuB,GAAG,IAAI,GAC1D,GAAG,CAsBf;AAiCD;;;;;;;;;;;;;;;;;;GAkBG;AACH,yCAjBW,OAAO,YAAY,EAAE,sBAAsB,iBAC3C,OAAO,YAAY,EAAE,gBAAgB,kBACrC,OAAO,aAAa,EAAE,cAAc,OACpC,MAAM,GAAG,MAAM,WACf,MAAM,iBACN,MAAM,GAAG,IAAI,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