@rjsf/utils 5.9.0 → 5.10.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/utils.esm.js CHANGED
@@ -523,11 +523,12 @@ function mergeSchemas(obj1, obj2) {
523
523
  }, acc);
524
524
  }
525
525
 
526
- var _excluded$1 = ["if", "then", "else"],
527
- _excluded2 = ["$ref"],
526
+ var _excluded$2 = ["if", "then", "else"],
527
+ _excluded2$1 = ["$ref"],
528
528
  _excluded3 = ["allOf"],
529
- _excluded4 = ["dependencies"],
530
- _excluded5 = ["oneOf"];
529
+ _excluded4 = ["oneOf", "anyOf"],
530
+ _excluded5 = ["dependencies"],
531
+ _excluded6 = ["oneOf"];
531
532
  /** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
532
533
  * resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
533
534
  * potentially recursive resolution.
@@ -560,7 +561,7 @@ function resolveCondition(validator, schema, rootSchema, expandAllBranches, form
560
561
  var expression = schema["if"],
561
562
  then = schema.then,
562
563
  otherwise = schema["else"],
563
- resolvedSchemaLessConditional = _objectWithoutPropertiesLoose(schema, _excluded$1);
564
+ resolvedSchemaLessConditional = _objectWithoutPropertiesLoose(schema, _excluded$2);
564
565
  var conditionValue = validator.isValid(expression, formData || {}, rootSchema);
565
566
  var resolvedSchemas = [resolvedSchemaLessConditional];
566
567
  var schemas = [];
@@ -670,7 +671,7 @@ function resolveSchema(validator, schema, rootSchema, expandAllBranches, formDat
670
671
  function resolveReference(validator, schema, rootSchema, expandAllBranches, formData) {
671
672
  // Drop the $ref property of the source schema.
672
673
  var $ref = schema.$ref,
673
- localSchema = _objectWithoutPropertiesLoose(schema, _excluded2);
674
+ localSchema = _objectWithoutPropertiesLoose(schema, _excluded2$1);
674
675
  // Retrieve the referenced schema definition.
675
676
  var refSchema = findSchemaDefinition($ref, rootSchema);
676
677
  // Update referenced schema definition with local schema properties.
@@ -752,9 +753,9 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
752
753
  if (IF_KEY in resolvedSchema) {
753
754
  return resolveCondition(validator, resolvedSchema, rootSchema, expandAllBranches, rawFormData);
754
755
  }
755
- if (ALL_OF_KEY in schema) {
756
+ if (ALL_OF_KEY in resolvedSchema) {
756
757
  try {
757
- resolvedSchema = mergeAllOf(s, {
758
+ resolvedSchema = mergeAllOf(resolvedSchema, {
758
759
  deep: false
759
760
  });
760
761
  } catch (e) {
@@ -789,10 +790,14 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
789
790
  */
790
791
  function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranches, rawFormData) {
791
792
  var anyOrOneOf;
792
- if (Array.isArray(schema.oneOf)) {
793
- anyOrOneOf = schema.oneOf;
794
- } else if (Array.isArray(schema.anyOf)) {
795
- anyOrOneOf = schema.anyOf;
793
+ var _schema = schema,
794
+ oneOf = _schema.oneOf,
795
+ anyOf = _schema.anyOf,
796
+ remaining = _objectWithoutPropertiesLoose(_schema, _excluded4);
797
+ if (Array.isArray(oneOf)) {
798
+ anyOrOneOf = oneOf;
799
+ } else if (Array.isArray(anyOf)) {
800
+ anyOrOneOf = anyOf;
796
801
  }
797
802
  if (anyOrOneOf) {
798
803
  // Ensure that during expand all branches we pass an object rather than undefined so that all options are interrogated
@@ -808,9 +813,11 @@ function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranch
808
813
  // Call this to trigger the set of isValid() calls that the schema parser will need
809
814
  var option = getFirstMatchingOption(validator, formData, anyOrOneOf, rootSchema, discriminator);
810
815
  if (expandAllBranches) {
811
- return anyOrOneOf;
816
+ return anyOrOneOf.map(function (item) {
817
+ return _extends({}, remaining, item);
818
+ });
812
819
  }
813
- schema = anyOrOneOf[option];
820
+ schema = _extends({}, remaining, anyOrOneOf[option]);
814
821
  }
815
822
  return [schema];
816
823
  }
@@ -828,7 +835,7 @@ function resolveAnyOrOneOfSchemas(validator, schema, rootSchema, expandAllBranch
828
835
  function resolveDependencies(validator, schema, rootSchema, expandAllBranches, formData) {
829
836
  // Drop the dependencies from the source schema.
830
837
  var dependencies = schema.dependencies,
831
- remainingSchema = _objectWithoutPropertiesLoose(schema, _excluded4);
838
+ remainingSchema = _objectWithoutPropertiesLoose(schema, _excluded5);
832
839
  var resolvedSchemas = resolveAnyOrOneOfSchemas(validator, remainingSchema, rootSchema, expandAllBranches, formData);
833
840
  return resolvedSchemas.flatMap(function (resolvedSchema) {
834
841
  return processDependencies(validator, dependencies, resolvedSchema, rootSchema, expandAllBranches, formData);
@@ -911,7 +918,7 @@ function withDependentSchema(validator, schema, rootSchema, dependencyKey, depen
911
918
  var dependentSchemas = retrieveSchemaInternal(validator, dependencyValue, rootSchema, formData, expandAllBranches);
912
919
  return dependentSchemas.flatMap(function (dependent) {
913
920
  var oneOf = dependent.oneOf,
914
- dependentSchema = _objectWithoutPropertiesLoose(dependent, _excluded5);
921
+ dependentSchema = _objectWithoutPropertiesLoose(dependent, _excluded6);
915
922
  schema = mergeSchemas(schema, dependentSchema);
916
923
  // Since it does not contain oneOf, we return the original schema.
917
924
  if (oneOf === undefined) {
@@ -1026,8 +1033,10 @@ function calculateIndexScore(validator, rootSchema, schema, formData) {
1026
1033
  var newSchema = retrieveSchema(validator, value, rootSchema, formValue);
1027
1034
  return score + calculateIndexScore(validator, rootSchema, newSchema, formValue || {});
1028
1035
  }
1029
- if (has(value, ONE_OF_KEY) && formValue) {
1030
- return score + getClosestMatchingOption(validator, rootSchema, formValue, get(value, ONE_OF_KEY));
1036
+ if ((has(value, ONE_OF_KEY) || has(value, ANY_OF_KEY)) && formValue) {
1037
+ var _key = has(value, ONE_OF_KEY) ? ONE_OF_KEY : ANY_OF_KEY;
1038
+ var discriminator = getDiscriminatorFieldFromSchema(value);
1039
+ return score + getClosestMatchingOption(validator, rootSchema, formValue, get(value, _key), -1, discriminator);
1031
1040
  }
1032
1041
  if (value.type === 'object') {
1033
1042
  return score + calculateIndexScore(validator, rootSchema, value, formValue || {});
@@ -1081,8 +1090,15 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1081
1090
  if (selectedOption === void 0) {
1082
1091
  selectedOption = -1;
1083
1092
  }
1093
+ // First resolve any refs in the options
1094
+ var resolvedOptions = options.map(function (option) {
1095
+ if (has(option, REF_KEY)) {
1096
+ return retrieveSchema(validator, option, rootSchema, formData);
1097
+ }
1098
+ return option;
1099
+ });
1084
1100
  // Reduce the array of options down to a list of the indexes that are considered matching options
1085
- var allValidIndexes = options.reduce(function (validList, option, index) {
1101
+ var allValidIndexes = resolvedOptions.reduce(function (validList, option, index) {
1086
1102
  var testOptions = [JUNK_OPTION, option];
1087
1103
  var match = getFirstMatchingOption(validator, formData, testOptions, rootSchema, discriminatorField);
1088
1104
  // The match is the real option, so add its index to list of valid indexes
@@ -1097,7 +1113,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1097
1113
  }
1098
1114
  if (!allValidIndexes.length) {
1099
1115
  // No indexes were valid, so we'll score all the options, add all the indexes
1100
- times(options.length, function (i) {
1116
+ times(resolvedOptions.length, function (i) {
1101
1117
  return allValidIndexes.push(i);
1102
1118
  });
1103
1119
  }
@@ -1105,10 +1121,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1105
1121
  // Score all the options in the list of valid indexes and return the index with the best score
1106
1122
  var _allValidIndexes$redu = allValidIndexes.reduce(function (scoreData, index) {
1107
1123
  var bestScore = scoreData.bestScore;
1108
- var option = options[index];
1109
- if (has(option, REF_KEY)) {
1110
- option = retrieveSchema(validator, option, rootSchema, formData);
1111
- }
1124
+ var option = resolvedOptions[index];
1112
1125
  var score = calculateIndexScore(validator, rootSchema, option, formData);
1113
1126
  scoreCount.add(score);
1114
1127
  if (score > bestScore) {
@@ -1270,6 +1283,8 @@ function isMultiSelect(validator, schema, rootSchema) {
1270
1283
  return isSelect(validator, schema.items, rootSchema);
1271
1284
  }
1272
1285
 
1286
+ var _excluded$1 = ["oneOf"],
1287
+ _excluded2 = ["anyOf"];
1273
1288
  /** Enum that indicates how `schema.additionalItems` should be handled by the `getInnerSchemaForArrayItem()` function.
1274
1289
  */
1275
1290
  var AdditionalItemsHandling;
@@ -1432,17 +1447,23 @@ function computeDefaults(validator, rawSchema, _temp) {
1432
1447
  });
1433
1448
  });
1434
1449
  } else if (ONE_OF_KEY in schema) {
1435
- if (schema.oneOf.length === 0) {
1450
+ var oneOf = schema.oneOf,
1451
+ remaining = _objectWithoutPropertiesLoose(schema, _excluded$1);
1452
+ if (oneOf.length === 0) {
1436
1453
  return undefined;
1437
1454
  }
1438
1455
  var discriminator = getDiscriminatorFieldFromSchema(schema);
1439
- schemaToCompute = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, schema.oneOf, 0, discriminator)];
1456
+ schemaToCompute = oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, oneOf, 0, discriminator)];
1457
+ schemaToCompute = _extends({}, remaining, schemaToCompute);
1440
1458
  } else if (ANY_OF_KEY in schema) {
1441
- if (schema.anyOf.length === 0) {
1459
+ var anyOf = schema.anyOf,
1460
+ _remaining = _objectWithoutPropertiesLoose(schema, _excluded2);
1461
+ if (anyOf.length === 0) {
1442
1462
  return undefined;
1443
1463
  }
1444
1464
  var _discriminator = getDiscriminatorFieldFromSchema(schema);
1445
- schemaToCompute = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, schema.anyOf, 0, _discriminator)];
1465
+ schemaToCompute = anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, anyOf, 0, _discriminator)];
1466
+ schemaToCompute = _extends({}, _remaining, schemaToCompute);
1446
1467
  }
1447
1468
  if (schemaToCompute) {
1448
1469
  return computeDefaults(validator, schemaToCompute, {