@rjsf/utils 5.6.2 → 5.7.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.d.ts +46 -5
- package/dist/utils.cjs.development.js +656 -213
- package/dist/utils.cjs.development.js.map +1 -1
- package/dist/utils.cjs.production.min.js +1 -1
- package/dist/utils.cjs.production.min.js.map +1 -1
- package/dist/utils.esm.js +652 -214
- package/dist/utils.esm.js.map +1 -1
- package/dist/utils.umd.development.js +659 -217
- package/dist/utils.umd.development.js.map +1 -1
- package/dist/utils.umd.production.min.js +1 -1
- package/dist/utils.umd.production.min.js.map +1 -1
- package/package.json +9 -9
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash-es/isPlainObject'), require('lodash-es/isEqualWith'), require('lodash-es/get'), require('lodash-es/isEmpty'), require('jsonpointer'), require('lodash-es/omit'), require('lodash-es/has'), require('lodash-es/isObject'), require('lodash-es/isString'), require('lodash-es/reduce'), require('lodash-es/times'), require('lodash-es/set'), require('json-schema-merge-allof'), require('lodash-es/union'), require('lodash-es/isEqual'), require('lodash-es/cloneDeep'), require('react/jsx-runtime'), require('react'), require('react-is'), require('lodash-es/toPath')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'lodash-es/isPlainObject', 'lodash-es/isEqualWith', 'lodash-es/get', 'lodash-es/isEmpty', 'jsonpointer', 'lodash-es/omit', 'lodash-es/has', 'lodash-es/isObject', 'lodash-es/isString', 'lodash-es/reduce', 'lodash-es/times', 'lodash-es/set', 'json-schema-merge-allof', 'lodash-es/union', 'lodash-es/isEqual', 'lodash-es/cloneDeep', 'react/jsx-runtime', 'react', 'react-is', 'lodash-es/toPath'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/utils"] = {}, global.isPlainObject, global.isEqualWith, global.get, global.isEmpty, global.jsonpointer, global.omit, global.has, global.isObject$1, global.isString, global.reduce, global.times, global.set, global.mergeAllOf, global.union, global.isEqual, global.cloneDeep, global.jsxRuntime, global.React, global.ReactIs, global.toPath));
|
|
5
|
-
})(this, (function (exports, isPlainObject, isEqualWith, get, isEmpty, jsonpointer, omit, has, isObject$1, isString, reduce, times, set, mergeAllOf, union, isEqual, cloneDeep, jsxRuntime, react, ReactIs, toPath) { 'use strict';
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('lodash-es/isPlainObject'), require('lodash-es/isEqualWith'), require('lodash-es/get'), require('lodash-es/isEmpty'), require('jsonpointer'), require('lodash-es/omit'), require('lodash-es/has'), require('lodash-es/isObject'), require('lodash-es/isString'), require('lodash-es/reduce'), require('lodash-es/times'), require('lodash-es/set'), require('json-schema-merge-allof'), require('lodash-es/union'), require('lodash-es/isEqual'), require('lodash-es/cloneDeep'), require('react/jsx-runtime'), require('react'), require('react-is'), require('lodash-es/toPath'), require('lodash-es/forEach')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'lodash-es/isPlainObject', 'lodash-es/isEqualWith', 'lodash-es/get', 'lodash-es/isEmpty', 'jsonpointer', 'lodash-es/omit', 'lodash-es/has', 'lodash-es/isObject', 'lodash-es/isString', 'lodash-es/reduce', 'lodash-es/times', 'lodash-es/set', 'json-schema-merge-allof', 'lodash-es/union', 'lodash-es/isEqual', 'lodash-es/cloneDeep', 'react/jsx-runtime', 'react', 'react-is', 'lodash-es/toPath', 'lodash-es/forEach'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@rjsf/utils"] = {}, global.isPlainObject, global.isEqualWith, global.get, global.isEmpty, global.jsonpointer, global.omit, global.has, global.isObject$1, global.isString, global.reduce, global.times, global.set, global.mergeAllOf, global.union, global.isEqual, global.cloneDeep, global.jsxRuntime, global.React, global.ReactIs, global.toPath, global.forEach));
|
|
5
|
+
})(this, (function (exports, isPlainObject, isEqualWith, get, isEmpty, jsonpointer, omit, has, isObject$1, isString, reduce, times, set, mergeAllOf, union, isEqual, cloneDeep, jsxRuntime, react, ReactIs, toPath, forEach) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
8
|
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);
|
|
25
25
|
var ReactIs__default = /*#__PURE__*/_interopDefaultLegacy(ReactIs);
|
|
26
26
|
var toPath__default = /*#__PURE__*/_interopDefaultLegacy(toPath);
|
|
27
|
+
var forEach__default = /*#__PURE__*/_interopDefaultLegacy(forEach);
|
|
27
28
|
|
|
28
29
|
/** Determines whether a `thing` is an object for the purposes of RSJF. In this case, `thing` is an object if it has
|
|
29
30
|
* the type `object` but is NOT null, an array or a File.
|
|
@@ -167,6 +168,7 @@
|
|
|
167
168
|
var ENUM_KEY = 'enum';
|
|
168
169
|
var ERRORS_KEY = '__errors';
|
|
169
170
|
var ID_KEY = '$id';
|
|
171
|
+
var IF_KEY = 'if';
|
|
170
172
|
var ITEMS_KEY = 'items';
|
|
171
173
|
var NAME_KEY = '$name';
|
|
172
174
|
var ONE_OF_KEY = 'oneOf';
|
|
@@ -424,6 +426,23 @@
|
|
|
424
426
|
return getMatchingOption(validator, formData, options, rootSchema, discriminatorField);
|
|
425
427
|
}
|
|
426
428
|
|
|
429
|
+
/** Returns the `discriminator.propertyName` when defined in the `schema` if it is a string. A warning is generated when
|
|
430
|
+
* it is not a string. Returns `undefined` when a valid discriminator is not present.
|
|
431
|
+
*
|
|
432
|
+
* @param schema - The schema from which the discriminator is potentially obtained
|
|
433
|
+
* @returns - The `discriminator.propertyName` if it exists in the schema, otherwise `undefined`
|
|
434
|
+
*/
|
|
435
|
+
function getDiscriminatorFieldFromSchema(schema) {
|
|
436
|
+
var discriminator;
|
|
437
|
+
var maybeString = get__default["default"](schema, 'discriminator.propertyName', undefined);
|
|
438
|
+
if (isString__default["default"](maybeString)) {
|
|
439
|
+
discriminator = maybeString;
|
|
440
|
+
} else if (maybeString !== undefined) {
|
|
441
|
+
console.warn("Expecting discriminator to be a string, got \"" + typeof maybeString + "\" instead");
|
|
442
|
+
}
|
|
443
|
+
return discriminator;
|
|
444
|
+
}
|
|
445
|
+
|
|
427
446
|
/** Given a specific `value` attempts to guess the type of a schema element. In the case where we have to implicitly
|
|
428
447
|
* create a schema, it is useful to know what type to use based on the data we are defining.
|
|
429
448
|
*
|
|
@@ -513,82 +532,164 @@
|
|
|
513
532
|
_excluded3 = ["allOf"],
|
|
514
533
|
_excluded4 = ["dependencies"],
|
|
515
534
|
_excluded5 = ["oneOf"];
|
|
535
|
+
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
|
|
536
|
+
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
|
|
537
|
+
* potentially recursive resolution.
|
|
538
|
+
*
|
|
539
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
540
|
+
* @param schema - The schema for which retrieving a schema is desired
|
|
541
|
+
* @param [rootSchema={}] - The root schema that will be forwarded to all the APIs
|
|
542
|
+
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
543
|
+
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
|
|
544
|
+
*/
|
|
545
|
+
function retrieveSchema(validator, schema, rootSchema, rawFormData) {
|
|
546
|
+
if (rootSchema === void 0) {
|
|
547
|
+
rootSchema = {};
|
|
548
|
+
}
|
|
549
|
+
return retrieveSchemaInternal(validator, schema, rootSchema, rawFormData)[0];
|
|
550
|
+
}
|
|
516
551
|
/** Resolves a conditional block (if/else/then) by removing the condition and merging the appropriate conditional branch
|
|
517
|
-
* with the rest of the schema
|
|
552
|
+
* with the rest of the schema. If `expandAllBranches` is true, then the `retrieveSchemaInteral()` results for both
|
|
553
|
+
* conditions will be returned.
|
|
518
554
|
*
|
|
519
|
-
* @param validator - An implementation of the `ValidatorType
|
|
555
|
+
* @param validator - An implementation of the `ValidatorType` interface that is used to detect valid schema conditions
|
|
520
556
|
* @param schema - The schema for which resolving a condition is desired
|
|
521
557
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
558
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and
|
|
559
|
+
* dependencies as a list of schemas
|
|
522
560
|
* @param [formData] - The current formData to assist retrieving a schema
|
|
523
|
-
* @returns - A
|
|
561
|
+
* @returns - A list of schemas with the appropriate conditions resolved, possibly with all branches expanded
|
|
524
562
|
*/
|
|
525
|
-
function resolveCondition(validator, schema, rootSchema, formData) {
|
|
563
|
+
function resolveCondition(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
526
564
|
var expression = schema["if"],
|
|
527
565
|
then = schema.then,
|
|
528
566
|
otherwise = schema["else"],
|
|
529
567
|
resolvedSchemaLessConditional = _objectWithoutPropertiesLoose(schema, _excluded$1);
|
|
530
|
-
var
|
|
531
|
-
|
|
532
|
-
|
|
568
|
+
var conditionValue = validator.isValid(expression, formData, rootSchema);
|
|
569
|
+
var resolvedSchemas = [resolvedSchemaLessConditional];
|
|
570
|
+
var schemas = [];
|
|
571
|
+
if (expandAllBranches) {
|
|
572
|
+
if (then && typeof then !== 'boolean') {
|
|
573
|
+
schemas = schemas.concat(retrieveSchemaInternal(validator, then, rootSchema, formData, expandAllBranches));
|
|
574
|
+
}
|
|
575
|
+
if (otherwise && typeof otherwise !== 'boolean') {
|
|
576
|
+
schemas = schemas.concat(retrieveSchemaInternal(validator, otherwise, rootSchema, formData, expandAllBranches));
|
|
577
|
+
}
|
|
578
|
+
} else {
|
|
579
|
+
var conditionalSchema = conditionValue ? then : otherwise;
|
|
580
|
+
if (conditionalSchema && typeof conditionalSchema !== 'boolean') {
|
|
581
|
+
schemas = schemas.concat(retrieveSchemaInternal(validator, conditionalSchema, rootSchema, formData, expandAllBranches));
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
if (schemas.length) {
|
|
585
|
+
resolvedSchemas = schemas.map(function (s) {
|
|
586
|
+
return mergeSchemas(resolvedSchemaLessConditional, s);
|
|
587
|
+
});
|
|
533
588
|
}
|
|
534
|
-
return
|
|
589
|
+
return resolvedSchemas.flatMap(function (s) {
|
|
590
|
+
return retrieveSchemaInternal(validator, s, rootSchema, formData, expandAllBranches);
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
/** Given a list of lists of allOf, anyOf or oneOf values, create a list of lists of all permutations of the values. The
|
|
594
|
+
* `listOfLists` is expected to be all resolved values of the 1st...nth schemas within an `allOf`, `anyOf` or `oneOf`.
|
|
595
|
+
* From those lists, build a matrix for each `xxxOf` where there is more than one schema for a row in the list of lists.
|
|
596
|
+
*
|
|
597
|
+
* For example:
|
|
598
|
+
* - If there are three xxxOf rows (A, B, C) and they have been resolved such that there is only one A, two B and three
|
|
599
|
+
* C schemas then:
|
|
600
|
+
* - The permutation for the first row is `[[A]]`
|
|
601
|
+
* - The permutations for the second row are `[[A,B1], [A,B2]]`
|
|
602
|
+
* - The permutations for the third row are `[[A,B1,C1], [A,B1,C2], [A,B1,C3], [A,B2,C1], [A,B2,C2], [A,B2,C3]]`
|
|
603
|
+
*
|
|
604
|
+
* @param listOfLists - The list of lists of elements that represent the allOf, anyOf or oneOf resolved values in order
|
|
605
|
+
* @returns - The list of all permutations of schemas for a set of `xxxOf`s
|
|
606
|
+
*/
|
|
607
|
+
function getAllPermutationsOfXxxOf(listOfLists) {
|
|
608
|
+
var allPermutations = listOfLists.reduce(function (permutations, list) {
|
|
609
|
+
// When there are more than one set of schemas for a row, duplicate the set of permutations and add in the values
|
|
610
|
+
if (list.length > 1) {
|
|
611
|
+
return list.flatMap(function (element) {
|
|
612
|
+
return times__default["default"](permutations.length, function (i) {
|
|
613
|
+
return [].concat(permutations[i]).concat(element);
|
|
614
|
+
});
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
// Otherwise just push in the single value into the current set of permutations
|
|
618
|
+
permutations.forEach(function (permutation) {
|
|
619
|
+
return permutation.push(list[0]);
|
|
620
|
+
});
|
|
621
|
+
return permutations;
|
|
622
|
+
}, [[]] // Start with an empty list
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
return allPermutations;
|
|
535
626
|
}
|
|
536
|
-
/** Resolves references and dependencies within a schema and its 'allOf' children.
|
|
537
|
-
*
|
|
627
|
+
/** Resolves references and dependencies within a schema and its 'allOf' children. Passes the `expandAllBranches` flag
|
|
628
|
+
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `resolveDependencies()` helper calls. If
|
|
629
|
+
* `expandAllBranches` is true, then all possible dependencies and/or allOf branches are returned.
|
|
538
630
|
*
|
|
539
|
-
* @param validator - An implementation of the `ValidatorType
|
|
631
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
540
632
|
* @param schema - The schema for which resolving a schema is desired
|
|
541
|
-
* @param
|
|
633
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
634
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
635
|
+
* as a list of schemas
|
|
542
636
|
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
543
|
-
* @returns - The
|
|
637
|
+
* @returns - The list of schemas having its references, dependencies and allOf schemas resolved
|
|
544
638
|
*/
|
|
545
|
-
function resolveSchema(validator, schema, rootSchema, formData) {
|
|
546
|
-
if (rootSchema === void 0) {
|
|
547
|
-
rootSchema = {};
|
|
548
|
-
}
|
|
639
|
+
function resolveSchema(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
549
640
|
if (REF_KEY in schema) {
|
|
550
|
-
return resolveReference(validator, schema, rootSchema, formData);
|
|
641
|
+
return resolveReference(validator, schema, rootSchema, expandAllBranches, formData);
|
|
551
642
|
}
|
|
552
643
|
if (DEPENDENCIES_KEY in schema) {
|
|
553
|
-
var
|
|
554
|
-
return
|
|
644
|
+
var resolvedSchemas = resolveDependencies(validator, schema, rootSchema, expandAllBranches, formData);
|
|
645
|
+
return resolvedSchemas.flatMap(function (s) {
|
|
646
|
+
return retrieveSchemaInternal(validator, s, rootSchema, formData, expandAllBranches);
|
|
647
|
+
});
|
|
555
648
|
}
|
|
556
|
-
if (ALL_OF_KEY in schema) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
649
|
+
if (ALL_OF_KEY in schema && Array.isArray(schema.allOf)) {
|
|
650
|
+
var allOfSchemaElements = schema.allOf.map(function (allOfSubschema) {
|
|
651
|
+
return retrieveSchemaInternal(validator, allOfSubschema, rootSchema, formData, expandAllBranches);
|
|
652
|
+
});
|
|
653
|
+
var allPermutations = getAllPermutationsOfXxxOf(allOfSchemaElements);
|
|
654
|
+
return allPermutations.map(function (permutation) {
|
|
655
|
+
return _extends({}, schema, {
|
|
656
|
+
allOf: permutation
|
|
657
|
+
});
|
|
561
658
|
});
|
|
562
659
|
}
|
|
563
|
-
// No $ref or dependencies attribute found, returning the original schema.
|
|
564
|
-
return schema;
|
|
660
|
+
// No $ref or dependencies or allOf attribute was found, returning the original schema.
|
|
661
|
+
return [schema];
|
|
565
662
|
}
|
|
566
|
-
/** Resolves references within a schema and
|
|
663
|
+
/** Resolves references within a schema and then returns the `retrieveSchemaInternal()` of the resolved schema. Passes
|
|
664
|
+
* the `expandAllBranches` flag down to the `retrieveSchemaInternal()` helper call.
|
|
567
665
|
*
|
|
568
|
-
* @param validator - An implementation of the `ValidatorType
|
|
666
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
569
667
|
* @param schema - The schema for which resolving a reference is desired
|
|
570
668
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
669
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
670
|
+
* as a list of schemas
|
|
571
671
|
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
572
|
-
* @returns - The
|
|
672
|
+
* @returns - The list schemas retrieved after having all references resolved
|
|
573
673
|
*/
|
|
574
|
-
function resolveReference(validator, schema, rootSchema, formData) {
|
|
575
|
-
// Retrieve the referenced schema definition.
|
|
576
|
-
var $refSchema = findSchemaDefinition(schema.$ref, rootSchema);
|
|
674
|
+
function resolveReference(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
577
675
|
// Drop the $ref property of the source schema.
|
|
578
|
-
var
|
|
676
|
+
var $ref = schema.$ref,
|
|
677
|
+
localSchema = _objectWithoutPropertiesLoose(schema, _excluded2);
|
|
678
|
+
// Retrieve the referenced schema definition.
|
|
679
|
+
var refSchema = findSchemaDefinition($ref, rootSchema);
|
|
579
680
|
// Update referenced schema definition with local schema properties.
|
|
580
|
-
return
|
|
681
|
+
return retrieveSchemaInternal(validator, _extends({}, refSchema, localSchema), rootSchema, formData, expandAllBranches);
|
|
581
682
|
}
|
|
582
683
|
/** Creates new 'properties' items for each key in the `formData`
|
|
583
684
|
*
|
|
584
|
-
* @param validator - An implementation of the `ValidatorType
|
|
685
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
|
|
585
686
|
* @param theSchema - The schema for which the existing additional properties is desired
|
|
586
687
|
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param validator
|
|
587
688
|
* @param [aFormData] - The current formData, if any, to assist retrieving a schema
|
|
588
689
|
* @returns - The updated schema with additional properties stubbed
|
|
589
690
|
*/
|
|
590
691
|
function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFormData) {
|
|
591
|
-
// Clone the schema so we don't ruin the consumer's original
|
|
692
|
+
// Clone the schema so that we don't ruin the consumer's original
|
|
592
693
|
var schema = _extends({}, theSchema, {
|
|
593
694
|
properties: _extends({}, theSchema.properties)
|
|
594
695
|
});
|
|
@@ -628,98 +729,155 @@
|
|
|
628
729
|
});
|
|
629
730
|
return schema;
|
|
630
731
|
}
|
|
631
|
-
/**
|
|
632
|
-
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData`
|
|
633
|
-
* potentially recursive resolution.
|
|
732
|
+
/** Internal handler that retrieves an expanded schema that has had all of its conditions, additional properties,
|
|
733
|
+
* references and dependencies resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData`
|
|
734
|
+
* that is used to do the potentially recursive resolution. If `expandAllBranches` is true, then all possible branches
|
|
735
|
+
* of the schema and its references, conditions and dependencies are returned.
|
|
634
736
|
*
|
|
635
|
-
* @param validator - An implementation of the `ValidatorType
|
|
737
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
636
738
|
* @param schema - The schema for which retrieving a schema is desired
|
|
637
|
-
* @param
|
|
739
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
638
740
|
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
|
|
639
|
-
* @
|
|
741
|
+
* @param [expandAllBranches=false] - Flag, if true, will return all possible branches of conditions, any/oneOf and
|
|
742
|
+
* dependencies as a list of schemas
|
|
743
|
+
* @returns - The schema(s) resulting from having its conditions, additional properties, references and dependencies
|
|
744
|
+
* resolved. Multiple schemas may be returned if `expandAllBranches` is true.
|
|
640
745
|
*/
|
|
641
|
-
function
|
|
642
|
-
if (
|
|
643
|
-
|
|
746
|
+
function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expandAllBranches) {
|
|
747
|
+
if (expandAllBranches === void 0) {
|
|
748
|
+
expandAllBranches = false;
|
|
644
749
|
}
|
|
645
750
|
if (!isObject(schema)) {
|
|
646
|
-
return {};
|
|
751
|
+
return [{}];
|
|
647
752
|
}
|
|
648
|
-
var
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
if (ALL_OF_KEY in schema) {
|
|
654
|
-
try {
|
|
655
|
-
resolvedSchema = mergeAllOf__default["default"](resolvedSchema, {
|
|
656
|
-
deep: false
|
|
657
|
-
});
|
|
658
|
-
} catch (e) {
|
|
659
|
-
console.warn('could not merge subschemas in allOf:\n', e);
|
|
660
|
-
var _resolvedSchema = resolvedSchema,
|
|
661
|
-
resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
|
|
662
|
-
return resolvedSchemaWithoutAllOf;
|
|
753
|
+
var resolvedSchemas = resolveSchema(validator, schema, rootSchema, expandAllBranches, rawFormData);
|
|
754
|
+
return resolvedSchemas.flatMap(function (s) {
|
|
755
|
+
var resolvedSchema = s;
|
|
756
|
+
if (IF_KEY in resolvedSchema) {
|
|
757
|
+
return resolveCondition(validator, resolvedSchema, rootSchema, expandAllBranches, rawFormData);
|
|
663
758
|
}
|
|
759
|
+
if (ALL_OF_KEY in schema) {
|
|
760
|
+
try {
|
|
761
|
+
resolvedSchema = mergeAllOf__default["default"](s, {
|
|
762
|
+
deep: false
|
|
763
|
+
});
|
|
764
|
+
} catch (e) {
|
|
765
|
+
console.warn('could not merge subschemas in allOf:\n', e);
|
|
766
|
+
var _resolvedSchema = resolvedSchema,
|
|
767
|
+
resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
|
|
768
|
+
return resolvedSchemaWithoutAllOf;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
var hasAdditionalProperties = ADDITIONAL_PROPERTIES_KEY in resolvedSchema && resolvedSchema.additionalProperties !== false;
|
|
772
|
+
if (hasAdditionalProperties) {
|
|
773
|
+
return stubExistingAdditionalProperties(validator, resolvedSchema, rootSchema, rawFormData);
|
|
774
|
+
}
|
|
775
|
+
return resolvedSchema;
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
/** Resolves an `anyOf` or `oneOf` within a schema (if present) to the list of schemas returned from
|
|
779
|
+
* `retrieveSchemaInternal()` for the best matching option. If `expandAllBranches` is true, then a list of schemas for ALL
|
|
780
|
+
* options are retrieved and returned.
|
|
781
|
+
*
|
|
782
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
783
|
+
* @param schema - The schema for which retrieving a schema is desired
|
|
784
|
+
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
785
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
786
|
+
* as a list of schemas
|
|
787
|
+
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema, defaults to an empty object
|
|
788
|
+
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
789
|
+
*/
|
|
790
|
+
function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranches, rawFormData) {
|
|
791
|
+
var anyOrOneOf;
|
|
792
|
+
if (Array.isArray(schema.oneOf)) {
|
|
793
|
+
anyOrOneOf = schema.oneOf;
|
|
794
|
+
} else if (Array.isArray(schema.anyOf)) {
|
|
795
|
+
anyOrOneOf = schema.anyOf;
|
|
796
|
+
}
|
|
797
|
+
if (anyOrOneOf) {
|
|
798
|
+
// Ensure that during expand all branches we pass an object rather than undefined so that all options are interrogated
|
|
799
|
+
var formData = rawFormData === undefined && expandAllBranches ? {} : rawFormData;
|
|
800
|
+
var discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
801
|
+
anyOrOneOf = anyOrOneOf.map(function (s) {
|
|
802
|
+
if (REF_KEY in s) {
|
|
803
|
+
// For this ref situation, don't expand all branches and just pick the first/only schema result
|
|
804
|
+
return resolveReference(validator, s, rootSchema, false, formData)[0];
|
|
805
|
+
}
|
|
806
|
+
return s;
|
|
807
|
+
});
|
|
808
|
+
// Call this to trigger the set of isValid() calls that the schema parser will need
|
|
809
|
+
var option = getFirstMatchingOption(validator, formData, anyOrOneOf, rootSchema, discriminator);
|
|
810
|
+
if (expandAllBranches) {
|
|
811
|
+
return anyOrOneOf;
|
|
812
|
+
}
|
|
813
|
+
schema = anyOrOneOf[option];
|
|
664
814
|
}
|
|
665
|
-
|
|
666
|
-
if (hasAdditionalProperties) {
|
|
667
|
-
return stubExistingAdditionalProperties(validator, resolvedSchema, rootSchema, formData);
|
|
668
|
-
}
|
|
669
|
-
return resolvedSchema;
|
|
815
|
+
return [schema];
|
|
670
816
|
}
|
|
671
|
-
/** Resolves dependencies within a schema and its '
|
|
817
|
+
/** Resolves dependencies within a schema and its 'anyOf/oneOf' children. Passes the `expandAllBranches` flag down to
|
|
818
|
+
* the `resolveAnyOrOneOfSchema()` and `processDependencies()` helper calls.
|
|
672
819
|
*
|
|
673
|
-
* @param validator - An implementation of the `ValidatorType
|
|
820
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
674
821
|
* @param schema - The schema for which resolving a dependency is desired
|
|
675
822
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
823
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
824
|
+
* as a list of schemas
|
|
676
825
|
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
677
|
-
* @returns - The
|
|
826
|
+
* @returns - The list of schemas with their dependencies resolved
|
|
678
827
|
*/
|
|
679
|
-
function resolveDependencies(validator, schema, rootSchema, formData) {
|
|
828
|
+
function resolveDependencies(validator, schema, rootSchema, expandAllBranches, formData) {
|
|
680
829
|
// Drop the dependencies from the source schema.
|
|
681
830
|
var dependencies = schema.dependencies,
|
|
682
831
|
remainingSchema = _objectWithoutPropertiesLoose(schema, _excluded4);
|
|
683
|
-
var
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
}
|
|
687
|
-
resolvedSchema = resolvedSchema.anyOf[getFirstMatchingOption(validator, formData, resolvedSchema.anyOf, rootSchema)];
|
|
688
|
-
}
|
|
689
|
-
return processDependencies(validator, dependencies, resolvedSchema, rootSchema, formData);
|
|
832
|
+
var resolvedSchemas = resolveAnyOrOneOfSchemas(validator, remainingSchema, rootSchema, expandAllBranches, formData);
|
|
833
|
+
return resolvedSchemas.flatMap(function (resolvedSchema) {
|
|
834
|
+
return processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData);
|
|
835
|
+
});
|
|
690
836
|
}
|
|
691
|
-
/** Processes all the `dependencies` recursively into the `resolvedSchema` as needed
|
|
837
|
+
/** Processes all the `dependencies` recursively into the list of `resolvedSchema`s as needed. Passes the
|
|
838
|
+
* `expandAllBranches` flag down to the `withDependentSchema()` and the recursive `processDependencies()` helper calls.
|
|
692
839
|
*
|
|
693
|
-
* @param validator - An implementation of the `ValidatorType
|
|
840
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
694
841
|
* @param dependencies - The set of dependencies that needs to be processed
|
|
695
842
|
* @param resolvedSchema - The schema for which processing dependencies is desired
|
|
696
843
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
844
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
845
|
+
* as a list of schemas
|
|
697
846
|
* @param [formData] - The current formData, if any, to assist retrieving a schema
|
|
698
847
|
* @returns - The schema with the `dependencies` resolved into it
|
|
699
848
|
*/
|
|
700
|
-
function processDependencies(validator, dependencies, resolvedSchema, rootSchema, formData) {
|
|
701
|
-
var
|
|
849
|
+
function processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData) {
|
|
850
|
+
var schemas = [resolvedSchema];
|
|
702
851
|
// Process dependencies updating the local schema properties as appropriate.
|
|
703
|
-
|
|
852
|
+
var _loop = function _loop() {
|
|
704
853
|
// Skip this dependency if its trigger property is not present.
|
|
705
|
-
if (get__default["default"](formData, [dependencyKey]) === undefined) {
|
|
706
|
-
continue;
|
|
854
|
+
if (!expandAllBranches && get__default["default"](formData, [dependencyKey]) === undefined) {
|
|
855
|
+
return "continue";
|
|
707
856
|
}
|
|
708
857
|
// Skip this dependency if it is not included in the schema (such as when dependencyKey is itself a hidden dependency.)
|
|
709
|
-
if (
|
|
710
|
-
continue;
|
|
858
|
+
if (resolvedSchema.properties && !(dependencyKey in resolvedSchema.properties)) {
|
|
859
|
+
return "continue";
|
|
711
860
|
}
|
|
712
861
|
var _splitKeyElementFromO = splitKeyElementFromObject(dependencyKey, dependencies),
|
|
713
862
|
remainingDependencies = _splitKeyElementFromO[0],
|
|
714
863
|
dependencyValue = _splitKeyElementFromO[1];
|
|
715
864
|
if (Array.isArray(dependencyValue)) {
|
|
716
|
-
|
|
865
|
+
schemas[0] = withDependentProperties(resolvedSchema, dependencyValue);
|
|
717
866
|
} else if (isObject(dependencyValue)) {
|
|
718
|
-
|
|
867
|
+
schemas = withDependentSchema(validator, resolvedSchema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, formData);
|
|
719
868
|
}
|
|
720
|
-
return
|
|
869
|
+
return {
|
|
870
|
+
v: schemas.flatMap(function (schema) {
|
|
871
|
+
return processDependencies(validator, remainingDependencies, schema, rootSchema, expandAllBranches, formData);
|
|
872
|
+
})
|
|
873
|
+
};
|
|
874
|
+
};
|
|
875
|
+
for (var dependencyKey in dependencies) {
|
|
876
|
+
var _ret = _loop();
|
|
877
|
+
if (_ret === "continue") continue;
|
|
878
|
+
if (typeof _ret === "object") return _ret.v;
|
|
721
879
|
}
|
|
722
|
-
return
|
|
880
|
+
return schemas;
|
|
723
881
|
}
|
|
724
882
|
/** Updates a schema with additionally required properties added
|
|
725
883
|
*
|
|
@@ -736,45 +894,57 @@
|
|
|
736
894
|
required: required
|
|
737
895
|
});
|
|
738
896
|
}
|
|
739
|
-
/** Merges a dependent schema into the `schema` dealing with oneOfs and references
|
|
897
|
+
/** Merges a dependent schema into the `schema` dealing with oneOfs and references. Passes the `expandAllBranches` flag
|
|
898
|
+
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `withExactlyOneSubschema()` helper calls.
|
|
740
899
|
*
|
|
741
|
-
* @param validator - An implementation of the `ValidatorType
|
|
900
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
742
901
|
* @param schema - The schema for which resolving a dependent schema is desired
|
|
743
902
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
744
903
|
* @param dependencyKey - The key name of the dependency
|
|
745
904
|
* @param dependencyValue - The potentially dependent schema
|
|
746
|
-
* @param
|
|
747
|
-
*
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
if (typeof subschema === 'boolean' || !(REF_KEY in subschema)) {
|
|
761
|
-
return subschema;
|
|
905
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
906
|
+
* as a list of schemas
|
|
907
|
+
* @param [formData]- The current formData to assist retrieving a schema
|
|
908
|
+
* @returns - The list of schemas with the dependent schema resolved into them
|
|
909
|
+
*/
|
|
910
|
+
function withDependentSchema(validator, schema, rootSchema, dependencyKey, dependencyValue, expandAllBranches, formData) {
|
|
911
|
+
var dependentSchemas = retrieveSchemaInternal(validator, dependencyValue, rootSchema, formData, expandAllBranches);
|
|
912
|
+
return dependentSchemas.flatMap(function (dependent) {
|
|
913
|
+
var oneOf = dependent.oneOf,
|
|
914
|
+
dependentSchema = _objectWithoutPropertiesLoose(dependent, _excluded5);
|
|
915
|
+
schema = mergeSchemas(schema, dependentSchema);
|
|
916
|
+
// Since it does not contain oneOf, we return the original schema.
|
|
917
|
+
if (oneOf === undefined) {
|
|
918
|
+
return schema;
|
|
762
919
|
}
|
|
763
|
-
|
|
920
|
+
// Resolve $refs inside oneOf.
|
|
921
|
+
var resolvedOneOfs = oneOf.map(function (subschema) {
|
|
922
|
+
if (typeof subschema === 'boolean' || !(REF_KEY in subschema)) {
|
|
923
|
+
return [subschema];
|
|
924
|
+
}
|
|
925
|
+
return resolveReference(validator, subschema, rootSchema, expandAllBranches, formData);
|
|
926
|
+
});
|
|
927
|
+
var allPermutations = getAllPermutationsOfXxxOf(resolvedOneOfs);
|
|
928
|
+
return allPermutations.flatMap(function (resolvedOneOf) {
|
|
929
|
+
return withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, resolvedOneOf, expandAllBranches, formData);
|
|
930
|
+
});
|
|
764
931
|
});
|
|
765
|
-
return withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, resolvedOneOf, formData);
|
|
766
932
|
}
|
|
767
|
-
/** Returns a `schema` with the best choice from the `oneOf` options merged into it
|
|
933
|
+
/** Returns a list of `schema`s with the best choice from the `oneOf` options merged into it. If `expandAllBranches` is
|
|
934
|
+
* true, then a list of schemas for ALL options are retrieved and returned. Passes the `expandAllBranches` flag down to
|
|
935
|
+
* the `retrieveSchemaInternal()` helper call.
|
|
768
936
|
*
|
|
769
|
-
* @param validator - An implementation of the `ValidatorType
|
|
937
|
+
* @param validator - An implementation of the `ValidatorType` interface that will be used to validate oneOf options
|
|
770
938
|
* @param schema - The schema for which resolving a oneOf subschema is desired
|
|
771
939
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
772
940
|
* @param dependencyKey - The key name of the oneOf dependency
|
|
773
941
|
* @param oneOf - The list of schemas representing the oneOf options
|
|
942
|
+
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
|
|
943
|
+
* as a list of schemas
|
|
774
944
|
* @param [formData] - The current formData to assist retrieving a schema
|
|
775
|
-
* @returns
|
|
945
|
+
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
|
|
776
946
|
*/
|
|
777
|
-
function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, oneOf, formData) {
|
|
947
|
+
function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, oneOf, expandAllBranches, formData) {
|
|
778
948
|
var validSubschemas = oneOf.filter(function (subschema) {
|
|
779
949
|
if (typeof subschema === 'boolean' || !subschema || !subschema.properties) {
|
|
780
950
|
return false;
|
|
@@ -786,21 +956,26 @@
|
|
|
786
956
|
type: 'object',
|
|
787
957
|
properties: (_properties = {}, _properties[dependencyKey] = conditionPropertySchema, _properties)
|
|
788
958
|
};
|
|
789
|
-
return validator.isValid(conditionSchema, formData, rootSchema);
|
|
959
|
+
return validator.isValid(conditionSchema, formData, rootSchema) || expandAllBranches;
|
|
790
960
|
}
|
|
791
961
|
return false;
|
|
792
962
|
});
|
|
793
|
-
if (validSubschemas.length !== 1) {
|
|
963
|
+
if (!expandAllBranches && validSubschemas.length !== 1) {
|
|
794
964
|
console.warn("ignoring oneOf in dependencies because there isn't exactly one subschema that is valid");
|
|
795
|
-
return schema;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
965
|
+
return [schema];
|
|
966
|
+
}
|
|
967
|
+
return validSubschemas.flatMap(function (s) {
|
|
968
|
+
var subschema = s;
|
|
969
|
+
var _splitKeyElementFromO2 = splitKeyElementFromObject(dependencyKey, subschema.properties),
|
|
970
|
+
dependentSubschema = _splitKeyElementFromO2[0];
|
|
971
|
+
var dependentSchema = _extends({}, subschema, {
|
|
972
|
+
properties: dependentSubschema
|
|
973
|
+
});
|
|
974
|
+
var schemas = retrieveSchemaInternal(validator, dependentSchema, rootSchema, formData, expandAllBranches);
|
|
975
|
+
return schemas.map(function (s) {
|
|
976
|
+
return mergeSchemas(schema, s);
|
|
977
|
+
});
|
|
802
978
|
});
|
|
803
|
-
return mergeSchemas(schema, retrieveSchema(validator, dependentSchema, rootSchema, formData));
|
|
804
979
|
}
|
|
805
980
|
|
|
806
981
|
/** A junk option used to determine when the getFirstMatchingOption call really matches an option rather than returning
|
|
@@ -1124,10 +1299,11 @@
|
|
|
1124
1299
|
}
|
|
1125
1300
|
return {};
|
|
1126
1301
|
}
|
|
1127
|
-
/** Either add `computedDefault` at `key` into `obj` or not add it based on its value
|
|
1128
|
-
* `includeUndefinedValues
|
|
1129
|
-
*
|
|
1130
|
-
* non-empty-object
|
|
1302
|
+
/** Either add `computedDefault` at `key` into `obj` or not add it based on its value, the value of
|
|
1303
|
+
* `includeUndefinedValues`, the value of `emptyObjectFields` and if its parent field is required. Generally undefined
|
|
1304
|
+
* `computedDefault` values are added only when `includeUndefinedValues` is either true/"excludeObjectChildren". If `
|
|
1305
|
+
* includeUndefinedValues` is false and `emptyObjectFields` is not "skipDefaults", then non-undefined and non-empty-object
|
|
1306
|
+
* values will be added based on certain conditions.
|
|
1131
1307
|
*
|
|
1132
1308
|
* @param obj - The object into which the computed default may be added
|
|
1133
1309
|
* @param key - The key into the object at which the computed default may be added
|
|
@@ -1137,22 +1313,38 @@
|
|
|
1137
1313
|
* false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
|
|
1138
1314
|
* values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
|
|
1139
1315
|
* false when computing defaults for any nested object properties.
|
|
1316
|
+
* @param isParentRequired - The optional boolean that indicates whether the parent field is required
|
|
1140
1317
|
* @param requiredFields - The list of fields that are required
|
|
1318
|
+
* @param experimental_defaultFormStateBehavior - Optional configuration object, if provided, allows users to override
|
|
1319
|
+
* default form state behavior
|
|
1141
1320
|
*/
|
|
1142
|
-
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
|
|
1321
|
+
function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, isParentRequired, requiredFields, experimental_defaultFormStateBehavior) {
|
|
1143
1322
|
if (requiredFields === void 0) {
|
|
1144
1323
|
requiredFields = [];
|
|
1145
1324
|
}
|
|
1325
|
+
if (experimental_defaultFormStateBehavior === void 0) {
|
|
1326
|
+
experimental_defaultFormStateBehavior = {};
|
|
1327
|
+
}
|
|
1328
|
+
var _experimental_default = experimental_defaultFormStateBehavior,
|
|
1329
|
+
_experimental_default2 = _experimental_default.emptyObjectFields,
|
|
1330
|
+
emptyObjectFields = _experimental_default2 === void 0 ? 'populateAllDefaults' : _experimental_default2;
|
|
1146
1331
|
if (includeUndefinedValues) {
|
|
1147
1332
|
obj[key] = computedDefault;
|
|
1148
|
-
} else if (
|
|
1149
|
-
|
|
1150
|
-
|
|
1333
|
+
} else if (emptyObjectFields !== 'skipDefaults') {
|
|
1334
|
+
if (isObject(computedDefault)) {
|
|
1335
|
+
// Store computedDefault if it's a non-empty object(e.g. not {}) and satisfies certain conditions
|
|
1336
|
+
// Condition 1: If computedDefault is not empty or if the key is a required field
|
|
1337
|
+
// Condition 2: If the parent object is required or emptyObjectFields is not 'populateRequiredDefaults'
|
|
1338
|
+
if ((!isEmpty__default["default"](computedDefault) || requiredFields.includes(key)) && (isParentRequired || emptyObjectFields !== 'populateRequiredDefaults')) {
|
|
1339
|
+
obj[key] = computedDefault;
|
|
1340
|
+
}
|
|
1341
|
+
} else if (
|
|
1342
|
+
// Store computedDefault if it's a defined primitive (e.g., true) and satisfies certain conditions
|
|
1343
|
+
// Condition 1: computedDefault is not undefined
|
|
1344
|
+
// Condition 2: If emptyObjectFields is 'populateAllDefaults' or if the key is a required field
|
|
1345
|
+
computedDefault !== undefined && (emptyObjectFields === 'populateAllDefaults' || requiredFields.includes(key))) {
|
|
1151
1346
|
obj[key] = computedDefault;
|
|
1152
1347
|
}
|
|
1153
|
-
} else if (computedDefault !== undefined) {
|
|
1154
|
-
// Store computedDefault if it's a defined primitive (e.g. true)
|
|
1155
|
-
obj[key] = computedDefault;
|
|
1156
1348
|
}
|
|
1157
1349
|
}
|
|
1158
1350
|
/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
|
|
@@ -1160,29 +1352,39 @@
|
|
|
1160
1352
|
*
|
|
1161
1353
|
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
|
|
1162
1354
|
* @param rawSchema - The schema for which the default state is desired
|
|
1163
|
-
* @param [
|
|
1164
|
-
* @param [
|
|
1165
|
-
* @param [
|
|
1166
|
-
* @param [
|
|
1355
|
+
* @param [props] - Optional props for this function
|
|
1356
|
+
* @param [props.parentDefaults] - Any defaults provided by the parent field in the schema
|
|
1357
|
+
* @param [props.rootSchema] - The options root schema, used to primarily to look up `$ref`s
|
|
1358
|
+
* @param [props.rawFormData] - The current formData, if any, onto which to provide any missing defaults
|
|
1359
|
+
* @param [props.includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1167
1360
|
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1168
1361
|
* false when computing defaults for any nested object properties.
|
|
1169
|
-
* @param [_recurseList=[]] - The list of ref names currently being recursed, used to prevent infinite recursion
|
|
1362
|
+
* @param [props._recurseList=[]] - The list of ref names currently being recursed, used to prevent infinite recursion
|
|
1363
|
+
* @param [props.experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1364
|
+
* @param [props.required] - Optional flag, if true, indicates this schema was required in the parent schema.
|
|
1170
1365
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1171
1366
|
*/
|
|
1172
|
-
function computeDefaults(validator, rawSchema,
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
_recurseList =
|
|
1181
|
-
|
|
1367
|
+
function computeDefaults(validator, rawSchema, _temp) {
|
|
1368
|
+
var _ref = _temp === void 0 ? {} : _temp,
|
|
1369
|
+
parentDefaults = _ref.parentDefaults,
|
|
1370
|
+
rawFormData = _ref.rawFormData,
|
|
1371
|
+
_ref$rootSchema = _ref.rootSchema,
|
|
1372
|
+
rootSchema = _ref$rootSchema === void 0 ? {} : _ref$rootSchema,
|
|
1373
|
+
_ref$includeUndefined = _ref.includeUndefinedValues,
|
|
1374
|
+
includeUndefinedValues = _ref$includeUndefined === void 0 ? false : _ref$includeUndefined,
|
|
1375
|
+
_ref$_recurseList = _ref._recurseList,
|
|
1376
|
+
_recurseList = _ref$_recurseList === void 0 ? [] : _ref$_recurseList,
|
|
1377
|
+
_ref$experimental_def = _ref.experimental_defaultFormStateBehavior,
|
|
1378
|
+
experimental_defaultFormStateBehavior = _ref$experimental_def === void 0 ? undefined : _ref$experimental_def,
|
|
1379
|
+
_ref$required = _ref.required,
|
|
1380
|
+
required = _ref$required === void 0 ? false : _ref$required;
|
|
1182
1381
|
var formData = isObject(rawFormData) ? rawFormData : {};
|
|
1183
1382
|
var schema = isObject(rawSchema) ? rawSchema : {};
|
|
1184
1383
|
// Compute the defaults recursively: give highest priority to deepest nodes.
|
|
1185
1384
|
var defaults = parentDefaults;
|
|
1385
|
+
// If we get a new schema, then we need to recompute defaults again for the new schema found.
|
|
1386
|
+
var schemaToCompute = null;
|
|
1387
|
+
var updatedRecurseList = _recurseList;
|
|
1186
1388
|
if (isObject(defaults) && isObject(schema["default"])) {
|
|
1187
1389
|
// For object defaults, only override parent defaults that are defined in
|
|
1188
1390
|
// schema.default.
|
|
@@ -1193,40 +1395,68 @@
|
|
|
1193
1395
|
var refName = schema[REF_KEY];
|
|
1194
1396
|
// Use referenced schema defaults for this node.
|
|
1195
1397
|
if (!_recurseList.includes(refName)) {
|
|
1196
|
-
|
|
1197
|
-
|
|
1398
|
+
updatedRecurseList = _recurseList.concat(refName);
|
|
1399
|
+
schemaToCompute = findSchemaDefinition(refName, rootSchema);
|
|
1198
1400
|
}
|
|
1199
1401
|
} else if (DEPENDENCIES_KEY in schema) {
|
|
1200
|
-
var resolvedSchema = resolveDependencies(validator, schema, rootSchema, formData);
|
|
1201
|
-
|
|
1402
|
+
var resolvedSchema = resolveDependencies(validator, schema, rootSchema, false, formData);
|
|
1403
|
+
schemaToCompute = resolvedSchema[0]; // pick the first element from resolve dependencies
|
|
1202
1404
|
} else if (isFixedItems(schema)) {
|
|
1203
1405
|
defaults = schema.items.map(function (itemSchema, idx) {
|
|
1204
|
-
return computeDefaults(validator, itemSchema,
|
|
1406
|
+
return computeDefaults(validator, itemSchema, {
|
|
1407
|
+
rootSchema: rootSchema,
|
|
1408
|
+
includeUndefinedValues: includeUndefinedValues,
|
|
1409
|
+
_recurseList: _recurseList,
|
|
1410
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1411
|
+
parentDefaults: Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined,
|
|
1412
|
+
rawFormData: formData
|
|
1413
|
+
});
|
|
1205
1414
|
});
|
|
1206
1415
|
} else if (ONE_OF_KEY in schema) {
|
|
1207
1416
|
if (schema.oneOf.length === 0) {
|
|
1208
1417
|
return undefined;
|
|
1209
1418
|
}
|
|
1210
|
-
|
|
1419
|
+
var discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1420
|
+
schemaToCompute = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0, discriminator)];
|
|
1211
1421
|
} else if (ANY_OF_KEY in schema) {
|
|
1212
1422
|
if (schema.anyOf.length === 0) {
|
|
1213
1423
|
return undefined;
|
|
1214
1424
|
}
|
|
1215
|
-
|
|
1425
|
+
var _discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1426
|
+
schemaToCompute = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0, _discriminator)];
|
|
1427
|
+
}
|
|
1428
|
+
if (schemaToCompute) {
|
|
1429
|
+
return computeDefaults(validator, schemaToCompute, {
|
|
1430
|
+
rootSchema: rootSchema,
|
|
1431
|
+
includeUndefinedValues: includeUndefinedValues,
|
|
1432
|
+
_recurseList: updatedRecurseList,
|
|
1433
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1434
|
+
parentDefaults: defaults,
|
|
1435
|
+
rawFormData: formData
|
|
1436
|
+
});
|
|
1216
1437
|
}
|
|
1217
|
-
//
|
|
1218
|
-
if (
|
|
1438
|
+
// No defaults defined for this node, fallback to generic typed ones.
|
|
1439
|
+
if (defaults === undefined) {
|
|
1219
1440
|
defaults = schema["default"];
|
|
1220
1441
|
}
|
|
1221
1442
|
switch (getSchemaType(schema)) {
|
|
1222
|
-
// We need to
|
|
1443
|
+
// We need to recurse for object schema inner default values.
|
|
1223
1444
|
case 'object':
|
|
1224
1445
|
{
|
|
1225
1446
|
var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
|
|
1447
|
+
var _schema$required;
|
|
1226
1448
|
// Compute the defaults for this node, with the parent defaults we might
|
|
1227
1449
|
// have from a previous run: defaults[key].
|
|
1228
|
-
var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]),
|
|
1229
|
-
|
|
1450
|
+
var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), {
|
|
1451
|
+
rootSchema: rootSchema,
|
|
1452
|
+
_recurseList: _recurseList,
|
|
1453
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1454
|
+
includeUndefinedValues: includeUndefinedValues === true,
|
|
1455
|
+
parentDefaults: get__default["default"](defaults, [key]),
|
|
1456
|
+
rawFormData: get__default["default"](formData, [key]),
|
|
1457
|
+
required: (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(key)
|
|
1458
|
+
});
|
|
1459
|
+
maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, required, schema.required, experimental_defaultFormStateBehavior);
|
|
1230
1460
|
return acc;
|
|
1231
1461
|
}, {});
|
|
1232
1462
|
if (schema.additionalProperties) {
|
|
@@ -1240,49 +1470,82 @@
|
|
|
1240
1470
|
return keys.add(key);
|
|
1241
1471
|
});
|
|
1242
1472
|
}
|
|
1473
|
+
var formDataRequired;
|
|
1243
1474
|
if (isObject(formData)) {
|
|
1475
|
+
formDataRequired = [];
|
|
1244
1476
|
Object.keys(formData).filter(function (key) {
|
|
1245
1477
|
return !schema.properties || !schema.properties[key];
|
|
1246
1478
|
}).forEach(function (key) {
|
|
1247
|
-
|
|
1479
|
+
keys.add(key);
|
|
1480
|
+
formDataRequired.push(key);
|
|
1248
1481
|
});
|
|
1249
1482
|
}
|
|
1250
1483
|
keys.forEach(function (key) {
|
|
1251
|
-
var
|
|
1252
|
-
|
|
1484
|
+
var _schema$required2;
|
|
1485
|
+
var computedDefault = computeDefaults(validator, additionalPropertiesSchema, {
|
|
1486
|
+
rootSchema: rootSchema,
|
|
1487
|
+
_recurseList: _recurseList,
|
|
1488
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1489
|
+
includeUndefinedValues: includeUndefinedValues === true,
|
|
1490
|
+
parentDefaults: get__default["default"](defaults, [key]),
|
|
1491
|
+
rawFormData: get__default["default"](formData, [key]),
|
|
1492
|
+
required: (_schema$required2 = schema.required) === null || _schema$required2 === void 0 ? void 0 : _schema$required2.includes(key)
|
|
1493
|
+
});
|
|
1494
|
+
// Since these are additional properties we don’t need to add the `experimental_defaultFormStateBehavior` prop
|
|
1495
|
+
maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues, required, formDataRequired);
|
|
1253
1496
|
});
|
|
1254
1497
|
}
|
|
1255
1498
|
return objectDefaults;
|
|
1256
1499
|
}
|
|
1257
1500
|
case 'array':
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
});
|
|
1271
|
-
}
|
|
1272
|
-
if (schema.minItems) {
|
|
1273
|
-
if (!isMultiSelect(validator, schema, rootSchema)) {
|
|
1274
|
-
var defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
|
|
1275
|
-
if (schema.minItems > defaultsLength) {
|
|
1276
|
-
var defaultEntries = defaults || [];
|
|
1277
|
-
// populate the array with the defaults
|
|
1278
|
-
var fillerSchema = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Invert);
|
|
1279
|
-
var fillerDefault = fillerSchema["default"];
|
|
1280
|
-
var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, fillerDefault, rootSchema, undefined, undefined, _recurseList));
|
|
1281
|
-
// then fill up the rest with either the item default or empty, up to minItems
|
|
1282
|
-
return defaultEntries.concat(fillerEntries);
|
|
1283
|
-
}
|
|
1501
|
+
{
|
|
1502
|
+
// Inject defaults into existing array defaults
|
|
1503
|
+
if (Array.isArray(defaults)) {
|
|
1504
|
+
defaults = defaults.map(function (item, idx) {
|
|
1505
|
+
var schemaItem = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Fallback, idx);
|
|
1506
|
+
return computeDefaults(validator, schemaItem, {
|
|
1507
|
+
rootSchema: rootSchema,
|
|
1508
|
+
_recurseList: _recurseList,
|
|
1509
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1510
|
+
parentDefaults: item
|
|
1511
|
+
});
|
|
1512
|
+
});
|
|
1284
1513
|
}
|
|
1285
|
-
|
|
1514
|
+
// Deeply inject defaults into already existing form data
|
|
1515
|
+
if (Array.isArray(rawFormData)) {
|
|
1516
|
+
var schemaItem = getInnerSchemaForArrayItem(schema);
|
|
1517
|
+
defaults = rawFormData.map(function (item, idx) {
|
|
1518
|
+
return computeDefaults(validator, schemaItem, {
|
|
1519
|
+
rootSchema: rootSchema,
|
|
1520
|
+
_recurseList: _recurseList,
|
|
1521
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1522
|
+
rawFormData: item,
|
|
1523
|
+
parentDefaults: get__default["default"](defaults, [idx])
|
|
1524
|
+
});
|
|
1525
|
+
});
|
|
1526
|
+
}
|
|
1527
|
+
var ignoreMinItemsFlagSet = (experimental_defaultFormStateBehavior === null || experimental_defaultFormStateBehavior === void 0 ? void 0 : experimental_defaultFormStateBehavior.arrayMinItems) === 'requiredOnly';
|
|
1528
|
+
if (ignoreMinItemsFlagSet && !required) {
|
|
1529
|
+
// If no form data exists or defaults are set leave the field empty/non-existent, otherwise
|
|
1530
|
+
// return form data/defaults
|
|
1531
|
+
return defaults ? defaults : undefined;
|
|
1532
|
+
}
|
|
1533
|
+
var defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
|
|
1534
|
+
if (!schema.minItems || isMultiSelect(validator, schema, rootSchema) || schema.minItems <= defaultsLength) {
|
|
1535
|
+
return defaults ? defaults : [];
|
|
1536
|
+
}
|
|
1537
|
+
var defaultEntries = defaults || [];
|
|
1538
|
+
var fillerSchema = getInnerSchemaForArrayItem(schema, AdditionalItemsHandling.Invert);
|
|
1539
|
+
var fillerDefault = fillerSchema["default"];
|
|
1540
|
+
// Calculate filler entries for remaining items (minItems - existing raw data/defaults)
|
|
1541
|
+
var fillerEntries = new Array(schema.minItems - defaultsLength).fill(computeDefaults(validator, fillerSchema, {
|
|
1542
|
+
parentDefaults: fillerDefault,
|
|
1543
|
+
rootSchema: rootSchema,
|
|
1544
|
+
_recurseList: _recurseList,
|
|
1545
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior
|
|
1546
|
+
}));
|
|
1547
|
+
// then fill up the rest with either the item default or empty, up to minItems
|
|
1548
|
+
return defaultEntries.concat(fillerEntries);
|
|
1286
1549
|
}
|
|
1287
1550
|
}
|
|
1288
1551
|
return defaults;
|
|
@@ -1297,9 +1560,10 @@
|
|
|
1297
1560
|
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
|
|
1298
1561
|
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
|
|
1299
1562
|
* false when computing defaults for any nested object properties.
|
|
1563
|
+
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1300
1564
|
* @returns - The resulting `formData` with all the defaults provided
|
|
1301
1565
|
*/
|
|
1302
|
-
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
|
|
1566
|
+
function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues, experimental_defaultFormStateBehavior) {
|
|
1303
1567
|
if (includeUndefinedValues === void 0) {
|
|
1304
1568
|
includeUndefinedValues = false;
|
|
1305
1569
|
}
|
|
@@ -1307,8 +1571,13 @@
|
|
|
1307
1571
|
throw new Error('Invalid schema: ' + theSchema);
|
|
1308
1572
|
}
|
|
1309
1573
|
var schema = retrieveSchema(validator, theSchema, rootSchema, formData);
|
|
1310
|
-
var defaults = computeDefaults(validator, schema,
|
|
1311
|
-
|
|
1574
|
+
var defaults = computeDefaults(validator, schema, {
|
|
1575
|
+
rootSchema: rootSchema,
|
|
1576
|
+
includeUndefinedValues: includeUndefinedValues,
|
|
1577
|
+
experimental_defaultFormStateBehavior: experimental_defaultFormStateBehavior,
|
|
1578
|
+
rawFormData: formData
|
|
1579
|
+
});
|
|
1580
|
+
if (formData === undefined || formData === null || typeof formData === 'number' && isNaN(formData)) {
|
|
1312
1581
|
// No form data? Use schema defaults.
|
|
1313
1582
|
return defaults;
|
|
1314
1583
|
}
|
|
@@ -1678,15 +1947,12 @@
|
|
|
1678
1947
|
}
|
|
1679
1948
|
}
|
|
1680
1949
|
var pathSchema = (_pathSchema = {}, _pathSchema[NAME_KEY] = name.replace(/^\./, ''), _pathSchema);
|
|
1681
|
-
if (ONE_OF_KEY in schema) {
|
|
1682
|
-
var
|
|
1683
|
-
var
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
var _index = getClosestMatchingOption(validator, rootSchema, formData, schema.anyOf, 0);
|
|
1688
|
-
var _schema3 = schema.anyOf[_index];
|
|
1689
|
-
return toPathSchemaInternal(validator, _schema3, name, rootSchema, formData, _recurseList);
|
|
1950
|
+
if (ONE_OF_KEY in schema || ANY_OF_KEY in schema) {
|
|
1951
|
+
var xxxOf = ONE_OF_KEY in schema ? schema.oneOf : schema.anyOf;
|
|
1952
|
+
var discriminator = getDiscriminatorFieldFromSchema(schema);
|
|
1953
|
+
var index = getClosestMatchingOption(validator, rootSchema, formData, xxxOf, 0, discriminator);
|
|
1954
|
+
var _schema2 = xxxOf[index];
|
|
1955
|
+
pathSchema = _extends({}, pathSchema, toPathSchemaInternal(validator, _schema2, name, rootSchema, formData, _recurseList));
|
|
1690
1956
|
}
|
|
1691
1957
|
if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) {
|
|
1692
1958
|
set__default["default"](pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
|
|
@@ -1723,8 +1989,8 @@
|
|
|
1723
1989
|
}
|
|
1724
1990
|
|
|
1725
1991
|
/** The `SchemaUtils` class provides a wrapper around the publicly exported APIs in the `utils/schema` directory such
|
|
1726
|
-
* that one does not have to explicitly pass the `validator` or `
|
|
1727
|
-
*
|
|
1992
|
+
* that one does not have to explicitly pass the `validator`, `rootSchema`, or `experimental_defaultFormStateBehavior` to each method.
|
|
1993
|
+
* Since these generally do not change across a `Form`, this allows for providing a simplified set of APIs to the
|
|
1728
1994
|
* `@rjsf/core` components and the various themes as well. This class implements the `SchemaUtilsType` interface.
|
|
1729
1995
|
*/
|
|
1730
1996
|
var SchemaUtils = /*#__PURE__*/function () {
|
|
@@ -1732,12 +1998,15 @@
|
|
|
1732
1998
|
*
|
|
1733
1999
|
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
1734
2000
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
2001
|
+
* @param experimental_defaultFormStateBehavior - Configuration flags to allow users to override default form state behavior
|
|
1735
2002
|
*/
|
|
1736
|
-
function SchemaUtils(validator, rootSchema) {
|
|
2003
|
+
function SchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
1737
2004
|
this.rootSchema = void 0;
|
|
1738
2005
|
this.validator = void 0;
|
|
2006
|
+
this.experimental_defaultFormStateBehavior = void 0;
|
|
1739
2007
|
this.rootSchema = rootSchema;
|
|
1740
2008
|
this.validator = validator;
|
|
2009
|
+
this.experimental_defaultFormStateBehavior = experimental_defaultFormStateBehavior;
|
|
1741
2010
|
}
|
|
1742
2011
|
/** Returns the `ValidatorType` in the `SchemaUtilsType`
|
|
1743
2012
|
*
|
|
@@ -1753,13 +2022,17 @@
|
|
|
1753
2022
|
*
|
|
1754
2023
|
* @param validator - An implementation of the `ValidatorType` interface that will be compared against the current one
|
|
1755
2024
|
* @param rootSchema - The root schema that will be compared against the current one
|
|
2025
|
+
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1756
2026
|
* @returns - True if the `SchemaUtilsType` differs from the given `validator` or `rootSchema`
|
|
1757
2027
|
*/;
|
|
1758
|
-
_proto.doesSchemaUtilsDiffer = function doesSchemaUtilsDiffer(validator, rootSchema) {
|
|
2028
|
+
_proto.doesSchemaUtilsDiffer = function doesSchemaUtilsDiffer(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
2029
|
+
if (experimental_defaultFormStateBehavior === void 0) {
|
|
2030
|
+
experimental_defaultFormStateBehavior = {};
|
|
2031
|
+
}
|
|
1759
2032
|
if (!validator || !rootSchema) {
|
|
1760
2033
|
return false;
|
|
1761
2034
|
}
|
|
1762
|
-
return this.validator !== validator || !deepEquals(this.rootSchema, rootSchema);
|
|
2035
|
+
return this.validator !== validator || !deepEquals(this.rootSchema, rootSchema) || !deepEquals(this.experimental_defaultFormStateBehavior, experimental_defaultFormStateBehavior);
|
|
1763
2036
|
}
|
|
1764
2037
|
/** Returns the superset of `formData` that includes the given set updated to include any missing fields that have
|
|
1765
2038
|
* computed to have defaults provided in the `schema`.
|
|
@@ -1775,7 +2048,7 @@
|
|
|
1775
2048
|
if (includeUndefinedValues === void 0) {
|
|
1776
2049
|
includeUndefinedValues = false;
|
|
1777
2050
|
}
|
|
1778
|
-
return getDefaultFormState(this.validator, schema, formData, this.rootSchema, includeUndefinedValues);
|
|
2051
|
+
return getDefaultFormState(this.validator, schema, formData, this.rootSchema, includeUndefinedValues, this.experimental_defaultFormStateBehavior);
|
|
1779
2052
|
}
|
|
1780
2053
|
/** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
|
|
1781
2054
|
* should be displayed in a UI.
|
|
@@ -1928,10 +2201,14 @@
|
|
|
1928
2201
|
*
|
|
1929
2202
|
* @param validator - an implementation of the `ValidatorType` interface that will be forwarded to all the APIs
|
|
1930
2203
|
* @param rootSchema - The root schema that will be forwarded to all the APIs
|
|
2204
|
+
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
|
|
1931
2205
|
* @returns - An implementation of a `SchemaUtilsType` interface
|
|
1932
2206
|
*/
|
|
1933
|
-
function createSchemaUtils(validator, rootSchema) {
|
|
1934
|
-
|
|
2207
|
+
function createSchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior) {
|
|
2208
|
+
if (experimental_defaultFormStateBehavior === void 0) {
|
|
2209
|
+
experimental_defaultFormStateBehavior = {};
|
|
2210
|
+
}
|
|
2211
|
+
return new SchemaUtils(validator, rootSchema, experimental_defaultFormStateBehavior);
|
|
1935
2212
|
}
|
|
1936
2213
|
|
|
1937
2214
|
/** Given the `FileReader.readAsDataURL()` based `dataURI` extracts that data into an actual Blob along with the name
|
|
@@ -2486,6 +2763,32 @@
|
|
|
2486
2763
|
throw new Error("No widget '" + widget + "' for type '" + type + "'");
|
|
2487
2764
|
}
|
|
2488
2765
|
|
|
2766
|
+
/** JS has no built-in hashing function, so rolling our own
|
|
2767
|
+
* based on Java's hashing fn:
|
|
2768
|
+
* http://www.java2s.com/example/nodejs-utility-method/string-hash/hashcode-4dc2b.html
|
|
2769
|
+
*
|
|
2770
|
+
* @param string - The string for which to get the hash
|
|
2771
|
+
* @returns - The resulting hash of the string in hex format
|
|
2772
|
+
*/
|
|
2773
|
+
function hashString(string) {
|
|
2774
|
+
var hash = 0;
|
|
2775
|
+
for (var i = 0; i < string.length; i += 1) {
|
|
2776
|
+
var chr = string.charCodeAt(i);
|
|
2777
|
+
hash = (hash << 5) - hash + chr;
|
|
2778
|
+
hash = hash & hash; // Convert to 32bit integer
|
|
2779
|
+
}
|
|
2780
|
+
|
|
2781
|
+
return hash.toString(16);
|
|
2782
|
+
}
|
|
2783
|
+
/** Stringifies the schema and returns the hash of the resulting string.
|
|
2784
|
+
*
|
|
2785
|
+
* @param schema - The schema for which the hash is desired
|
|
2786
|
+
* @returns - The string obtained from the hash of the stringified schema
|
|
2787
|
+
*/
|
|
2788
|
+
function hashForSchema(schema) {
|
|
2789
|
+
return hashString(JSON.stringify(schema));
|
|
2790
|
+
}
|
|
2791
|
+
|
|
2489
2792
|
/** Detects whether the `widget` exists for the `schema` with the associated `registryWidgets` and returns true if it
|
|
2490
2793
|
* does, or false if it doesn't.
|
|
2491
2794
|
*
|
|
@@ -3089,6 +3392,141 @@
|
|
|
3089
3392
|
TranslatableString["FilesInfo"] = "<strong>%1</strong> (%2, %3 bytes)";
|
|
3090
3393
|
})(exports.TranslatableString || (exports.TranslatableString = {}));
|
|
3091
3394
|
|
|
3395
|
+
/** An implementation of the `ValidatorType` interface that is designed for use in capturing schemas used by the
|
|
3396
|
+
* `isValid()` function. The rest of the implementation of the interface throws errors when it is attempted to be used.
|
|
3397
|
+
* An instance of the object allows the caller to capture the schemas used in calls to the `isValid()` function. These
|
|
3398
|
+
* captured schema, along with the root schema used to construct the object are stored in the map of schemas keyed by
|
|
3399
|
+
* the hashed value of the schema. NOTE: After hashing the schema, an $id with the hash value is added to the
|
|
3400
|
+
* schema IF that schema doesn't already have an $id, prior to putting the schema into the map.
|
|
3401
|
+
*/
|
|
3402
|
+
var ParserValidator = /*#__PURE__*/function () {
|
|
3403
|
+
/** Construct the ParserValidator for the given `rootSchema`. This `rootSchema` will be stashed in the `schemaMap`
|
|
3404
|
+
* first.
|
|
3405
|
+
*
|
|
3406
|
+
* @param rootSchema - The root schema against which this validator will be executed
|
|
3407
|
+
*/
|
|
3408
|
+
function ParserValidator(rootSchema) {
|
|
3409
|
+
/** The rootSchema provided during construction of the class */
|
|
3410
|
+
this.rootSchema = void 0;
|
|
3411
|
+
/** The map of schemas encountered by the ParserValidator */
|
|
3412
|
+
this.schemaMap = {};
|
|
3413
|
+
this.rootSchema = rootSchema;
|
|
3414
|
+
this.addSchema(rootSchema, hashForSchema(rootSchema));
|
|
3415
|
+
}
|
|
3416
|
+
/** Adds the given `schema` to the `schemaMap` keyed by the `hash` or `ID_KEY` if present on the `schema`. If the
|
|
3417
|
+
* schema does not have an `ID_KEY`, then the `hash` will be added as the `ID_KEY` to allow the schema to be
|
|
3418
|
+
* associated with it's `hash` for future use (by a schema compiler).
|
|
3419
|
+
*
|
|
3420
|
+
* @param schema - The schema which is to be added to the map
|
|
3421
|
+
* @param hash - The hash value at which to map the schema
|
|
3422
|
+
*/
|
|
3423
|
+
var _proto = ParserValidator.prototype;
|
|
3424
|
+
_proto.addSchema = function addSchema(schema, hash) {
|
|
3425
|
+
var _extends2;
|
|
3426
|
+
var key = get__default["default"](schema, ID_KEY, hash);
|
|
3427
|
+
var identifiedSchema = _extends({}, schema, (_extends2 = {}, _extends2[ID_KEY] = key, _extends2));
|
|
3428
|
+
var existing = this.schemaMap[key];
|
|
3429
|
+
if (!existing) {
|
|
3430
|
+
this.schemaMap[key] = identifiedSchema;
|
|
3431
|
+
} else if (!isEqual__default["default"](existing, identifiedSchema)) {
|
|
3432
|
+
console.error('existing schema:', JSON.stringify(existing, null, 2));
|
|
3433
|
+
console.error('new schema:', JSON.stringify(identifiedSchema, null, 2));
|
|
3434
|
+
throw new Error("Two different schemas exist with the same key " + key + "! What a bad coincidence. If possible, try adding an $id to one of the schemas");
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
/** Returns the current `schemaMap` to the caller
|
|
3438
|
+
*/;
|
|
3439
|
+
_proto.getSchemaMap = function getSchemaMap() {
|
|
3440
|
+
return this.schemaMap;
|
|
3441
|
+
}
|
|
3442
|
+
/** Implements the `ValidatorType` `isValid()` method to capture the `schema` in the `schemaMap`. Throws an error when
|
|
3443
|
+
* the `rootSchema` is not the same as the root schema provided during construction.
|
|
3444
|
+
*
|
|
3445
|
+
* @param schema - The schema to record in the `schemaMap`
|
|
3446
|
+
* @param _formData - The formData parameter that is ignored
|
|
3447
|
+
* @param rootSchema - The root schema associated with the schema
|
|
3448
|
+
* @throws - Error when the given `rootSchema` differs from the root schema provided during construction
|
|
3449
|
+
*/;
|
|
3450
|
+
_proto.isValid = function isValid(schema, _formData, rootSchema) {
|
|
3451
|
+
if (!isEqual__default["default"](rootSchema, this.rootSchema)) {
|
|
3452
|
+
throw new Error('Unexpectedly calling isValid() with a rootSchema that differs from the construction rootSchema');
|
|
3453
|
+
}
|
|
3454
|
+
this.addSchema(schema, hashForSchema(schema));
|
|
3455
|
+
return false;
|
|
3456
|
+
}
|
|
3457
|
+
/** Implements the `ValidatorType` `rawValidation()` method to throw an error since it is never supposed to be called
|
|
3458
|
+
*
|
|
3459
|
+
* @param _schema - The schema parameter that is ignored
|
|
3460
|
+
* @param _formData - The formData parameter that is ignored
|
|
3461
|
+
*/;
|
|
3462
|
+
_proto.rawValidation = function rawValidation(_schema, _formData) {
|
|
3463
|
+
throw new Error('Unexpectedly calling the `rawValidation()` method during schema parsing');
|
|
3464
|
+
}
|
|
3465
|
+
/** Implements the `ValidatorType` `toErrorList()` method to throw an error since it is never supposed to be called
|
|
3466
|
+
*
|
|
3467
|
+
* @param _errorSchema - The error schema parameter that is ignored
|
|
3468
|
+
* @param _fieldPath - The field path parameter that is ignored
|
|
3469
|
+
*/;
|
|
3470
|
+
_proto.toErrorList = function toErrorList(_errorSchema, _fieldPath) {
|
|
3471
|
+
throw new Error('Unexpectedly calling the `toErrorList()` method during schema parsing');
|
|
3472
|
+
}
|
|
3473
|
+
/** Implements the `ValidatorType` `validateFormData()` method to throw an error since it is never supposed to be
|
|
3474
|
+
* called
|
|
3475
|
+
*
|
|
3476
|
+
* @param _formData - The formData parameter that is ignored
|
|
3477
|
+
* @param _schema - The schema parameter that is ignored
|
|
3478
|
+
* @param _customValidate - The customValidate parameter that is ignored
|
|
3479
|
+
* @param _transformErrors - The transformErrors parameter that is ignored
|
|
3480
|
+
* @param _uiSchema - The uiSchema parameter that is ignored
|
|
3481
|
+
*/;
|
|
3482
|
+
_proto.validateFormData = function validateFormData(_formData, _schema, _customValidate, _transformErrors, _uiSchema) {
|
|
3483
|
+
throw new Error('Unexpectedly calling the `validateFormData()` method during schema parsing');
|
|
3484
|
+
};
|
|
3485
|
+
return ParserValidator;
|
|
3486
|
+
}();
|
|
3487
|
+
|
|
3488
|
+
/** Recursive function used to parse the given `schema` belonging to the `rootSchema`. The `validator` is used to
|
|
3489
|
+
* capture the sub-schemas that the `isValid()` function is called with. For each schema returned by the
|
|
3490
|
+
* `retrieveSchemaInternal()`, the `resolveAnyOrOneOfSchemas()` function is called. For each of the schemas returned
|
|
3491
|
+
* from THAT call have `properties`, then each of the sub-schema property objects are then recursively parsed.
|
|
3492
|
+
*
|
|
3493
|
+
* @param validator - The `ParserValidator` implementation used to capture `isValid()` calls during parsing
|
|
3494
|
+
* @param recurseList - The list of schemas returned from the `retrieveSchemaInternal`, preventing infinite recursion
|
|
3495
|
+
* @param rootSchema - The root schema from which the schema parsing began
|
|
3496
|
+
* @param schema - The current schema element being parsed
|
|
3497
|
+
*/
|
|
3498
|
+
function parseSchema(validator, recurseList, rootSchema, schema) {
|
|
3499
|
+
var schemas = retrieveSchemaInternal(validator, schema, rootSchema, undefined, true);
|
|
3500
|
+
schemas.forEach(function (schema) {
|
|
3501
|
+
var sameSchemaIndex = recurseList.findIndex(function (item) {
|
|
3502
|
+
return isEqual__default["default"](item, schema);
|
|
3503
|
+
});
|
|
3504
|
+
if (sameSchemaIndex === -1) {
|
|
3505
|
+
recurseList.push(schema);
|
|
3506
|
+
var allOptions = resolveAnyOrOneOfSchemas(validator, schema, rootSchema, true);
|
|
3507
|
+
allOptions.forEach(function (s) {
|
|
3508
|
+
if (PROPERTIES_KEY in s && s[PROPERTIES_KEY]) {
|
|
3509
|
+
forEach__default["default"](schema[PROPERTIES_KEY], function (value) {
|
|
3510
|
+
parseSchema(validator, recurseList, rootSchema, value);
|
|
3511
|
+
});
|
|
3512
|
+
}
|
|
3513
|
+
});
|
|
3514
|
+
}
|
|
3515
|
+
});
|
|
3516
|
+
}
|
|
3517
|
+
/** Parses the given `rootSchema` to extract out all the sub-schemas that maybe contained within it. Returns a map of
|
|
3518
|
+
* the hash of the schema to schema/sub-schema.
|
|
3519
|
+
*
|
|
3520
|
+
* @param rootSchema - The root schema to parse for sub-schemas used by `isValid()` calls
|
|
3521
|
+
* @returns - The `SchemaMap` of all schemas that were parsed
|
|
3522
|
+
*/
|
|
3523
|
+
function schemaParser(rootSchema) {
|
|
3524
|
+
var validator = new ParserValidator(rootSchema);
|
|
3525
|
+
var recurseList = [];
|
|
3526
|
+
parseSchema(validator, recurseList, rootSchema, rootSchema);
|
|
3527
|
+
return validator.getSchemaMap();
|
|
3528
|
+
}
|
|
3529
|
+
|
|
3092
3530
|
exports.ADDITIONAL_PROPERTIES_KEY = ADDITIONAL_PROPERTIES_KEY;
|
|
3093
3531
|
exports.ADDITIONAL_PROPERTY_FLAG = ADDITIONAL_PROPERTY_FLAG;
|
|
3094
3532
|
exports.ALL_OF_KEY = ALL_OF_KEY;
|
|
@@ -3101,6 +3539,7 @@
|
|
|
3101
3539
|
exports.ERRORS_KEY = ERRORS_KEY;
|
|
3102
3540
|
exports.ErrorSchemaBuilder = ErrorSchemaBuilder;
|
|
3103
3541
|
exports.ID_KEY = ID_KEY;
|
|
3542
|
+
exports.IF_KEY = IF_KEY;
|
|
3104
3543
|
exports.ITEMS_KEY = ITEMS_KEY;
|
|
3105
3544
|
exports.NAME_KEY = NAME_KEY;
|
|
3106
3545
|
exports.ONE_OF_KEY = ONE_OF_KEY;
|
|
@@ -3134,6 +3573,7 @@
|
|
|
3134
3573
|
exports.findSchemaDefinition = findSchemaDefinition;
|
|
3135
3574
|
exports.getClosestMatchingOption = getClosestMatchingOption;
|
|
3136
3575
|
exports.getDefaultFormState = getDefaultFormState;
|
|
3576
|
+
exports.getDiscriminatorFieldFromSchema = getDiscriminatorFieldFromSchema;
|
|
3137
3577
|
exports.getDisplayLabel = getDisplayLabel;
|
|
3138
3578
|
exports.getFirstMatchingOption = getFirstMatchingOption;
|
|
3139
3579
|
exports.getInputProps = getInputProps;
|
|
@@ -3145,6 +3585,7 @@
|
|
|
3145
3585
|
exports.getWidget = getWidget;
|
|
3146
3586
|
exports.guessType = guessType;
|
|
3147
3587
|
exports.hasWidget = hasWidget;
|
|
3588
|
+
exports.hashForSchema = hashForSchema;
|
|
3148
3589
|
exports.helpId = helpId;
|
|
3149
3590
|
exports.isConstant = isConstant;
|
|
3150
3591
|
exports.isCustomWidget = isCustomWidget;
|
|
@@ -3168,6 +3609,7 @@
|
|
|
3168
3609
|
exports.replaceStringParameters = replaceStringParameters;
|
|
3169
3610
|
exports.retrieveSchema = retrieveSchema;
|
|
3170
3611
|
exports.sanitizeDataForNewSchema = sanitizeDataForNewSchema;
|
|
3612
|
+
exports.schemaParser = schemaParser;
|
|
3171
3613
|
exports.schemaRequiresTrueValue = schemaRequiresTrueValue;
|
|
3172
3614
|
exports.shouldRender = shouldRender;
|
|
3173
3615
|
exports.titleId = titleId;
|