@rjsf/utils 5.21.1 → 5.22.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/dist/index.js +201 -62
- package/dist/index.js.map +3 -3
- package/dist/utils.esm.js +201 -62
- package/dist/utils.esm.js.map +2 -2
- package/dist/utils.umd.js +199 -69
- package/lib/ErrorSchemaBuilder.d.ts +3 -3
- package/lib/ErrorSchemaBuilder.js +2 -1
- package/lib/ErrorSchemaBuilder.js.map +1 -1
- package/lib/createSchemaUtils.d.ts +3 -2
- package/lib/createSchemaUtils.js +17 -11
- package/lib/createSchemaUtils.js.map +1 -1
- package/lib/mergeDefaultsWithFormData.d.ts +4 -2
- package/lib/mergeDefaultsWithFormData.js +9 -4
- package/lib/mergeDefaultsWithFormData.js.map +1 -1
- package/lib/schema/getClosestMatchingOption.js +6 -2
- package/lib/schema/getClosestMatchingOption.js.map +1 -1
- package/lib/schema/getDefaultFormState.d.ts +6 -3
- package/lib/schema/getDefaultFormState.js +43 -22
- package/lib/schema/getDefaultFormState.js.map +1 -1
- package/lib/schema/retrieveSchema.d.ts +19 -10
- package/lib/schema/retrieveSchema.js +54 -26
- package/lib/schema/retrieveSchema.js.map +1 -1
- package/lib/schema/toIdSchema.d.ts +3 -2
- package/lib/schema/toIdSchema.js +8 -6
- package/lib/schema/toIdSchema.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +19 -3
- package/package.json +2 -2
- package/src/ErrorSchemaBuilder.ts +6 -5
- package/src/createSchemaUtils.ts +43 -11
- package/src/mergeDefaultsWithFormData.ts +16 -4
- package/src/schema/getClosestMatchingOption.ts +6 -2
- package/src/schema/getDefaultFormState.ts +58 -19
- package/src/schema/retrieveSchema.ts +125 -28
- package/src/schema/toIdSchema.ts +32 -7
- package/src/types.ts +20 -2
package/dist/utils.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/isPlainObject'), require('lodash/isEqualWith'), require('lodash/get'), require('lodash/isEmpty'), require('jsonpointer'), require('lodash/omit'), require('lodash/has'), require('lodash/isNumber'), require('lodash/isObject'), require('lodash/isString'), require('lodash/reduce'), require('lodash/times'), require('lodash/isEqual'), require('lodash/set'), require('lodash/transform'), require('lodash/merge'), require('lodash/flattenDeep'), require('lodash/uniq'), require('json-schema-merge-allof'), require('lodash/union'), require('lodash/isNil'), require('lodash/cloneDeep'), require('react'), require('react-is'), require('react/jsx-runtime'), require('lodash/toPath'), require('lodash/forEach')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'lodash/isPlainObject', 'lodash/isEqualWith', 'lodash/get', 'lodash/isEmpty', 'jsonpointer', 'lodash/omit', 'lodash/has', 'lodash/isNumber', 'lodash/isObject', 'lodash/isString', 'lodash/reduce', 'lodash/times', 'lodash/isEqual', 'lodash/set', 'lodash/transform', 'lodash/merge', 'lodash/flattenDeep', 'lodash/uniq', 'json-schema-merge-allof', 'lodash/union', 'lodash/isNil', 'lodash/cloneDeep', 'react', 'react-is', 'react/jsx-runtime', 'lodash/toPath', 'lodash/forEach'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/utils"] = {}, global.isPlainObject, global.isEqualWith, global.get8, global.isEmpty, global.jsonpointer, global.omit, global.has3, global.isNumber, global.isObject2, global.isString, global.reduce, global.times, global.isEqual4, global.set3, global.transform, global.merge, global.flattenDeep, global.uniq, global.mergeAllOf, global.union, global.isNil, global.cloneDeep, global.react, global.ReactIs, global.jsxRuntime, global.toPath, global.forEach));
|
|
5
|
-
})(this, (function (exports, isPlainObject, isEqualWith, get8, isEmpty, jsonpointer, omit, has3, isNumber, isObject2, isString, reduce, times, isEqual4, set3, transform, merge, flattenDeep, uniq, mergeAllOf, union, isNil, cloneDeep, react, ReactIs, jsxRuntime, toPath, forEach) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash/isPlainObject'), require('lodash/isEqualWith'), require('lodash/get'), require('lodash/isEmpty'), require('jsonpointer'), require('lodash/omit'), require('lodash/has'), require('lodash/isNumber'), require('lodash/isObject'), require('lodash/isString'), require('lodash/reduce'), require('lodash/times'), require('lodash/isEqual'), require('lodash/set'), require('lodash/transform'), require('lodash/merge'), require('lodash/flattenDeep'), require('lodash/uniq'), require('json-schema-merge-allof'), require('lodash/union'), require('lodash/isNil'), require('lodash/cloneDeep'), require('lodash/setWith'), require('react'), require('react-is'), require('react/jsx-runtime'), require('lodash/toPath'), require('lodash/forEach')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'lodash/isPlainObject', 'lodash/isEqualWith', 'lodash/get', 'lodash/isEmpty', 'jsonpointer', 'lodash/omit', 'lodash/has', 'lodash/isNumber', 'lodash/isObject', 'lodash/isString', 'lodash/reduce', 'lodash/times', 'lodash/isEqual', 'lodash/set', 'lodash/transform', 'lodash/merge', 'lodash/flattenDeep', 'lodash/uniq', 'json-schema-merge-allof', 'lodash/union', 'lodash/isNil', 'lodash/cloneDeep', 'lodash/setWith', 'react', 'react-is', 'react/jsx-runtime', 'lodash/toPath', 'lodash/forEach'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/utils"] = {}, global.isPlainObject, global.isEqualWith, global.get8, global.isEmpty, global.jsonpointer, global.omit, global.has3, global.isNumber, global.isObject2, global.isString, global.reduce, global.times, global.isEqual4, global.set3, global.transform, global.merge, global.flattenDeep, global.uniq, global.mergeAllOf, global.union, global.isNil, global.cloneDeep, global.setWith, global.react, global.ReactIs, global.jsxRuntime, global.toPath, global.forEach));
|
|
5
|
+
})(this, (function (exports, isPlainObject, isEqualWith, get8, isEmpty, jsonpointer, omit, has3, isNumber, isObject2, isString, reduce, times, isEqual4, set3, transform, merge, flattenDeep, uniq, mergeAllOf, union, isNil, cloneDeep, setWith, react, ReactIs, jsxRuntime, toPath, forEach) { 'use strict';
|
|
6
6
|
|
|
7
7
|
// src/isObject.ts
|
|
8
8
|
function isObject(thing) {
|
|
@@ -324,10 +324,18 @@
|
|
|
324
324
|
}
|
|
325
325
|
|
|
326
326
|
// src/schema/retrieveSchema.ts
|
|
327
|
-
function retrieveSchema(validator, schema, rootSchema = {}, rawFormData) {
|
|
328
|
-
return retrieveSchemaInternal(
|
|
327
|
+
function retrieveSchema(validator, schema, rootSchema = {}, rawFormData, experimental_customMergeAllOf) {
|
|
328
|
+
return retrieveSchemaInternal(
|
|
329
|
+
validator,
|
|
330
|
+
schema,
|
|
331
|
+
rootSchema,
|
|
332
|
+
rawFormData,
|
|
333
|
+
void 0,
|
|
334
|
+
void 0,
|
|
335
|
+
experimental_customMergeAllOf
|
|
336
|
+
)[0];
|
|
329
337
|
}
|
|
330
|
-
function resolveCondition(validator, schema, rootSchema, expandAllBranches, recurseList, formData) {
|
|
338
|
+
function resolveCondition(validator, schema, rootSchema, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
331
339
|
const { if: expression, then, else: otherwise, ...resolvedSchemaLessConditional } = schema;
|
|
332
340
|
const conditionValue = validator.isValid(expression, formData || {}, rootSchema);
|
|
333
341
|
let resolvedSchemas = [resolvedSchemaLessConditional];
|
|
@@ -335,12 +343,28 @@
|
|
|
335
343
|
if (expandAllBranches) {
|
|
336
344
|
if (then && typeof then !== "boolean") {
|
|
337
345
|
schemas = schemas.concat(
|
|
338
|
-
retrieveSchemaInternal(
|
|
346
|
+
retrieveSchemaInternal(
|
|
347
|
+
validator,
|
|
348
|
+
then,
|
|
349
|
+
rootSchema,
|
|
350
|
+
formData,
|
|
351
|
+
expandAllBranches,
|
|
352
|
+
recurseList,
|
|
353
|
+
experimental_customMergeAllOf
|
|
354
|
+
)
|
|
339
355
|
);
|
|
340
356
|
}
|
|
341
357
|
if (otherwise && typeof otherwise !== "boolean") {
|
|
342
358
|
schemas = schemas.concat(
|
|
343
|
-
retrieveSchemaInternal(
|
|
359
|
+
retrieveSchemaInternal(
|
|
360
|
+
validator,
|
|
361
|
+
otherwise,
|
|
362
|
+
rootSchema,
|
|
363
|
+
formData,
|
|
364
|
+
expandAllBranches,
|
|
365
|
+
recurseList,
|
|
366
|
+
experimental_customMergeAllOf
|
|
367
|
+
)
|
|
344
368
|
);
|
|
345
369
|
}
|
|
346
370
|
} else {
|
|
@@ -353,7 +377,8 @@
|
|
|
353
377
|
rootSchema,
|
|
354
378
|
formData,
|
|
355
379
|
expandAllBranches,
|
|
356
|
-
recurseList
|
|
380
|
+
recurseList,
|
|
381
|
+
experimental_customMergeAllOf
|
|
357
382
|
)
|
|
358
383
|
);
|
|
359
384
|
}
|
|
@@ -362,7 +387,15 @@
|
|
|
362
387
|
resolvedSchemas = schemas.map((s) => mergeSchemas(resolvedSchemaLessConditional, s));
|
|
363
388
|
}
|
|
364
389
|
return resolvedSchemas.flatMap(
|
|
365
|
-
(s) => retrieveSchemaInternal(
|
|
390
|
+
(s) => retrieveSchemaInternal(
|
|
391
|
+
validator,
|
|
392
|
+
s,
|
|
393
|
+
rootSchema,
|
|
394
|
+
formData,
|
|
395
|
+
expandAllBranches,
|
|
396
|
+
recurseList,
|
|
397
|
+
experimental_customMergeAllOf
|
|
398
|
+
)
|
|
366
399
|
);
|
|
367
400
|
}
|
|
368
401
|
function getAllPermutationsOfXxxOf(listOfLists) {
|
|
@@ -379,7 +412,7 @@
|
|
|
379
412
|
);
|
|
380
413
|
return allPermutations;
|
|
381
414
|
}
|
|
382
|
-
function resolveSchema(validator, schema, rootSchema, expandAllBranches, recurseList, formData) {
|
|
415
|
+
function resolveSchema(validator, schema, rootSchema, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
383
416
|
const updatedSchemas = resolveReference(
|
|
384
417
|
validator,
|
|
385
418
|
schema,
|
|
@@ -401,7 +434,15 @@
|
|
|
401
434
|
formData
|
|
402
435
|
);
|
|
403
436
|
return resolvedSchemas.flatMap((s) => {
|
|
404
|
-
return retrieveSchemaInternal(
|
|
437
|
+
return retrieveSchemaInternal(
|
|
438
|
+
validator,
|
|
439
|
+
s,
|
|
440
|
+
rootSchema,
|
|
441
|
+
formData,
|
|
442
|
+
expandAllBranches,
|
|
443
|
+
recurseList,
|
|
444
|
+
experimental_customMergeAllOf
|
|
445
|
+
);
|
|
405
446
|
});
|
|
406
447
|
}
|
|
407
448
|
if (ALL_OF_KEY in schema && Array.isArray(schema.allOf)) {
|
|
@@ -412,7 +453,8 @@
|
|
|
412
453
|
rootSchema,
|
|
413
454
|
formData,
|
|
414
455
|
expandAllBranches,
|
|
415
|
-
recurseList
|
|
456
|
+
recurseList,
|
|
457
|
+
experimental_customMergeAllOf
|
|
416
458
|
)
|
|
417
459
|
);
|
|
418
460
|
const allPermutations = getAllPermutationsOfXxxOf(allOfSchemaElements);
|
|
@@ -420,7 +462,7 @@
|
|
|
420
462
|
}
|
|
421
463
|
return [schema];
|
|
422
464
|
}
|
|
423
|
-
function resolveReference(validator, schema, rootSchema, expandAllBranches, recurseList, formData) {
|
|
465
|
+
function resolveReference(validator, schema, rootSchema, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
424
466
|
const updatedSchema = resolveAllReferences(schema, rootSchema, recurseList);
|
|
425
467
|
if (updatedSchema !== schema) {
|
|
426
468
|
return retrieveSchemaInternal(
|
|
@@ -429,7 +471,8 @@
|
|
|
429
471
|
rootSchema,
|
|
430
472
|
formData,
|
|
431
473
|
expandAllBranches,
|
|
432
|
-
recurseList
|
|
474
|
+
recurseList,
|
|
475
|
+
experimental_customMergeAllOf
|
|
433
476
|
);
|
|
434
477
|
}
|
|
435
478
|
return [schema];
|
|
@@ -507,7 +550,7 @@
|
|
|
507
550
|
});
|
|
508
551
|
return schema;
|
|
509
552
|
}
|
|
510
|
-
function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expandAllBranches = false, recurseList = []) {
|
|
553
|
+
function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expandAllBranches = false, recurseList = [], experimental_customMergeAllOf) {
|
|
511
554
|
if (!isObject(schema)) {
|
|
512
555
|
return [{}];
|
|
513
556
|
}
|
|
@@ -528,7 +571,8 @@
|
|
|
528
571
|
rootSchema,
|
|
529
572
|
expandAllBranches,
|
|
530
573
|
recurseList,
|
|
531
|
-
rawFormData
|
|
574
|
+
rawFormData,
|
|
575
|
+
experimental_customMergeAllOf
|
|
532
576
|
);
|
|
533
577
|
}
|
|
534
578
|
if (ALL_OF_KEY in resolvedSchema) {
|
|
@@ -537,9 +581,24 @@
|
|
|
537
581
|
return [...allOf, restOfSchema];
|
|
538
582
|
}
|
|
539
583
|
try {
|
|
540
|
-
|
|
584
|
+
const withContainsSchemas = [];
|
|
585
|
+
const withoutContainsSchemas = [];
|
|
586
|
+
resolvedSchema.allOf?.forEach((s2) => {
|
|
587
|
+
if (typeof s2 === "object" && s2.contains) {
|
|
588
|
+
withContainsSchemas.push(s2);
|
|
589
|
+
} else {
|
|
590
|
+
withoutContainsSchemas.push(s2);
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
if (withContainsSchemas.length) {
|
|
594
|
+
resolvedSchema = { ...resolvedSchema, allOf: withoutContainsSchemas };
|
|
595
|
+
}
|
|
596
|
+
resolvedSchema = experimental_customMergeAllOf ? experimental_customMergeAllOf(resolvedSchema) : mergeAllOf(resolvedSchema, {
|
|
541
597
|
deep: false
|
|
542
598
|
});
|
|
599
|
+
if (withContainsSchemas.length) {
|
|
600
|
+
resolvedSchema.allOf = withContainsSchemas;
|
|
601
|
+
}
|
|
543
602
|
} catch (e) {
|
|
544
603
|
console.warn("could not merge subschemas in allOf:\n", e);
|
|
545
604
|
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
|
|
@@ -575,7 +634,7 @@
|
|
|
575
634
|
}
|
|
576
635
|
return [schema];
|
|
577
636
|
}
|
|
578
|
-
function resolveDependencies(validator, schema, rootSchema, expandAllBranches, recurseList, formData) {
|
|
637
|
+
function resolveDependencies(validator, schema, rootSchema, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
579
638
|
const { dependencies, ...remainingSchema } = schema;
|
|
580
639
|
const resolvedSchemas = resolveAnyOrOneOfSchemas(
|
|
581
640
|
validator,
|
|
@@ -592,11 +651,12 @@
|
|
|
592
651
|
rootSchema,
|
|
593
652
|
expandAllBranches,
|
|
594
653
|
recurseList,
|
|
595
|
-
formData
|
|
654
|
+
formData,
|
|
655
|
+
experimental_customMergeAllOf
|
|
596
656
|
)
|
|
597
657
|
);
|
|
598
658
|
}
|
|
599
|
-
function processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, recurseList, formData) {
|
|
659
|
+
function processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
600
660
|
let schemas = [resolvedSchema];
|
|
601
661
|
for (const dependencyKey in dependencies) {
|
|
602
662
|
if (!expandAllBranches && get8(formData, [dependencyKey]) === void 0) {
|
|
@@ -620,7 +680,8 @@
|
|
|
620
680
|
dependencyValue,
|
|
621
681
|
expandAllBranches,
|
|
622
682
|
recurseList,
|
|
623
|
-
formData
|
|
683
|
+
formData,
|
|
684
|
+
experimental_customMergeAllOf
|
|
624
685
|
);
|
|
625
686
|
}
|
|
626
687
|
return schemas.flatMap(
|
|
@@ -631,7 +692,8 @@
|
|
|
631
692
|
rootSchema,
|
|
632
693
|
expandAllBranches,
|
|
633
694
|
recurseList,
|
|
634
|
-
formData
|
|
695
|
+
formData,
|
|
696
|
+
experimental_customMergeAllOf
|
|
635
697
|
)
|
|
636
698
|
);
|
|
637
699
|
}
|
|
@@ -644,14 +706,15 @@
|
|
|
644
706
|
const required = Array.isArray(schema.required) ? Array.from(/* @__PURE__ */ new Set([...schema.required, ...additionallyRequired])) : additionallyRequired;
|
|
645
707
|
return { ...schema, required };
|
|
646
708
|
}
|
|
647
|
-
function withDependentSchema(validator, schema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, recurseList, formData) {
|
|
709
|
+
function withDependentSchema(validator, schema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
648
710
|
const dependentSchemas = retrieveSchemaInternal(
|
|
649
711
|
validator,
|
|
650
712
|
dependencyValue,
|
|
651
713
|
rootSchema,
|
|
652
714
|
formData,
|
|
653
715
|
expandAllBranches,
|
|
654
|
-
recurseList
|
|
716
|
+
recurseList,
|
|
717
|
+
experimental_customMergeAllOf
|
|
655
718
|
);
|
|
656
719
|
return dependentSchemas.flatMap((dependent) => {
|
|
657
720
|
const { oneOf, ...dependentSchema } = dependent;
|
|
@@ -675,12 +738,13 @@
|
|
|
675
738
|
resolvedOneOf,
|
|
676
739
|
expandAllBranches,
|
|
677
740
|
recurseList,
|
|
678
|
-
formData
|
|
741
|
+
formData,
|
|
742
|
+
experimental_customMergeAllOf
|
|
679
743
|
)
|
|
680
744
|
);
|
|
681
745
|
});
|
|
682
746
|
}
|
|
683
|
-
function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, oneOf, expandAllBranches, recurseList, formData) {
|
|
747
|
+
function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, oneOf, expandAllBranches, recurseList, formData, experimental_customMergeAllOf) {
|
|
684
748
|
const validSubschemas = oneOf.filter((subschema) => {
|
|
685
749
|
if (typeof subschema === "boolean" || !subschema || !subschema.properties) {
|
|
686
750
|
return false;
|
|
@@ -711,7 +775,8 @@
|
|
|
711
775
|
rootSchema,
|
|
712
776
|
formData,
|
|
713
777
|
expandAllBranches,
|
|
714
|
-
recurseList
|
|
778
|
+
recurseList,
|
|
779
|
+
experimental_customMergeAllOf
|
|
715
780
|
);
|
|
716
781
|
return schemas.map((s2) => mergeSchemas(schema, s2));
|
|
717
782
|
});
|
|
@@ -727,7 +792,7 @@
|
|
|
727
792
|
}
|
|
728
793
|
}
|
|
729
794
|
};
|
|
730
|
-
function calculateIndexScore(validator, rootSchema, schema, formData
|
|
795
|
+
function calculateIndexScore(validator, rootSchema, schema, formData) {
|
|
731
796
|
let totalScore = 0;
|
|
732
797
|
if (schema) {
|
|
733
798
|
if (isObject2(schema.properties)) {
|
|
@@ -755,7 +820,10 @@
|
|
|
755
820
|
);
|
|
756
821
|
}
|
|
757
822
|
if (value.type === "object") {
|
|
758
|
-
|
|
823
|
+
if (isObject2(formValue)) {
|
|
824
|
+
score += 1;
|
|
825
|
+
}
|
|
826
|
+
return score + calculateIndexScore(validator, rootSchema, value, formValue);
|
|
759
827
|
}
|
|
760
828
|
if (value.type === guessType(formValue)) {
|
|
761
829
|
let newScore = score + 1;
|
|
@@ -822,12 +890,17 @@
|
|
|
822
890
|
function isFixedItems(schema) {
|
|
823
891
|
return Array.isArray(schema.items) && schema.items.length > 0 && schema.items.every((item) => isObject(item));
|
|
824
892
|
}
|
|
825
|
-
function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults = false) {
|
|
893
|
+
function mergeDefaultsWithFormData(defaults, formData, mergeExtraArrayDefaults = false, defaultSupercedesUndefined = false) {
|
|
826
894
|
if (Array.isArray(formData)) {
|
|
827
895
|
const defaultsArray = Array.isArray(defaults) ? defaults : [];
|
|
828
896
|
const mapped = formData.map((value, idx) => {
|
|
829
897
|
if (defaultsArray[idx]) {
|
|
830
|
-
return mergeDefaultsWithFormData(
|
|
898
|
+
return mergeDefaultsWithFormData(
|
|
899
|
+
defaultsArray[idx],
|
|
900
|
+
value,
|
|
901
|
+
mergeExtraArrayDefaults,
|
|
902
|
+
defaultSupercedesUndefined
|
|
903
|
+
);
|
|
831
904
|
}
|
|
832
905
|
return value;
|
|
833
906
|
});
|
|
@@ -842,11 +915,15 @@
|
|
|
842
915
|
acc2[key] = mergeDefaultsWithFormData(
|
|
843
916
|
defaults ? get8(defaults, key) : {},
|
|
844
917
|
get8(formData, key),
|
|
845
|
-
mergeExtraArrayDefaults
|
|
918
|
+
mergeExtraArrayDefaults,
|
|
919
|
+
defaultSupercedesUndefined
|
|
846
920
|
);
|
|
847
921
|
return acc2;
|
|
848
922
|
}, acc);
|
|
849
923
|
}
|
|
924
|
+
if (defaultSupercedesUndefined && formData === void 0) {
|
|
925
|
+
return defaults;
|
|
926
|
+
}
|
|
850
927
|
return formData;
|
|
851
928
|
}
|
|
852
929
|
|
|
@@ -917,9 +994,9 @@
|
|
|
917
994
|
}
|
|
918
995
|
return {};
|
|
919
996
|
}
|
|
920
|
-
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields = [], experimental_defaultFormStateBehavior = {}) {
|
|
997
|
+
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields = [], experimental_defaultFormStateBehavior = {}, isConst = false) {
|
|
921
998
|
const { emptyObjectFields = "populateAllDefaults" } = experimental_defaultFormStateBehavior;
|
|
922
|
-
if (includeUndefinedValues) {
|
|
999
|
+
if (includeUndefinedValues || isConst) {
|
|
923
1000
|
obj[key] = computedDefault;
|
|
924
1001
|
} else if (emptyObjectFields !== "skipDefaults") {
|
|
925
1002
|
if (isObject(computedDefault)) {
|
|
@@ -949,6 +1026,7 @@
|
|
|
949
1026
|
includeUndefinedValues = false,
|
|
950
1027
|
_recurseList = [],
|
|
951
1028
|
experimental_defaultFormStateBehavior = void 0,
|
|
1029
|
+
experimental_customMergeAllOf = void 0,
|
|
952
1030
|
required
|
|
953
1031
|
} = computeDefaultsProps;
|
|
954
1032
|
const formData = isObject(rawFormData) ? rawFormData : {};
|
|
@@ -956,7 +1034,9 @@
|
|
|
956
1034
|
let defaults = parentDefaults;
|
|
957
1035
|
let schemaToCompute = null;
|
|
958
1036
|
let updatedRecurseList = _recurseList;
|
|
959
|
-
if (
|
|
1037
|
+
if (isConstant(schema)) {
|
|
1038
|
+
defaults = schema.const;
|
|
1039
|
+
} else if (isObject(defaults) && isObject(schema.default)) {
|
|
960
1040
|
defaults = mergeObjects(defaults, schema.default);
|
|
961
1041
|
} else if (DEFAULT_KEY in schema) {
|
|
962
1042
|
defaults = schema.default;
|
|
@@ -971,7 +1051,15 @@
|
|
|
971
1051
|
...formData,
|
|
972
1052
|
...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults)
|
|
973
1053
|
};
|
|
974
|
-
const resolvedSchema = resolveDependencies(
|
|
1054
|
+
const resolvedSchema = resolveDependencies(
|
|
1055
|
+
validator,
|
|
1056
|
+
schema,
|
|
1057
|
+
rootSchema,
|
|
1058
|
+
false,
|
|
1059
|
+
[],
|
|
1060
|
+
defaultFormData,
|
|
1061
|
+
experimental_customMergeAllOf
|
|
1062
|
+
);
|
|
975
1063
|
schemaToCompute = resolvedSchema[0];
|
|
976
1064
|
} else if (isFixedItems(schema)) {
|
|
977
1065
|
defaults = schema.items.map(
|
|
@@ -1039,18 +1127,24 @@
|
|
|
1039
1127
|
includeUndefinedValues = false,
|
|
1040
1128
|
_recurseList = [],
|
|
1041
1129
|
experimental_defaultFormStateBehavior = void 0,
|
|
1130
|
+
experimental_customMergeAllOf = void 0,
|
|
1042
1131
|
required
|
|
1043
1132
|
} = {}, defaults) {
|
|
1044
1133
|
{
|
|
1045
1134
|
const formData = isObject(rawFormData) ? rawFormData : {};
|
|
1046
1135
|
const schema = rawSchema;
|
|
1047
|
-
const retrievedSchema = experimental_defaultFormStateBehavior?.allOf === "populateDefaults" && ALL_OF_KEY in schema ? retrieveSchema(validator, schema, rootSchema, formData) : schema;
|
|
1136
|
+
const retrievedSchema = experimental_defaultFormStateBehavior?.allOf === "populateDefaults" && ALL_OF_KEY in schema ? retrieveSchema(validator, schema, rootSchema, formData, experimental_customMergeAllOf) : schema;
|
|
1137
|
+
const parentConst = retrievedSchema[CONST_KEY];
|
|
1048
1138
|
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
|
|
1049
1139
|
(acc, key) => {
|
|
1050
|
-
const
|
|
1140
|
+
const propertySchema = get8(retrievedSchema, [PROPERTIES_KEY, key]);
|
|
1141
|
+
const hasParentConst = isObject(parentConst) && parentConst[key] !== void 0;
|
|
1142
|
+
const hasConst = isObject(propertySchema) && CONST_KEY in propertySchema || hasParentConst;
|
|
1143
|
+
const computedDefault = computeDefaults(validator, propertySchema, {
|
|
1051
1144
|
rootSchema,
|
|
1052
1145
|
_recurseList,
|
|
1053
1146
|
experimental_defaultFormStateBehavior,
|
|
1147
|
+
experimental_customMergeAllOf,
|
|
1054
1148
|
includeUndefinedValues: includeUndefinedValues === true,
|
|
1055
1149
|
parentDefaults: get8(defaults, [key]),
|
|
1056
1150
|
rawFormData: get8(formData, [key]),
|
|
@@ -1063,7 +1157,8 @@
|
|
|
1063
1157
|
includeUndefinedValues,
|
|
1064
1158
|
required,
|
|
1065
1159
|
retrievedSchema.required,
|
|
1066
|
-
experimental_defaultFormStateBehavior
|
|
1160
|
+
experimental_defaultFormStateBehavior,
|
|
1161
|
+
hasConst
|
|
1067
1162
|
);
|
|
1068
1163
|
return acc;
|
|
1069
1164
|
},
|
|
@@ -1145,11 +1240,14 @@
|
|
|
1145
1240
|
});
|
|
1146
1241
|
}
|
|
1147
1242
|
}
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1243
|
+
const hasConst = isObject(schema) && CONST_KEY in schema;
|
|
1244
|
+
if (hasConst === false) {
|
|
1245
|
+
if (neverPopulate) {
|
|
1246
|
+
return defaults ?? emptyDefault;
|
|
1247
|
+
}
|
|
1248
|
+
if (ignoreMinItemsFlagSet && !required) {
|
|
1249
|
+
return defaults ? defaults : void 0;
|
|
1250
|
+
}
|
|
1153
1251
|
}
|
|
1154
1252
|
const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
|
|
1155
1253
|
if (!schema.minItems || isMultiSelect(validator, schema, rootSchema) || computeSkipPopulate(validator, schema, rootSchema) || schema.minItems <= defaultsLength) {
|
|
@@ -1179,26 +1277,29 @@
|
|
|
1179
1277
|
}
|
|
1180
1278
|
}
|
|
1181
1279
|
}
|
|
1182
|
-
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues = false, experimental_defaultFormStateBehavior) {
|
|
1280
|
+
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues = false, experimental_defaultFormStateBehavior, experimental_customMergeAllOf) {
|
|
1183
1281
|
if (!isObject(theSchema)) {
|
|
1184
1282
|
throw new Error("Invalid schema: " + theSchema);
|
|
1185
1283
|
}
|
|
1186
|
-
const schema = retrieveSchema(validator, theSchema, rootSchema, formData);
|
|
1284
|
+
const schema = retrieveSchema(validator, theSchema, rootSchema, formData, experimental_customMergeAllOf);
|
|
1187
1285
|
const defaults = computeDefaults(validator, schema, {
|
|
1188
1286
|
rootSchema,
|
|
1189
1287
|
includeUndefinedValues,
|
|
1190
1288
|
experimental_defaultFormStateBehavior,
|
|
1289
|
+
experimental_customMergeAllOf,
|
|
1191
1290
|
rawFormData: formData
|
|
1192
1291
|
});
|
|
1193
1292
|
if (formData === void 0 || formData === null || typeof formData === "number" && isNaN(formData)) {
|
|
1194
1293
|
return defaults;
|
|
1195
1294
|
}
|
|
1196
|
-
const {
|
|
1295
|
+
const { mergeDefaultsIntoFormData, arrayMinItems = {} } = experimental_defaultFormStateBehavior || {};
|
|
1296
|
+
const { mergeExtraDefaults } = arrayMinItems;
|
|
1297
|
+
const defaultSupercedesUndefined = mergeDefaultsIntoFormData === "useDefaultIfFormDataUndefined";
|
|
1197
1298
|
if (isObject(formData)) {
|
|
1198
|
-
return mergeDefaultsWithFormData(defaults, formData, mergeExtraDefaults);
|
|
1299
|
+
return mergeDefaultsWithFormData(defaults, formData, mergeExtraDefaults, defaultSupercedesUndefined);
|
|
1199
1300
|
}
|
|
1200
1301
|
if (Array.isArray(formData)) {
|
|
1201
|
-
return mergeDefaultsWithFormData(defaults, formData, mergeExtraDefaults);
|
|
1302
|
+
return mergeDefaultsWithFormData(defaults, formData, mergeExtraDefaults, defaultSupercedesUndefined);
|
|
1202
1303
|
}
|
|
1203
1304
|
return formData;
|
|
1204
1305
|
}
|
|
@@ -1360,7 +1461,7 @@
|
|
|
1360
1461
|
}
|
|
1361
1462
|
return newFormData;
|
|
1362
1463
|
}
|
|
1363
|
-
function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList = []) {
|
|
1464
|
+
function toIdSchemaInternal(validator, schema, idPrefix, idSeparator, id, rootSchema, formData, _recurseList = [], experimental_customMergeAllOf) {
|
|
1364
1465
|
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
|
|
1365
1466
|
const _schema = retrieveSchema(validator, schema, rootSchema, formData);
|
|
1366
1467
|
const sameSchemaIndex = _recurseList.findIndex((item) => isEqual4(item, _schema));
|
|
@@ -1373,8 +1474,7 @@
|
|
|
1373
1474
|
id,
|
|
1374
1475
|
rootSchema,
|
|
1375
1476
|
formData,
|
|
1376
|
-
_recurseList.concat(_schema)
|
|
1377
|
-
);
|
|
1477
|
+
_recurseList.concat(_schema));
|
|
1378
1478
|
}
|
|
1379
1479
|
}
|
|
1380
1480
|
if (ITEMS_KEY in schema && !get8(schema, [ITEMS_KEY, REF_KEY])) {
|
|
@@ -1386,8 +1486,7 @@
|
|
|
1386
1486
|
id,
|
|
1387
1487
|
rootSchema,
|
|
1388
1488
|
formData,
|
|
1389
|
-
_recurseList
|
|
1390
|
-
);
|
|
1489
|
+
_recurseList);
|
|
1391
1490
|
}
|
|
1392
1491
|
const $id = id || idPrefix;
|
|
1393
1492
|
const idSchema = { $id };
|
|
@@ -1405,14 +1504,21 @@
|
|
|
1405
1504
|
// It's possible that formData is not an object -- this can happen if an
|
|
1406
1505
|
// array item has just been added, but not populated with data yet
|
|
1407
1506
|
get8(formData, [name]),
|
|
1408
|
-
_recurseList
|
|
1409
|
-
);
|
|
1507
|
+
_recurseList);
|
|
1410
1508
|
}
|
|
1411
1509
|
}
|
|
1412
1510
|
return idSchema;
|
|
1413
1511
|
}
|
|
1414
|
-
function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix = "root", idSeparator = "_") {
|
|
1415
|
-
return toIdSchemaInternal(
|
|
1512
|
+
function toIdSchema(validator, schema, id, rootSchema, formData, idPrefix = "root", idSeparator = "_", experimental_customMergeAllOf) {
|
|
1513
|
+
return toIdSchemaInternal(
|
|
1514
|
+
validator,
|
|
1515
|
+
schema,
|
|
1516
|
+
idPrefix,
|
|
1517
|
+
idSeparator,
|
|
1518
|
+
id,
|
|
1519
|
+
rootSchema,
|
|
1520
|
+
formData,
|
|
1521
|
+
void 0);
|
|
1416
1522
|
}
|
|
1417
1523
|
function toPathSchemaInternal(validator, schema, name, rootSchema, formData, _recurseList = []) {
|
|
1418
1524
|
if (REF_KEY in schema || DEPENDENCIES_KEY in schema || ALL_OF_KEY in schema) {
|
|
@@ -1511,11 +1617,13 @@
|
|
|
1511
1617
|
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
1512
1618
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
1513
1619
|
* @param experimental_defaultFormStateBehavior - Configuration flags to allow users to override default form state behavior
|
|
1620
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
1514
1621
|
*/
|
|
1515
|
-
constructor(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
1622
|
+
constructor(validator, rootSchema, experimental_defaultFormStateBehavior, experimental_customMergeAllOf) {
|
|
1516
1623
|
this.rootSchema = rootSchema;
|
|
1517
1624
|
this.validator = validator;
|
|
1518
1625
|
this.experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior;
|
|
1626
|
+
this.experimental_customMergeAllOf = experimental_customMergeAllOf;
|
|
1519
1627
|
}
|
|
1520
1628
|
/** Returns the `ValidatorType` in the `SchemaUtilsType`
|
|
1521
1629
|
*
|
|
@@ -1531,13 +1639,14 @@
|
|
|
1531
1639
|
* @param validator - An implementation of the `ValidatorType` interface that will be compared against the current one
|
|
1532
1640
|
* @param rootSchema - The root schema that will be compared against the current one
|
|
1533
1641
|
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1642
|
+
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
|
|
1534
1643
|
* @returns - True if the `SchemaUtilsType` differs from the given `validator` or `rootSchema`
|
|
1535
1644
|
*/
|
|
1536
|
-
doesSchemaUtilsDiffer(validator, rootSchema, experimental_defaultFormStateBehavior = {}) {
|
|
1645
|
+
doesSchemaUtilsDiffer(validator, rootSchema, experimental_defaultFormStateBehavior = {}, experimental_customMergeAllOf) {
|
|
1537
1646
|
if (!validator || !rootSchema) {
|
|
1538
1647
|
return false;
|
|
1539
1648
|
}
|
|
1540
|
-
return this.validator !== validator || !deepEquals(this.rootSchema, rootSchema) || !deepEquals(this.experimental_defaultFormStateBehavior, experimental_defaultFormStateBehavior);
|
|
1649
|
+
return this.validator !== validator || !deepEquals(this.rootSchema, rootSchema) || !deepEquals(this.experimental_defaultFormStateBehavior, experimental_defaultFormStateBehavior) || this.experimental_customMergeAllOf !== experimental_customMergeAllOf;
|
|
1541
1650
|
}
|
|
1542
1651
|
/** Returns the superset of `formData` that includes the given set updated to include any missing fields that have
|
|
1543
1652
|
* computed to have defaults provided in the `schema`.
|
|
@@ -1556,7 +1665,8 @@
|
|
|
1556
1665
|
formData,
|
|
1557
1666
|
this.rootSchema,
|
|
1558
1667
|
includeUndefinedValues,
|
|
1559
|
-
this.experimental_defaultFormStateBehavior
|
|
1668
|
+
this.experimental_defaultFormStateBehavior,
|
|
1669
|
+
this.experimental_customMergeAllOf
|
|
1560
1670
|
);
|
|
1561
1671
|
}
|
|
1562
1672
|
/** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
|
|
@@ -1666,7 +1776,13 @@
|
|
|
1666
1776
|
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
|
|
1667
1777
|
*/
|
|
1668
1778
|
retrieveSchema(schema, rawFormData) {
|
|
1669
|
-
return retrieveSchema(
|
|
1779
|
+
return retrieveSchema(
|
|
1780
|
+
this.validator,
|
|
1781
|
+
schema,
|
|
1782
|
+
this.rootSchema,
|
|
1783
|
+
rawFormData,
|
|
1784
|
+
this.experimental_customMergeAllOf
|
|
1785
|
+
);
|
|
1670
1786
|
}
|
|
1671
1787
|
/** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
|
|
1672
1788
|
* new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
|
|
@@ -1692,7 +1808,16 @@
|
|
|
1692
1808
|
* @returns - The `IdSchema` object for the `schema`
|
|
1693
1809
|
*/
|
|
1694
1810
|
toIdSchema(schema, id, formData, idPrefix = "root", idSeparator = "_") {
|
|
1695
|
-
return toIdSchema(
|
|
1811
|
+
return toIdSchema(
|
|
1812
|
+
this.validator,
|
|
1813
|
+
schema,
|
|
1814
|
+
id,
|
|
1815
|
+
this.rootSchema,
|
|
1816
|
+
formData,
|
|
1817
|
+
idPrefix,
|
|
1818
|
+
idSeparator,
|
|
1819
|
+
this.experimental_customMergeAllOf
|
|
1820
|
+
);
|
|
1696
1821
|
}
|
|
1697
1822
|
/** Generates an `PathSchema` object for the `schema`, recursively
|
|
1698
1823
|
*
|
|
@@ -1705,8 +1830,13 @@
|
|
|
1705
1830
|
return toPathSchema(this.validator, schema, name, this.rootSchema, formData);
|
|
1706
1831
|
}
|
|
1707
1832
|
};
|
|
1708
|
-
function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior = {}) {
|
|
1709
|
-
return new SchemaUtils(
|
|
1833
|
+
function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior = {}, experimental_customMergeAllOf) {
|
|
1834
|
+
return new SchemaUtils(
|
|
1835
|
+
validator,
|
|
1836
|
+
rootSchema,
|
|
1837
|
+
experimental_defaultFormStateBehavior,
|
|
1838
|
+
experimental_customMergeAllOf
|
|
1839
|
+
);
|
|
1710
1840
|
}
|
|
1711
1841
|
|
|
1712
1842
|
// src/dataURItoBlob.ts
|
|
@@ -1860,7 +1990,7 @@
|
|
|
1860
1990
|
let errorBlock = hasPath ? get8(this.errorSchema, pathOfError) : this.errorSchema;
|
|
1861
1991
|
if (!errorBlock && pathOfError) {
|
|
1862
1992
|
errorBlock = {};
|
|
1863
|
-
|
|
1993
|
+
setWith(this.errorSchema, pathOfError, errorBlock, Object);
|
|
1864
1994
|
}
|
|
1865
1995
|
return errorBlock;
|
|
1866
1996
|
}
|
|
@@ -39,7 +39,7 @@ export default class ErrorSchemaBuilder<T = any> {
|
|
|
39
39
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to add the error(s)
|
|
40
40
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
41
41
|
*/
|
|
42
|
-
addErrors(errorOrList: string | string[], pathOfError?: string | string[]): this;
|
|
42
|
+
addErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]): this;
|
|
43
43
|
/** Sets/replaces the `errorOrList` as the error(s) in the `ErrorSchema` at either the root level or the location
|
|
44
44
|
* within the schema described by the `pathOfError`. For more information about how to specify the path see the
|
|
45
45
|
* [eslint lodash plugin docs](https://github.com/wix/eslint-plugin-lodash/blob/master/docs/rules/path-style.md).
|
|
@@ -48,7 +48,7 @@ export default class ErrorSchemaBuilder<T = any> {
|
|
|
48
48
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to set the error(s)
|
|
49
49
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
50
50
|
*/
|
|
51
|
-
setErrors(errorOrList: string | string[], pathOfError?: string | string[]): this;
|
|
51
|
+
setErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]): this;
|
|
52
52
|
/** Clears the error(s) in the `ErrorSchema` at either the root level or the location within the schema described by
|
|
53
53
|
* the `pathOfError`. For more information about how to specify the path see the
|
|
54
54
|
* [eslint lodash plugin docs](https://github.com/wix/eslint-plugin-lodash/blob/master/docs/rules/path-style.md).
|
|
@@ -56,5 +56,5 @@ export default class ErrorSchemaBuilder<T = any> {
|
|
|
56
56
|
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to clear the error(s)
|
|
57
57
|
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
|
|
58
58
|
*/
|
|
59
|
-
clearErrors(pathOfError?: string | string[]): this;
|
|
59
|
+
clearErrors(pathOfError?: string | (string | number)[]): this;
|
|
60
60
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import cloneDeep from 'lodash/cloneDeep';
|
|
2
2
|
import get from 'lodash/get';
|
|
3
3
|
import set from 'lodash/set';
|
|
4
|
+
import setWith from 'lodash/setWith';
|
|
4
5
|
import { ERRORS_KEY } from './constants';
|
|
5
6
|
/** The `ErrorSchemaBuilder<T>` is used to build an `ErrorSchema<T>` since the definition of the `ErrorSchema` type is
|
|
6
7
|
* designed for reading information rather than writing it. Use this class to add, replace or clear errors in an error
|
|
@@ -36,7 +37,7 @@ export default class ErrorSchemaBuilder {
|
|
|
36
37
|
let errorBlock = hasPath ? get(this.errorSchema, pathOfError) : this.errorSchema;
|
|
37
38
|
if (!errorBlock && pathOfError) {
|
|
38
39
|
errorBlock = {};
|
|
39
|
-
|
|
40
|
+
setWith(this.errorSchema, pathOfError, errorBlock, Object);
|
|
40
41
|
}
|
|
41
42
|
return errorBlock;
|
|
42
43
|
}
|