@rjsf/utils 5.7.2 → 5.8.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
@@ -13,6 +13,7 @@ import set from 'lodash-es/set';
13
13
  import mergeAllOf from 'json-schema-merge-allof';
14
14
  import union from 'lodash-es/union';
15
15
  import isEqual from 'lodash-es/isEqual';
16
+ import { isNil } from 'lodash-es';
16
17
  import cloneDeep from 'lodash-es/cloneDeep';
17
18
  import { jsx } from 'react/jsx-runtime';
18
19
  import { createElement } from 'react';
@@ -164,6 +165,7 @@ var ERRORS_KEY = '__errors';
164
165
  var ID_KEY = '$id';
165
166
  var IF_KEY = 'if';
166
167
  var ITEMS_KEY = 'items';
168
+ var JUNK_OPTION_ID = '_$junk_option_schema_id$_';
167
169
  var NAME_KEY = '$name';
168
170
  var ONE_OF_KEY = 'oneOf';
169
171
  var PROPERTIES_KEY = 'properties';
@@ -559,7 +561,7 @@ function resolveCondition(validator, schema, rootSchema, expandAllBranches, form
559
561
  then = schema.then,
560
562
  otherwise = schema["else"],
561
563
  resolvedSchemaLessConditional = _objectWithoutPropertiesLoose(schema, _excluded$1);
562
- var conditionValue = validator.isValid(expression, formData, rootSchema);
564
+ var conditionValue = validator.isValid(expression, formData || {}, rootSchema);
563
565
  var resolvedSchemas = [resolvedSchemaLessConditional];
564
566
  var schemas = [];
565
567
  if (expandAllBranches) {
@@ -758,7 +760,11 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
758
760
  } catch (e) {
759
761
  console.warn('could not merge subschemas in allOf:\n', e);
760
762
  var _resolvedSchema = resolvedSchema,
763
+ allOf = _resolvedSchema.allOf,
761
764
  resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
765
+ if (expandAllBranches && allOf) {
766
+ return [resolvedSchemaWithoutAllOf].concat(allOf);
767
+ }
762
768
  return resolvedSchemaWithoutAllOf;
763
769
  }
764
770
  }
@@ -977,6 +983,7 @@ function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, o
977
983
  */
978
984
  var JUNK_OPTION = {
979
985
  type: 'object',
986
+ $id: JUNK_OPTION_ID,
980
987
  properties: {
981
988
  __not_really_there__: {
982
989
  type: 'number'
@@ -1094,6 +1101,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1094
1101
  return allValidIndexes.push(i);
1095
1102
  });
1096
1103
  }
1104
+ var scoreCount = new Set();
1097
1105
  // Score all the options in the list of valid indexes and return the index with the best score
1098
1106
  var _allValidIndexes$redu = allValidIndexes.reduce(function (scoreData, index) {
1099
1107
  var bestScore = scoreData.bestScore;
@@ -1102,6 +1110,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1102
1110
  option = retrieveSchema(validator, option, rootSchema, formData);
1103
1111
  }
1104
1112
  var score = calculateIndexScore(validator, rootSchema, option, formData);
1113
+ scoreCount.add(score);
1105
1114
  if (score > bestScore) {
1106
1115
  return {
1107
1116
  bestIndex: index,
@@ -1114,6 +1123,10 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1114
1123
  bestScore: 0
1115
1124
  }),
1116
1125
  bestIndex = _allValidIndexes$redu.bestIndex;
1126
+ // if all scores are the same go with selectedOption
1127
+ if (scoreCount.size === 1 && selectedOption >= 0) {
1128
+ return selectedOption;
1129
+ }
1117
1130
  return bestIndex;
1118
1131
  }
1119
1132
 
@@ -2404,7 +2417,7 @@ function enumOptionsSelectValue(valueIndex, selected, allEnumOptions) {
2404
2417
  allEnumOptions = [];
2405
2418
  }
2406
2419
  var value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2407
- if (value) {
2420
+ if (!isNil(value)) {
2408
2421
  var index = allEnumOptions.findIndex(function (opt) {
2409
2422
  return value === opt.value;
2410
2423
  });
@@ -2774,13 +2787,19 @@ function hashString(string) {
2774
2787
 
2775
2788
  return hash.toString(16);
2776
2789
  }
2777
- /** Stringifies the schema and returns the hash of the resulting string.
2790
+ /** Stringifies the schema and returns the hash of the resulting string. Sorts schema fields
2791
+ * in consistent order before stringify to prevent different hash ids for the same schema.
2778
2792
  *
2779
2793
  * @param schema - The schema for which the hash is desired
2780
2794
  * @returns - The string obtained from the hash of the stringified schema
2781
2795
  */
2782
2796
  function hashForSchema(schema) {
2783
- return hashString(JSON.stringify(schema));
2797
+ var allKeys = new Set();
2798
+ // solution source: https://stackoverflow.com/questions/16167581/sort-object-properties-and-json-stringify/53593328#53593328
2799
+ JSON.stringify(schema, function (key, value) {
2800
+ return allKeys.add(key), value;
2801
+ });
2802
+ return hashString(JSON.stringify(schema, Array.from(allKeys).sort()));
2784
2803
  }
2785
2804
 
2786
2805
  /** Detects whether the `widget` exists for the `schema` with the associated `registryWidgets` and returns true if it
@@ -3505,6 +3524,9 @@ function parseSchema(validator, recurseList, rootSchema, schema) {
3505
3524
  });
3506
3525
  }
3507
3526
  });
3527
+ if (ITEMS_KEY in schema && !Array.isArray(schema.items) && typeof schema.items !== 'boolean') {
3528
+ parseSchema(validator, recurseList, rootSchema, schema.items);
3529
+ }
3508
3530
  }
3509
3531
  });
3510
3532
  }
@@ -3521,5 +3543,5 @@ function schemaParser(rootSchema) {
3521
3543
  return validator.getSchemaMap();
3522
3544
  }
3523
3545
 
3524
- export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, CONST_KEY, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, ENUM_KEY, ERRORS_KEY, ErrorSchemaBuilder, ID_KEY, IF_KEY, ITEMS_KEY, NAME_KEY, ONE_OF_KEY, PROPERTIES_KEY, REF_KEY, REQUIRED_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG, ROOT_SCHEMA_PREFIX, SUBMIT_BTN_OPTIONS_KEY, TranslatableString, UI_FIELD_KEY, UI_GLOBAL_OPTIONS_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createErrorHandler, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, englishStringTranslator, enumOptionsDeselectValue, enumOptionsIndexForValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, errorId, examplesId, findSchemaDefinition, getClosestMatchingOption, getDefaultFormState, getDiscriminatorFieldFromSchema, getDisplayLabel, getFirstMatchingOption, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, hashForSchema, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, labelValue, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, rangeSpec, replaceStringParameters, retrieveSchema, sanitizeDataForNewSchema, schemaParser, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toErrorList, toErrorSchema, toIdSchema, toPathSchema, unwrapErrorHandler, utcToLocal, validationDataMerge, withIdRefPrefix };
3546
+ export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, CONST_KEY, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, ENUM_KEY, ERRORS_KEY, ErrorSchemaBuilder, ID_KEY, IF_KEY, ITEMS_KEY, JUNK_OPTION_ID, NAME_KEY, ONE_OF_KEY, PROPERTIES_KEY, REF_KEY, REQUIRED_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG, ROOT_SCHEMA_PREFIX, SUBMIT_BTN_OPTIONS_KEY, TranslatableString, UI_FIELD_KEY, UI_GLOBAL_OPTIONS_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createErrorHandler, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, englishStringTranslator, enumOptionsDeselectValue, enumOptionsIndexForValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, errorId, examplesId, findSchemaDefinition, getClosestMatchingOption, getDefaultFormState, getDiscriminatorFieldFromSchema, getDisplayLabel, getFirstMatchingOption, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, hashForSchema, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, labelValue, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, rangeSpec, replaceStringParameters, retrieveSchema, sanitizeDataForNewSchema, schemaParser, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toErrorList, toErrorSchema, toIdSchema, toPathSchema, unwrapErrorHandler, utcToLocal, validationDataMerge, withIdRefPrefix };
3525
3547
  //# sourceMappingURL=utils.esm.js.map