@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/index.d.ts CHANGED
@@ -1317,7 +1317,8 @@ declare function getWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F e
1317
1317
  */
1318
1318
  declare function guessType(value: any): "array" | "string" | "null" | "boolean" | "number" | "object";
1319
1319
 
1320
- /** Stringifies the schema and returns the hash of the resulting string.
1320
+ /** Stringifies the schema and returns the hash of the resulting string. Sorts schema fields
1321
+ * in consistent order before stringify to prevent different hash ids for the same schema.
1321
1322
  *
1322
1323
  * @param schema - The schema for which the hash is desired
1323
1324
  * @returns - The string obtained from the hash of the stringified schema
@@ -1645,6 +1646,7 @@ declare const ERRORS_KEY = "__errors";
1645
1646
  declare const ID_KEY = "$id";
1646
1647
  declare const IF_KEY = "if";
1647
1648
  declare const ITEMS_KEY = "items";
1649
+ declare const JUNK_OPTION_ID = "_$junk_option_schema_id$_";
1648
1650
  declare const NAME_KEY = "$name";
1649
1651
  declare const ONE_OF_KEY = "oneOf";
1650
1652
  declare const PROPERTIES_KEY = "properties";
@@ -1877,4 +1879,4 @@ declare function toIdSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F
1877
1879
  */
1878
1880
  declare function toPathSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, name?: string, rootSchema?: S, formData?: T): PathSchema<T>;
1879
1881
 
1880
- export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, ArrayFieldDescriptionProps, ArrayFieldTemplateItemType, ArrayFieldTemplateProps, ArrayFieldTitleProps, BaseInputTemplateProps, CONST_KEY, CustomValidator, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, DateObject, DescriptionFieldProps, ENUM_KEY, ERRORS_KEY, EnumOptionsType, ErrorListProps, ErrorSchema, ErrorSchemaBuilder, ErrorTransformer, Experimental_DefaultFormStateBehavior, Field, FieldError, FieldErrorProps, FieldErrors, FieldHelpProps, FieldId, FieldPath, FieldProps, FieldTemplateProps, FieldValidation, FormContextType, FormValidation, GenericObjectType, GlobalUISchemaOptions, ID_KEY, IF_KEY, ITEMS_KEY, IconButtonProps, IdSchema, InputPropsType, NAME_KEY, ONE_OF_KEY, ObjectFieldTemplatePropertyType, ObjectFieldTemplateProps, PROPERTIES_KEY, PathSchema, REF_KEY, REQUIRED_KEY, RJSFSchema, RJSFValidationError, RJSF_ADDITONAL_PROPERTIES_FLAG, ROOT_SCHEMA_PREFIX, RangeSpecType, Registry, RegistryFieldsType, RegistryWidgetsType, SUBMIT_BTN_OPTIONS_KEY, SchemaMap, SchemaUtilsType, StrictRJSFSchema, SubmitButtonProps, TemplatesType, TitleFieldProps, TranslatableString, UIOptionsType, UISchemaSubmitButtonOptions, UI_FIELD_KEY, UI_GLOBAL_OPTIONS_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, UiSchema, UnsupportedFieldProps, ValidationData, ValidatorType, Widget, WidgetProps, WrapIfAdditionalTemplateProps, 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 };
1882
+ export { ADDITIONAL_PROPERTIES_KEY, ADDITIONAL_PROPERTY_FLAG, ALL_OF_KEY, ANY_OF_KEY, ArrayFieldDescriptionProps, ArrayFieldTemplateItemType, ArrayFieldTemplateProps, ArrayFieldTitleProps, BaseInputTemplateProps, CONST_KEY, CustomValidator, DEFAULT_KEY, DEFINITIONS_KEY, DEPENDENCIES_KEY, DateObject, DescriptionFieldProps, ENUM_KEY, ERRORS_KEY, EnumOptionsType, ErrorListProps, ErrorSchema, ErrorSchemaBuilder, ErrorTransformer, Experimental_DefaultFormStateBehavior, Field, FieldError, FieldErrorProps, FieldErrors, FieldHelpProps, FieldId, FieldPath, FieldProps, FieldTemplateProps, FieldValidation, FormContextType, FormValidation, GenericObjectType, GlobalUISchemaOptions, ID_KEY, IF_KEY, ITEMS_KEY, IconButtonProps, IdSchema, InputPropsType, JUNK_OPTION_ID, NAME_KEY, ONE_OF_KEY, ObjectFieldTemplatePropertyType, ObjectFieldTemplateProps, PROPERTIES_KEY, PathSchema, REF_KEY, REQUIRED_KEY, RJSFSchema, RJSFValidationError, RJSF_ADDITONAL_PROPERTIES_FLAG, ROOT_SCHEMA_PREFIX, RangeSpecType, Registry, RegistryFieldsType, RegistryWidgetsType, SUBMIT_BTN_OPTIONS_KEY, SchemaMap, SchemaUtilsType, StrictRJSFSchema, SubmitButtonProps, TemplatesType, TitleFieldProps, TranslatableString, UIOptionsType, UISchemaSubmitButtonOptions, UI_FIELD_KEY, UI_GLOBAL_OPTIONS_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, UiSchema, UnsupportedFieldProps, ValidationData, ValidatorType, Widget, WidgetProps, WrapIfAdditionalTemplateProps, 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 };
@@ -17,6 +17,7 @@ var set = require('lodash/set');
17
17
  var mergeAllOf = require('json-schema-merge-allof');
18
18
  var union = require('lodash/union');
19
19
  var isEqual = require('lodash/isEqual');
20
+ var lodash = require('lodash');
20
21
  var cloneDeep = require('lodash/cloneDeep');
21
22
  var jsxRuntime = require('react/jsx-runtime');
22
23
  var react = require('react');
@@ -190,6 +191,7 @@ var ERRORS_KEY = '__errors';
190
191
  var ID_KEY = '$id';
191
192
  var IF_KEY = 'if';
192
193
  var ITEMS_KEY = 'items';
194
+ var JUNK_OPTION_ID = '_$junk_option_schema_id$_';
193
195
  var NAME_KEY = '$name';
194
196
  var ONE_OF_KEY = 'oneOf';
195
197
  var PROPERTIES_KEY = 'properties';
@@ -585,7 +587,7 @@ function resolveCondition(validator, schema, rootSchema, expandAllBranches, form
585
587
  then = schema.then,
586
588
  otherwise = schema["else"],
587
589
  resolvedSchemaLessConditional = _objectWithoutPropertiesLoose(schema, _excluded$1);
588
- var conditionValue = validator.isValid(expression, formData, rootSchema);
590
+ var conditionValue = validator.isValid(expression, formData || {}, rootSchema);
589
591
  var resolvedSchemas = [resolvedSchemaLessConditional];
590
592
  var schemas = [];
591
593
  if (expandAllBranches) {
@@ -784,7 +786,11 @@ function retrieveSchemaInternal(validator, schema, rootSchema, rawFormData, expa
784
786
  } catch (e) {
785
787
  console.warn('could not merge subschemas in allOf:\n', e);
786
788
  var _resolvedSchema = resolvedSchema,
789
+ allOf = _resolvedSchema.allOf,
787
790
  resolvedSchemaWithoutAllOf = _objectWithoutPropertiesLoose(_resolvedSchema, _excluded3);
791
+ if (expandAllBranches && allOf) {
792
+ return [resolvedSchemaWithoutAllOf].concat(allOf);
793
+ }
788
794
  return resolvedSchemaWithoutAllOf;
789
795
  }
790
796
  }
@@ -1003,6 +1009,7 @@ function withExactlyOneSubschema(validator, schema, rootSchema, dependencyKey, o
1003
1009
  */
1004
1010
  var JUNK_OPTION = {
1005
1011
  type: 'object',
1012
+ $id: JUNK_OPTION_ID,
1006
1013
  properties: {
1007
1014
  __not_really_there__: {
1008
1015
  type: 'number'
@@ -1120,6 +1127,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1120
1127
  return allValidIndexes.push(i);
1121
1128
  });
1122
1129
  }
1130
+ var scoreCount = new Set();
1123
1131
  // Score all the options in the list of valid indexes and return the index with the best score
1124
1132
  var _allValidIndexes$redu = allValidIndexes.reduce(function (scoreData, index) {
1125
1133
  var bestScore = scoreData.bestScore;
@@ -1128,6 +1136,7 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1128
1136
  option = retrieveSchema(validator, option, rootSchema, formData);
1129
1137
  }
1130
1138
  var score = calculateIndexScore(validator, rootSchema, option, formData);
1139
+ scoreCount.add(score);
1131
1140
  if (score > bestScore) {
1132
1141
  return {
1133
1142
  bestIndex: index,
@@ -1140,6 +1149,10 @@ function getClosestMatchingOption(validator, rootSchema, formData, options, sele
1140
1149
  bestScore: 0
1141
1150
  }),
1142
1151
  bestIndex = _allValidIndexes$redu.bestIndex;
1152
+ // if all scores are the same go with selectedOption
1153
+ if (scoreCount.size === 1 && selectedOption >= 0) {
1154
+ return selectedOption;
1155
+ }
1143
1156
  return bestIndex;
1144
1157
  }
1145
1158
 
@@ -2430,7 +2443,7 @@ function enumOptionsSelectValue(valueIndex, selected, allEnumOptions) {
2430
2443
  allEnumOptions = [];
2431
2444
  }
2432
2445
  var value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2433
- if (value) {
2446
+ if (!lodash.isNil(value)) {
2434
2447
  var index = allEnumOptions.findIndex(function (opt) {
2435
2448
  return value === opt.value;
2436
2449
  });
@@ -2800,13 +2813,19 @@ function hashString(string) {
2800
2813
 
2801
2814
  return hash.toString(16);
2802
2815
  }
2803
- /** Stringifies the schema and returns the hash of the resulting string.
2816
+ /** Stringifies the schema and returns the hash of the resulting string. Sorts schema fields
2817
+ * in consistent order before stringify to prevent different hash ids for the same schema.
2804
2818
  *
2805
2819
  * @param schema - The schema for which the hash is desired
2806
2820
  * @returns - The string obtained from the hash of the stringified schema
2807
2821
  */
2808
2822
  function hashForSchema(schema) {
2809
- return hashString(JSON.stringify(schema));
2823
+ var allKeys = new Set();
2824
+ // solution source: https://stackoverflow.com/questions/16167581/sort-object-properties-and-json-stringify/53593328#53593328
2825
+ JSON.stringify(schema, function (key, value) {
2826
+ return allKeys.add(key), value;
2827
+ });
2828
+ return hashString(JSON.stringify(schema, Array.from(allKeys).sort()));
2810
2829
  }
2811
2830
 
2812
2831
  /** Detects whether the `widget` exists for the `schema` with the associated `registryWidgets` and returns true if it
@@ -3531,6 +3550,9 @@ function parseSchema(validator, recurseList, rootSchema, schema) {
3531
3550
  });
3532
3551
  }
3533
3552
  });
3553
+ if (ITEMS_KEY in schema && !Array.isArray(schema.items) && typeof schema.items !== 'boolean') {
3554
+ parseSchema(validator, recurseList, rootSchema, schema.items);
3555
+ }
3534
3556
  }
3535
3557
  });
3536
3558
  }
@@ -3561,6 +3583,7 @@ exports.ErrorSchemaBuilder = ErrorSchemaBuilder;
3561
3583
  exports.ID_KEY = ID_KEY;
3562
3584
  exports.IF_KEY = IF_KEY;
3563
3585
  exports.ITEMS_KEY = ITEMS_KEY;
3586
+ exports.JUNK_OPTION_ID = JUNK_OPTION_ID;
3564
3587
  exports.NAME_KEY = NAME_KEY;
3565
3588
  exports.ONE_OF_KEY = ONE_OF_KEY;
3566
3589
  exports.PROPERTIES_KEY = PROPERTIES_KEY;