@rjsf/utils 5.0.0-beta.17 → 5.0.0-beta.19

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.
@@ -7,13 +7,17 @@ var get = require('lodash/get');
7
7
  var isEmpty = require('lodash/isEmpty');
8
8
  var jsonpointer = require('jsonpointer');
9
9
  var omit = require('lodash/omit');
10
+ var has = require('lodash/has');
11
+ var isObject$1 = require('lodash/isObject');
12
+ var isString = require('lodash/isString');
13
+ var reduce = require('lodash/reduce');
14
+ var times = require('lodash/times');
10
15
  var set = require('lodash/set');
11
16
  var mergeAllOf = require('json-schema-merge-allof');
12
17
  var union = require('lodash/union');
13
18
  var cloneDeep = require('lodash/cloneDeep');
14
19
  var React = require('react');
15
20
  var ReactIs = require('react-is');
16
- var isString = require('lodash/isString');
17
21
 
18
22
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
19
23
 
@@ -22,13 +26,17 @@ var get__default = /*#__PURE__*/_interopDefaultLegacy(get);
22
26
  var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
23
27
  var jsonpointer__default = /*#__PURE__*/_interopDefaultLegacy(jsonpointer);
24
28
  var omit__default = /*#__PURE__*/_interopDefaultLegacy(omit);
29
+ var has__default = /*#__PURE__*/_interopDefaultLegacy(has);
30
+ var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject$1);
31
+ var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
32
+ var reduce__default = /*#__PURE__*/_interopDefaultLegacy(reduce);
33
+ var times__default = /*#__PURE__*/_interopDefaultLegacy(times);
25
34
  var set__default = /*#__PURE__*/_interopDefaultLegacy(set);
26
35
  var mergeAllOf__default = /*#__PURE__*/_interopDefaultLegacy(mergeAllOf);
27
36
  var union__default = /*#__PURE__*/_interopDefaultLegacy(union);
28
37
  var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);
29
38
  var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
30
39
  var ReactIs__default = /*#__PURE__*/_interopDefaultLegacy(ReactIs);
31
- var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
32
40
 
33
41
  /** Determines whether a `thing` is an object for the purposes of RSJF. In this case, `thing` is an object if it has
34
42
  * the type `object` but is NOT null, an array or a File.
@@ -309,12 +317,14 @@ function findSchemaDefinition($ref, rootSchema) {
309
317
  }
310
318
 
311
319
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
320
+ * Deprecated, use `getFirstMatchingOption()` instead.
312
321
  *
313
322
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
314
323
  * @param formData - The current formData, if any, used to figure out a match
315
324
  * @param options - The list of options to find a matching options from
316
325
  * @param rootSchema - The root schema, used to primarily to look up `$ref`s
317
326
  * @returns - The index of the matched option or 0 if none is available
327
+ * @deprecated
318
328
  */
319
329
  function getMatchingOption(validator, formData, options, rootSchema) {
320
330
  // For performance, skip validating subschemas if formData is undefined. We just
@@ -370,6 +380,19 @@ function getMatchingOption(validator, formData, options, rootSchema) {
370
380
  return 0;
371
381
  }
372
382
 
383
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
384
+ * Always returns the first option if there is nothing that matches.
385
+ *
386
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
387
+ * @param formData - The current formData, if any, used to figure out a match
388
+ * @param options - The list of options to find a matching options from
389
+ * @param rootSchema - The root schema, used to primarily to look up `$ref`s
390
+ * @returns - The index of the first matched option or 0 if none is available
391
+ */
392
+ function getFirstMatchingOption(validator, formData, options, rootSchema) {
393
+ return getMatchingOption(validator, formData, options, rootSchema);
394
+ }
395
+
373
396
  /** Given a specific `value` attempts to guess the type of a schema element. In the case where we have to implicitly
374
397
  * create a schema, it is useful to know what type to use based on the data we are defining.
375
398
  *
@@ -421,6 +444,12 @@ function getSchemaType(schema) {
421
444
  if (!type && (schema.properties || schema.additionalProperties)) {
422
445
  return "object";
423
446
  }
447
+ if (!type && Array.isArray(schema.oneOf) && schema.oneOf.length) {
448
+ return getSchemaType(schema.oneOf[0]);
449
+ }
450
+ if (!type && Array.isArray(schema.anyOf) && schema.anyOf.length) {
451
+ return getSchemaType(schema.anyOf[0]);
452
+ }
424
453
  if (Array.isArray(type) && type.length === 2 && type.includes("null")) {
425
454
  type = type.find(function (type) {
426
455
  return type !== "null";
@@ -429,99 +458,6 @@ function getSchemaType(schema) {
429
458
  return type;
430
459
  }
431
460
 
432
- /** Detects whether the given `schema` contains fixed items. This is the case when `schema.items` is a non-empty array
433
- * that only contains objects.
434
- *
435
- * @param schema - The schema in which to check for fixed items
436
- * @returns - True if there are fixed items in the schema, false otherwise
437
- */
438
- function isFixedItems(schema) {
439
- return Array.isArray(schema.items) && schema.items.length > 0 && schema.items.every(function (item) {
440
- return isObject(item);
441
- });
442
- }
443
-
444
- /** Merges the `defaults` object of type `T` into the `formData` of type `T`
445
- *
446
- * When merging defaults and form data, we want to merge in this specific way:
447
- * - objects are deeply merged
448
- * - arrays are merged in such a way that:
449
- * - when the array is set in form data, only array entries set in form data
450
- * are deeply merged; additional entries from the defaults are ignored
451
- * - when the array is not set in form data, the default is copied over
452
- * - scalars are overwritten/set by form data
453
- *
454
- * @param defaults - The defaults to merge
455
- * @param formData - The form data into which the defaults will be merged
456
- * @returns - The resulting merged form data with defaults
457
- */
458
- function mergeDefaultsWithFormData(defaults, formData) {
459
- if (Array.isArray(formData)) {
460
- var defaultsArray = Array.isArray(defaults) ? defaults : [];
461
- var mapped = formData.map(function (value, idx) {
462
- if (defaultsArray[idx]) {
463
- return mergeDefaultsWithFormData(defaultsArray[idx], value);
464
- }
465
- return value;
466
- });
467
- return mapped;
468
- }
469
- if (isObject(formData)) {
470
- var acc = Object.assign({}, defaults); // Prevent mutation of source object.
471
- return Object.keys(formData).reduce(function (acc, key) {
472
- acc[key] = mergeDefaultsWithFormData(defaults ? get__default["default"](defaults, key) : {}, get__default["default"](formData, key));
473
- return acc;
474
- }, acc);
475
- }
476
- return formData;
477
- }
478
-
479
- /** Recursively merge deeply nested objects.
480
- *
481
- * @param obj1 - The first object to merge
482
- * @param obj2 - The second object to merge
483
- * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated. Use
484
- * "preventDuplicates" to merge arrays in a manner that prevents any duplicate entries from being merged.
485
- * NOTE: Uses shallow comparison for the duplicate checking.
486
- * @returns - A new object that is the merge of the two given objects
487
- */
488
- function mergeObjects(obj1, obj2, concatArrays) {
489
- if (concatArrays === void 0) {
490
- concatArrays = false;
491
- }
492
- return Object.keys(obj2).reduce(function (acc, key) {
493
- var left = obj1 ? obj1[key] : {},
494
- right = obj2[key];
495
- if (obj1 && key in obj1 && isObject(right)) {
496
- acc[key] = mergeObjects(left, right, concatArrays);
497
- } else if (concatArrays && Array.isArray(left) && Array.isArray(right)) {
498
- var toMerge = right;
499
- if (concatArrays === "preventDuplicates") {
500
- toMerge = right.reduce(function (result, value) {
501
- if (!left.includes(value)) {
502
- result.push(value);
503
- }
504
- return result;
505
- }, []);
506
- }
507
- acc[key] = left.concat(toMerge);
508
- } else {
509
- acc[key] = right;
510
- }
511
- return acc;
512
- }, Object.assign({}, obj1)); // Prevent mutation of source object.
513
- }
514
-
515
- /** This function checks if the given `schema` matches a single constant value. This happens when either the schema has
516
- * an `enum` array with a single value or there is a `const` defined.
517
- *
518
- * @param schema - The schema for a field
519
- * @returns - True if the `schema` has a single constant value, false otherwise
520
- */
521
- function isConstant(schema) {
522
- return Array.isArray(schema["enum"]) && schema["enum"].length === 1 || CONST_KEY in schema;
523
- }
524
-
525
461
  /** Recursively merge deeply nested schemas. The difference between `mergeSchemas` and `mergeObjects` is that
526
462
  * `mergeSchemas` only concats arrays for values under the 'required' keyword, and when it does, it doesn't include
527
463
  * duplicate values.
@@ -558,7 +494,7 @@ var _excluded$1 = ["if", "then", "else"],
558
494
  * @param validator - An implementation of the `ValidatorType<T, S>` interface that is used to detect valid schema conditions
559
495
  * @param schema - The schema for which resolving a condition is desired
560
496
  * @param rootSchema - The root schema that will be forwarded to all the APIs
561
- * @param formData - The current formData to assist retrieving a schema
497
+ * @param [formData] - The current formData to assist retrieving a schema
562
498
  * @returns - A schema with the appropriate condition resolved
563
499
  */
564
500
  function resolveCondition(validator, schema, rootSchema, formData) {
@@ -646,6 +582,10 @@ function stubExistingAdditionalProperties(validator, theSchema, rootSchema, aFor
646
582
  }, rootSchema, formData);
647
583
  } else if ("type" in schema.additionalProperties) {
648
584
  additionalProperties = _extends({}, schema.additionalProperties);
585
+ } else if (ANY_OF_KEY in schema.additionalProperties || ONE_OF_KEY in schema.additionalProperties) {
586
+ additionalProperties = _extends({
587
+ type: "object"
588
+ }, schema.additionalProperties);
649
589
  } else {
650
590
  additionalProperties = {
651
591
  type: guessType(get__default["default"](formData, [key]))
@@ -717,9 +657,9 @@ function resolveDependencies(validator, schema, rootSchema, formData) {
717
657
  remainingSchema = _objectWithoutPropertiesLoose(schema, _excluded4);
718
658
  var resolvedSchema = remainingSchema;
719
659
  if (Array.isArray(resolvedSchema.oneOf)) {
720
- resolvedSchema = resolvedSchema.oneOf[getMatchingOption(validator, formData, resolvedSchema.oneOf, rootSchema)];
660
+ resolvedSchema = resolvedSchema.oneOf[getFirstMatchingOption(validator, formData, resolvedSchema.oneOf, rootSchema)];
721
661
  } else if (Array.isArray(resolvedSchema.anyOf)) {
722
- resolvedSchema = resolvedSchema.anyOf[getMatchingOption(validator, formData, resolvedSchema.anyOf, rootSchema)];
662
+ resolvedSchema = resolvedSchema.anyOf[getFirstMatchingOption(validator, formData, resolvedSchema.anyOf, rootSchema)];
723
663
  }
724
664
  return processDependencies(validator, dependencies, resolvedSchema, rootSchema, formData);
725
665
  }
@@ -840,6 +780,242 @@ function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, o
840
780
  return mergeSchemas(schema, retrieveSchema(validator, dependentSchema, rootSchema, formData));
841
781
  }
842
782
 
783
+ /** A junk option used to determine when the getFirstMatchingOption call really matches an option rather than returning
784
+ * the first item
785
+ */
786
+ var JUNK_OPTION = {
787
+ type: "object",
788
+ properties: {
789
+ __not_really_there__: {
790
+ type: "number"
791
+ }
792
+ }
793
+ };
794
+ /** Recursive function that calculates the score of a `formData` against the given `schema`. The computation is fairly
795
+ * simple. Initially the total score is 0. When `schema.properties` object exists, then all the `key/value` pairs within
796
+ * the object are processed as follows after obtaining the formValue from `formData` using the `key`:
797
+ * - If the `value` contains a `$ref`, `calculateIndexScore()` is called recursively with the formValue and the new
798
+ * schema that is the result of the ref in the schema being resolved and that sub-schema's resulting score is added to
799
+ * the total.
800
+ * - If the `value` contains a `oneOf` and there is a formValue, then score based on the index returned from calling
801
+ * `getClosestMatchingOption()` of that oneOf.
802
+ * - If the type of the `value` is 'object', `calculateIndexScore()` is called recursively with the formValue and the
803
+ * `value` itself as the sub-schema, and the score is added to the total.
804
+ * - If the type of the `value` matches the guessed-type of the `formValue`, the score is incremented by 1, UNLESS the
805
+ * value has a `default` or `const`. In those case, if the `default` or `const` and the `formValue` match, the score
806
+ * is incremented by another 1 otherwise it is decremented by 1.
807
+ *
808
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
809
+ * @param rootSchema - The root JSON schema of the entire form
810
+ * @param schema - The schema for which the score is being calculated
811
+ * @param formData - The form data associated with the schema, used to calculate the score
812
+ * @returns - The score a schema against the formData
813
+ */
814
+ function calculateIndexScore(validator, rootSchema, schema, formData) {
815
+ if (formData === void 0) {
816
+ formData = {};
817
+ }
818
+ var totalScore = 0;
819
+ if (schema) {
820
+ if (isObject__default["default"](schema.properties)) {
821
+ totalScore += reduce__default["default"](schema.properties, function (score, value, key) {
822
+ var formValue = get__default["default"](formData, key);
823
+ if (typeof value === "boolean") {
824
+ return score;
825
+ }
826
+ if (has__default["default"](value, REF_KEY)) {
827
+ var newSchema = retrieveSchema(validator, value, rootSchema, formValue);
828
+ return score + calculateIndexScore(validator, rootSchema, newSchema, formValue || {});
829
+ }
830
+ if (has__default["default"](value, ONE_OF_KEY) && formValue) {
831
+ return score + getClosestMatchingOption(validator, rootSchema, formValue, get__default["default"](value, ONE_OF_KEY));
832
+ }
833
+ if (value.type === "object") {
834
+ return score + calculateIndexScore(validator, rootSchema, value, formValue || {});
835
+ }
836
+ if (value.type === guessType(formValue)) {
837
+ // If the types match, then we bump the score by one
838
+ var newScore = score + 1;
839
+ if (value["default"]) {
840
+ // If the schema contains a readonly default value score the value that matches the default higher and
841
+ // any non-matching value lower
842
+ newScore += formValue === value["default"] ? 1 : -1;
843
+ } else if (value["const"]) {
844
+ // If the schema contains a const value score the value that matches the default higher and
845
+ // any non-matching value lower
846
+ newScore += formValue === value["const"] ? 1 : -1;
847
+ }
848
+ // TODO eventually, deal with enums/arrays
849
+ return newScore;
850
+ }
851
+ return score;
852
+ }, 0);
853
+ } else if (isString__default["default"](schema.type) && schema.type === guessType(formData)) {
854
+ totalScore += 1;
855
+ }
856
+ }
857
+ return totalScore;
858
+ }
859
+ /** Determines which of the given `options` provided most closely matches the `formData`. Using
860
+ * `getFirstMatchingOption()` to match two schemas that differ only by the readOnly, default or const value of a field
861
+ * based on the `formData` and returns 0 when there is no match. Rather than passing in all the `options` at once to
862
+ * this utility, instead an array of valid option indexes is created by iterating over the list of options, call
863
+ * `getFirstMatchingOptions` with a list of one junk option and one good option, seeing if the good option is considered
864
+ * matched.
865
+ *
866
+ * Once the list of valid indexes is created, if there is only one valid index, just return it. Otherwise, if there are
867
+ * no valid indexes, then fill the valid indexes array with the indexes of all the options. Next, the index of the
868
+ * option with the highest score is determined by iterating over the list of valid options, calling
869
+ * `calculateIndexScore()` on each, comparing it against the current best score, and returning the index of the one that
870
+ * eventually has the best score.
871
+ *
872
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
873
+ * @param rootSchema - The root JSON schema of the entire form
874
+ * @param formData - The form data associated with the schema
875
+ * @param options - The list of options that can be selected from
876
+ * @param [selectedOption=-1] - The index of the currently selected option, defaulted to -1 if not specified
877
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
878
+ */
879
+ function getClosestMatchingOption(validator, rootSchema, formData, options, selectedOption) {
880
+ if (selectedOption === void 0) {
881
+ selectedOption = -1;
882
+ }
883
+ // Reduce the array of options down to a list of the indexes that are considered matching options
884
+ var allValidIndexes = options.reduce(function (validList, option, index) {
885
+ var testOptions = [JUNK_OPTION, option];
886
+ var match = getFirstMatchingOption(validator, formData, testOptions, rootSchema);
887
+ // The match is the real option, so add its index to list of valid indexes
888
+ if (match === 1) {
889
+ validList.push(index);
890
+ }
891
+ return validList;
892
+ }, []);
893
+ // There is only one valid index, so return it!
894
+ if (allValidIndexes.length === 1) {
895
+ return allValidIndexes[0];
896
+ }
897
+ if (!allValidIndexes.length) {
898
+ // No indexes were valid, so we'll score all the options, add all the indexes
899
+ times__default["default"](options.length, function (i) {
900
+ return allValidIndexes.push(i);
901
+ });
902
+ }
903
+ // Score all the options in the list of valid indexes and return the index with the best score
904
+ var _allValidIndexes$redu = allValidIndexes.reduce(function (scoreData, index) {
905
+ var bestScore = scoreData.bestScore;
906
+ var option = options[index];
907
+ if (has__default["default"](option, REF_KEY)) {
908
+ option = retrieveSchema(validator, option, rootSchema, formData);
909
+ }
910
+ var score = calculateIndexScore(validator, rootSchema, option, formData);
911
+ if (score > bestScore) {
912
+ return {
913
+ bestIndex: index,
914
+ bestScore: score
915
+ };
916
+ }
917
+ return scoreData;
918
+ }, {
919
+ bestIndex: selectedOption,
920
+ bestScore: 0
921
+ }),
922
+ bestIndex = _allValidIndexes$redu.bestIndex;
923
+ return bestIndex;
924
+ }
925
+
926
+ /** Detects whether the given `schema` contains fixed items. This is the case when `schema.items` is a non-empty array
927
+ * that only contains objects.
928
+ *
929
+ * @param schema - The schema in which to check for fixed items
930
+ * @returns - True if there are fixed items in the schema, false otherwise
931
+ */
932
+ function isFixedItems(schema) {
933
+ return Array.isArray(schema.items) && schema.items.length > 0 && schema.items.every(function (item) {
934
+ return isObject(item);
935
+ });
936
+ }
937
+
938
+ /** Merges the `defaults` object of type `T` into the `formData` of type `T`
939
+ *
940
+ * When merging defaults and form data, we want to merge in this specific way:
941
+ * - objects are deeply merged
942
+ * - arrays are merged in such a way that:
943
+ * - when the array is set in form data, only array entries set in form data
944
+ * are deeply merged; additional entries from the defaults are ignored
945
+ * - when the array is not set in form data, the default is copied over
946
+ * - scalars are overwritten/set by form data
947
+ *
948
+ * @param [defaults] - The defaults to merge
949
+ * @param [formData] - The form data into which the defaults will be merged
950
+ * @returns - The resulting merged form data with defaults
951
+ */
952
+ function mergeDefaultsWithFormData(defaults, formData) {
953
+ if (Array.isArray(formData)) {
954
+ var defaultsArray = Array.isArray(defaults) ? defaults : [];
955
+ var mapped = formData.map(function (value, idx) {
956
+ if (defaultsArray[idx]) {
957
+ return mergeDefaultsWithFormData(defaultsArray[idx], value);
958
+ }
959
+ return value;
960
+ });
961
+ return mapped;
962
+ }
963
+ if (isObject(formData)) {
964
+ var acc = Object.assign({}, defaults); // Prevent mutation of source object.
965
+ return Object.keys(formData).reduce(function (acc, key) {
966
+ acc[key] = mergeDefaultsWithFormData(defaults ? get__default["default"](defaults, key) : {}, get__default["default"](formData, key));
967
+ return acc;
968
+ }, acc);
969
+ }
970
+ return formData;
971
+ }
972
+
973
+ /** Recursively merge deeply nested objects.
974
+ *
975
+ * @param obj1 - The first object to merge
976
+ * @param obj2 - The second object to merge
977
+ * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated. Use
978
+ * "preventDuplicates" to merge arrays in a manner that prevents any duplicate entries from being merged.
979
+ * NOTE: Uses shallow comparison for the duplicate checking.
980
+ * @returns - A new object that is the merge of the two given objects
981
+ */
982
+ function mergeObjects(obj1, obj2, concatArrays) {
983
+ if (concatArrays === void 0) {
984
+ concatArrays = false;
985
+ }
986
+ return Object.keys(obj2).reduce(function (acc, key) {
987
+ var left = obj1 ? obj1[key] : {},
988
+ right = obj2[key];
989
+ if (obj1 && key in obj1 && isObject(right)) {
990
+ acc[key] = mergeObjects(left, right, concatArrays);
991
+ } else if (concatArrays && Array.isArray(left) && Array.isArray(right)) {
992
+ var toMerge = right;
993
+ if (concatArrays === "preventDuplicates") {
994
+ toMerge = right.reduce(function (result, value) {
995
+ if (!left.includes(value)) {
996
+ result.push(value);
997
+ }
998
+ return result;
999
+ }, []);
1000
+ }
1001
+ acc[key] = left.concat(toMerge);
1002
+ } else {
1003
+ acc[key] = right;
1004
+ }
1005
+ return acc;
1006
+ }, Object.assign({}, obj1)); // Prevent mutation of source object.
1007
+ }
1008
+
1009
+ /** This function checks if the given `schema` matches a single constant value. This happens when either the schema has
1010
+ * an `enum` array with a single value or there is a `const` defined.
1011
+ *
1012
+ * @param schema - The schema for a field
1013
+ * @returns - True if the `schema` has a single constant value, false otherwise
1014
+ */
1015
+ function isConstant(schema) {
1016
+ return Array.isArray(schema["enum"]) && schema["enum"].length === 1 || CONST_KEY in schema;
1017
+ }
1018
+
843
1019
  /** Checks to see if the `schema` combination represents a select
844
1020
  *
845
1021
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
@@ -965,9 +1141,9 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
965
1141
  return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
966
1142
  });
967
1143
  } else if (ONE_OF_KEY in schema) {
968
- schema = schema.oneOf[getMatchingOption(validator, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, rootSchema)];
1144
+ schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0)];
969
1145
  } else if (ANY_OF_KEY in schema) {
970
- schema = schema.anyOf[getMatchingOption(validator, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, rootSchema)];
1146
+ schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0)];
971
1147
  }
972
1148
  // Not defaults defined for this node, fallback to generic typed ones.
973
1149
  if (typeof defaults === "undefined") {
@@ -1159,6 +1335,169 @@ function mergeValidationData(validator, validationData, additionalErrorSchema) {
1159
1335
  };
1160
1336
  }
1161
1337
 
1338
+ var NO_VALUE = /*#__PURE__*/Symbol("no Value");
1339
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the new
1340
+ * schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the nature
1341
+ * of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the old schema
1342
+ * that are non-existent in the new schema are set to `undefined`. The data sanitization process has the following flow:
1343
+ *
1344
+ * - If the new schema is an object that contains a `properties` object then:
1345
+ * - Create a `removeOldSchemaData` object, setting each key in the `oldSchema.properties` having `data` to undefined
1346
+ * - Create an empty `nestedData` object for use in the key filtering below:
1347
+ * - Iterate over each key in the `newSchema.properties` as follows:
1348
+ * - Get the `formValue` of the key from the `data`
1349
+ * - Get the `oldKeySchema` and `newKeyedSchema` for the key, defaulting to `{}` when it doesn't exist
1350
+ * - Retrieve the schema for any refs within each `oldKeySchema` and/or `newKeySchema`
1351
+ * - Get the types of the old and new keyed schemas and if the old doesn't exist or the old & new are the same then:
1352
+ * - If `removeOldSchemaData` has an entry for the key, delete it since the new schema has the same property
1353
+ * - If type of the key in the new schema is `object`:
1354
+ * - Store the value from the recursive `sanitizeDataForNewSchema` call in `nestedData[key]`
1355
+ * - Otherwise, check for default or const values:
1356
+ * - Get the old and new `default` values from the schema and check:
1357
+ * - If the new `default` value does not match the form value:
1358
+ * - If the old `default` value DOES match the form value, then:
1359
+ * - Replace `removeOldSchemaData[key]` with the new `default`
1360
+ * - Otherwise, if the new schema is `readOnly` then replace `removeOldSchemaData[key]` with undefined
1361
+ * - Get the old and new `const` values from the schema and check:
1362
+ * - If the new `const` value does not match the form value:
1363
+ * - If the old `const` value DOES match the form value, then:
1364
+ * - Replace `removeOldSchemaData[key]` with the new `const`
1365
+ * - Otherwise, replace `removeOldSchemaData[key]` with undefined
1366
+ * - Once all keys have been processed, return an object built as follows:
1367
+ * - `{ ...removeOldSchemaData, ...nestedData, ...pick(data, keysToKeep) }`
1368
+ * - If the new and old schema types are array and the `data` is an array then:
1369
+ * - If the type of the old and new schema `items` are a non-array objects:
1370
+ * - Retrieve the schema for any refs within each `oldKeySchema.items` and/or `newKeySchema.items`
1371
+ * - If the `type`s of both items are the same (or the old does not have a type):
1372
+ * - If the type is "object", then:
1373
+ * - For each element in the `data` recursively sanitize the data, stopping at `maxItems` if specified
1374
+ * - Otherwise, just return the `data` removing any values after `maxItems` if it is set
1375
+ * - If the type of the old and new schema `items` are booleans of the same value, return `data` as is
1376
+ * - Otherwise return `undefined`
1377
+ *
1378
+ * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
1379
+ * @param rootSchema - The root JSON schema of the entire form
1380
+ * @param [newSchema] - The new schema for which the data is being sanitized
1381
+ * @param [oldSchema] - The old schema from which the data originated
1382
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
1383
+ * @returns - The new form data, with all the fields uniquely associated with the old schema set
1384
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
1385
+ */
1386
+ function sanitizeDataForNewSchema(validator, rootSchema, newSchema, oldSchema, data) {
1387
+ if (data === void 0) {
1388
+ data = {};
1389
+ }
1390
+ // By default, we will clear the form data
1391
+ var newFormData;
1392
+ // If the new schema is of type object and that object contains a list of properties
1393
+ if (has__default["default"](newSchema, PROPERTIES_KEY)) {
1394
+ // Create an object containing root-level keys in the old schema, setting each key to undefined to remove the data
1395
+ var removeOldSchemaData = {};
1396
+ if (has__default["default"](oldSchema, PROPERTIES_KEY)) {
1397
+ var properties = get__default["default"](oldSchema, PROPERTIES_KEY, {});
1398
+ Object.keys(properties).forEach(function (key) {
1399
+ if (has__default["default"](data, key)) {
1400
+ removeOldSchemaData[key] = undefined;
1401
+ }
1402
+ });
1403
+ }
1404
+ var keys = Object.keys(get__default["default"](newSchema, PROPERTIES_KEY, {}));
1405
+ // Create a place to store nested data that will be a side-effect of the filter
1406
+ var nestedData = {};
1407
+ keys.forEach(function (key) {
1408
+ var formValue = get__default["default"](data, key);
1409
+ var oldKeyedSchema = get__default["default"](oldSchema, [PROPERTIES_KEY, key], {});
1410
+ var newKeyedSchema = get__default["default"](newSchema, [PROPERTIES_KEY, key], {});
1411
+ // Resolve the refs if they exist
1412
+ if (has__default["default"](oldKeyedSchema, REF_KEY)) {
1413
+ oldKeyedSchema = retrieveSchema(validator, oldKeyedSchema, rootSchema, formValue);
1414
+ }
1415
+ if (has__default["default"](newKeyedSchema, REF_KEY)) {
1416
+ newKeyedSchema = retrieveSchema(validator, newKeyedSchema, rootSchema, formValue);
1417
+ }
1418
+ // Now get types and see if they are the same
1419
+ var oldSchemaTypeForKey = get__default["default"](oldKeyedSchema, "type");
1420
+ var newSchemaTypeForKey = get__default["default"](newKeyedSchema, "type");
1421
+ // Check if the old option has the same key with the same type
1422
+ if (!oldSchemaTypeForKey || oldSchemaTypeForKey === newSchemaTypeForKey) {
1423
+ if (has__default["default"](removeOldSchemaData, key)) {
1424
+ // SIDE-EFFECT: remove the undefined value for a key that has the same type between the old and new schemas
1425
+ delete removeOldSchemaData[key];
1426
+ }
1427
+ // If it is an object, we'll recurse and store the resulting sanitized data for the key
1428
+ if (newSchemaTypeForKey === "object" || newSchemaTypeForKey === "array" && Array.isArray(formValue)) {
1429
+ // SIDE-EFFECT: process the new schema type of object recursively to save iterations
1430
+ var itemData = sanitizeDataForNewSchema(validator, rootSchema, newKeyedSchema, oldKeyedSchema, formValue);
1431
+ if (itemData !== undefined || newSchemaTypeForKey === "array") {
1432
+ // only put undefined values for the array type and not the object type
1433
+ nestedData[key] = itemData;
1434
+ }
1435
+ } else {
1436
+ // Ok, the non-object types match, let's make sure that a default or a const of a different value is replaced
1437
+ // with the new default or const. This allows the case where two schemas differ that only by the default/const
1438
+ // value to be properly selected
1439
+ var newOptionDefault = get__default["default"](newKeyedSchema, "default", NO_VALUE);
1440
+ var oldOptionDefault = get__default["default"](oldKeyedSchema, "default", NO_VALUE);
1441
+ if (newOptionDefault !== NO_VALUE && newOptionDefault !== formValue) {
1442
+ if (oldOptionDefault === formValue) {
1443
+ // If the old default matches the formValue, we'll update the new value to match the new default
1444
+ removeOldSchemaData[key] = newOptionDefault;
1445
+ } else if (get__default["default"](newKeyedSchema, "readOnly") === true) {
1446
+ // If the new schema has the default set to read-only, treat it like a const and remove the value
1447
+ removeOldSchemaData[key] = undefined;
1448
+ }
1449
+ }
1450
+ var newOptionConst = get__default["default"](newKeyedSchema, "const", NO_VALUE);
1451
+ var oldOptionConst = get__default["default"](oldKeyedSchema, "const", NO_VALUE);
1452
+ if (newOptionConst !== NO_VALUE && newOptionConst !== formValue) {
1453
+ // Since this is a const, if the old value matches, replace the value with the new const otherwise clear it
1454
+ removeOldSchemaData[key] = oldOptionConst === formValue ? newOptionConst : undefined;
1455
+ }
1456
+ }
1457
+ }
1458
+ });
1459
+ newFormData = _extends({}, data, removeOldSchemaData, nestedData);
1460
+ // First apply removing the old schema data, then apply the nested data, then apply the old data keys to keep
1461
+ } else if (get__default["default"](oldSchema, "type") === "array" && get__default["default"](newSchema, "type") === "array" && Array.isArray(data)) {
1462
+ var oldSchemaItems = get__default["default"](oldSchema, "items");
1463
+ var newSchemaItems = get__default["default"](newSchema, "items");
1464
+ // If any of the array types `items` are arrays (remember arrays are objects) then we'll just drop the data
1465
+ // Eventually, we may want to deal with when either of the `items` are arrays since those tuple validations
1466
+ if (typeof oldSchemaItems === "object" && typeof newSchemaItems === "object" && !Array.isArray(oldSchemaItems) && !Array.isArray(newSchemaItems)) {
1467
+ if (has__default["default"](oldSchemaItems, REF_KEY)) {
1468
+ oldSchemaItems = retrieveSchema(validator, oldSchemaItems, rootSchema, data);
1469
+ }
1470
+ if (has__default["default"](newSchemaItems, REF_KEY)) {
1471
+ newSchemaItems = retrieveSchema(validator, newSchemaItems, rootSchema, data);
1472
+ }
1473
+ // Now get types and see if they are the same
1474
+ var oldSchemaType = get__default["default"](oldSchemaItems, "type");
1475
+ var newSchemaType = get__default["default"](newSchemaItems, "type");
1476
+ // Check if the old option has the same key with the same type
1477
+ if (!oldSchemaType || oldSchemaType === newSchemaType) {
1478
+ var maxItems = get__default["default"](newSchema, "maxItems", -1);
1479
+ if (newSchemaType === "object") {
1480
+ newFormData = data.reduce(function (newValue, aValue) {
1481
+ var itemValue = sanitizeDataForNewSchema(validator, rootSchema, newSchemaItems, oldSchemaItems, aValue);
1482
+ if (itemValue !== undefined && (maxItems < 0 || newValue.length < maxItems)) {
1483
+ newValue.push(itemValue);
1484
+ }
1485
+ return newValue;
1486
+ }, []);
1487
+ } else {
1488
+ newFormData = maxItems > 0 && data.length > maxItems ? data.slice(0, maxItems) : data;
1489
+ }
1490
+ }
1491
+ } else if (typeof oldSchemaItems === "boolean" && typeof newSchemaItems === "boolean" && oldSchemaItems === newSchemaItems) {
1492
+ // If they are both booleans and have the same value just return the data as is otherwise fall-thru to undefined
1493
+ newFormData = data;
1494
+ }
1495
+ // Also probably want to deal with `prefixItems` as tuples with the latest 2020 draft
1496
+ }
1497
+
1498
+ return newFormData;
1499
+ }
1500
+
1162
1501
  /** Generates an `IdSchema` object for the `schema`, recursively
1163
1502
  *
1164
1503
  * @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
@@ -1220,6 +1559,16 @@ function toPathSchema(validator, schema, name, rootSchema, formData) {
1220
1559
  return toPathSchema(validator, _schema, name, rootSchema, formData);
1221
1560
  }
1222
1561
  var pathSchema = (_pathSchema = {}, _pathSchema[NAME_KEY] = name.replace(/^\./, ""), _pathSchema);
1562
+ if (ONE_OF_KEY in schema) {
1563
+ var index = getClosestMatchingOption(validator, rootSchema, formData, schema.oneOf, 0);
1564
+ var _schema2 = schema.oneOf[index];
1565
+ return toPathSchema(validator, _schema2, name, rootSchema, formData);
1566
+ }
1567
+ if (ANY_OF_KEY in schema) {
1568
+ var _index = getClosestMatchingOption(validator, rootSchema, formData, schema.anyOf, 0);
1569
+ var _schema3 = schema.anyOf[_index];
1570
+ return toPathSchema(validator, _schema3, name, rootSchema, formData);
1571
+ }
1223
1572
  if (ADDITIONAL_PROPERTIES_KEY in schema && schema[ADDITIONAL_PROPERTIES_KEY] !== false) {
1224
1573
  set__default["default"](pathSchema, RJSF_ADDITONAL_PROPERTIES_FLAG, true);
1225
1574
  }
@@ -1304,11 +1653,37 @@ var SchemaUtils = /*#__PURE__*/function () {
1304
1653
  _proto.getDisplayLabel = function getDisplayLabel$1(schema, uiSchema) {
1305
1654
  return getDisplayLabel(this.validator, schema, uiSchema, this.rootSchema);
1306
1655
  }
1656
+ /** Determines which of the given `options` provided most closely matches the `formData`.
1657
+ * Returns the index of the option that is valid and is the closest match, or 0 if there is no match.
1658
+ *
1659
+ * The closest match is determined using the number of matching properties, and more heavily favors options with
1660
+ * matching readOnly, default, or const values.
1661
+ *
1662
+ * @param formData - The form data associated with the schema
1663
+ * @param options - The list of options that can be selected from
1664
+ * @param [selectedOption] - The index of the currently selected option, defaulted to -1 if not specified
1665
+ * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
1666
+ */;
1667
+ _proto.getClosestMatchingOption = function getClosestMatchingOption$1(formData, options, selectedOption) {
1668
+ return getClosestMatchingOption(this.validator, this.rootSchema, formData, options, selectedOption);
1669
+ }
1670
+ /** Given the `formData` and list of `options`, attempts to find the index of the first option that matches the data.
1671
+ * Always returns the first option if there is nothing that matches.
1672
+ *
1673
+ * @param formData - The current formData, if any, used to figure out a match
1674
+ * @param options - The list of options to find a matching options from
1675
+ * @returns - The firstindex of the matched option or 0 if none is available
1676
+ */;
1677
+ _proto.getFirstMatchingOption = function getFirstMatchingOption$1(formData, options) {
1678
+ return getFirstMatchingOption(this.validator, formData, options, this.rootSchema);
1679
+ }
1307
1680
  /** Given the `formData` and list of `options`, attempts to find the index of the option that best matches the data.
1681
+ * Deprecated, use `getFirstMatchingOption()` instead.
1308
1682
  *
1309
1683
  * @param formData - The current formData, if any, onto which to provide any missing defaults
1310
1684
  * @param options - The list of options to find a matching options from
1311
1685
  * @returns - The index of the matched option or 0 if none is available
1686
+ * @deprecated
1312
1687
  */;
1313
1688
  _proto.getMatchingOption = function getMatchingOption$1(formData, options) {
1314
1689
  return getMatchingOption(this.validator, formData, options, this.rootSchema);
@@ -1361,6 +1736,20 @@ var SchemaUtils = /*#__PURE__*/function () {
1361
1736
  _proto.retrieveSchema = function retrieveSchema$1(schema, rawFormData) {
1362
1737
  return retrieveSchema(this.validator, schema, this.rootSchema, rawFormData);
1363
1738
  }
1739
+ /** Sanitize the `data` associated with the `oldSchema` so it is considered appropriate for the `newSchema`. If the
1740
+ * new schema does not contain any properties, then `undefined` is returned to clear all the form data. Due to the
1741
+ * nature of schemas, this sanitization happens recursively for nested objects of data. Also, any properties in the
1742
+ * old schemas that are non-existent in the new schema are set to `undefined`.
1743
+ *
1744
+ * @param [newSchema] - The new schema for which the data is being sanitized
1745
+ * @param [oldSchema] - The old schema from which the data originated
1746
+ * @param [data={}] - The form data associated with the schema, defaulting to an empty object when undefined
1747
+ * @returns - The new form data, with all the fields uniquely associated with the old schema set
1748
+ * to `undefined`. Will return `undefined` if the new schema is not an object containing properties.
1749
+ */;
1750
+ _proto.sanitizeDataForNewSchema = function sanitizeDataForNewSchema$1(newSchema, oldSchema, data) {
1751
+ return sanitizeDataForNewSchema(this.validator, this.rootSchema, newSchema, oldSchema, data);
1752
+ }
1364
1753
  /** Generates an `IdSchema` object for the `schema`, recursively
1365
1754
  *
1366
1755
  * @param schema - The schema for which the display label flag is desired
@@ -2251,8 +2640,10 @@ exports.enumOptionsSelectValue = enumOptionsSelectValue;
2251
2640
  exports.errorId = errorId;
2252
2641
  exports.examplesId = examplesId;
2253
2642
  exports.findSchemaDefinition = findSchemaDefinition;
2643
+ exports.getClosestMatchingOption = getClosestMatchingOption;
2254
2644
  exports.getDefaultFormState = getDefaultFormState;
2255
2645
  exports.getDisplayLabel = getDisplayLabel;
2646
+ exports.getFirstMatchingOption = getFirstMatchingOption;
2256
2647
  exports.getInputProps = getInputProps;
2257
2648
  exports.getMatchingOption = getMatchingOption;
2258
2649
  exports.getSchemaType = getSchemaType;
@@ -2283,6 +2674,7 @@ exports.parseDateString = parseDateString;
2283
2674
  exports.processSelectValue = processSelectValue;
2284
2675
  exports.rangeSpec = rangeSpec;
2285
2676
  exports.retrieveSchema = retrieveSchema;
2677
+ exports.sanitizeDataForNewSchema = sanitizeDataForNewSchema;
2286
2678
  exports.schemaRequiresTrueValue = schemaRequiresTrueValue;
2287
2679
  exports.shouldRender = shouldRender;
2288
2680
  exports.titleId = titleId;