@rjsf/utils 5.0.2 → 5.2.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
@@ -1071,17 +1071,48 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
1071
1071
  }
1072
1072
  return {};
1073
1073
  }
1074
+ /** Either add `computedDefault` at `key` into `obj` or not add it based on its value and the value of
1075
+ * `includeUndefinedValues`. Generally undefined `computedDefault` values are added only when `includeUndefinedValues`
1076
+ * is either true or "excludeObjectChildren". If `includeUndefinedValues` is false, then non-undefined and
1077
+ * non-empty-object values will be added.
1078
+ *
1079
+ * @param obj - The object into which the computed default may be added
1080
+ * @param key - The key into the object at which the computed default may be added
1081
+ * @param computedDefault - The computed default value that maybe should be added to the obj
1082
+ * @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
1083
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1084
+ * false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
1085
+ * values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
1086
+ * false when computing defaults for any nested object properties.
1087
+ * @param requiredFields - The list of fields that are required
1088
+ */
1089
+ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
1090
+ if (requiredFields === void 0) {
1091
+ requiredFields = [];
1092
+ }
1093
+ if (includeUndefinedValues) {
1094
+ obj[key] = computedDefault;
1095
+ } else if (isObject(computedDefault)) {
1096
+ // Store computedDefault if it's a non-empty object (e.g. not {})
1097
+ if (!isEmpty(computedDefault) || requiredFields.includes(key)) {
1098
+ obj[key] = computedDefault;
1099
+ }
1100
+ } else if (computedDefault !== undefined) {
1101
+ // Store computedDefault if it's a defined primitive (e.g. true)
1102
+ obj[key] = computedDefault;
1103
+ }
1104
+ }
1074
1105
  /** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
1075
1106
  * each level of the schema, recursively, to fill out every level of defaults provided by the schema.
1076
1107
  *
1077
1108
  * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
1078
- * @param schema - The schema for which the default state is desired
1109
+ * @param rawSchema - The schema for which the default state is desired
1079
1110
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
1080
1111
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
1081
1112
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
1082
1113
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1083
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1084
- * object properties.
1114
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1115
+ * false when computing defaults for any nested object properties.
1085
1116
  * @returns - The resulting `formData` with all the defaults provided
1086
1117
  */
1087
1118
  function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -1113,8 +1144,14 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1113
1144
  return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1114
1145
  });
1115
1146
  } else if (ONE_OF_KEY in schema) {
1147
+ if (schema.oneOf.length === 0) {
1148
+ return undefined;
1149
+ }
1116
1150
  schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, schema.oneOf, 0)];
1117
1151
  } else if (ANY_OF_KEY in schema) {
1152
+ if (schema.anyOf.length === 0) {
1153
+ return undefined;
1154
+ }
1118
1155
  schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty(formData) ? undefined : formData, schema.anyOf, 0)];
1119
1156
  }
1120
1157
  // Not defaults defined for this node, fallback to generic typed ones.
@@ -1124,23 +1161,25 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1124
1161
  switch (getSchemaType(schema)) {
1125
1162
  // We need to recur for object schema inner default values.
1126
1163
  case "object":
1127
- return Object.keys(schema.properties || {}).reduce(function (acc, key) {
1128
- // Compute the defaults for this node, with the parent defaults we might
1129
- // have from a previous run: defaults[key].
1130
- var computedDefault = computeDefaults(validator, get(schema, [PROPERTIES_KEY, key]), get(defaults, [key]), rootSchema, get(formData, [key]), includeUndefinedValues === "excludeObjectChildren" ? false : includeUndefinedValues);
1131
- if (includeUndefinedValues) {
1132
- acc[key] = computedDefault;
1133
- } else if (isObject(computedDefault)) {
1134
- // Store computedDefault if it's a non-empty object (e.g. not {})
1135
- if (!isEmpty(computedDefault)) {
1136
- acc[key] = computedDefault;
1137
- }
1138
- } else if (computedDefault !== undefined) {
1139
- // Store computedDefault if it's a defined primitive (e.g. true)
1140
- acc[key] = computedDefault;
1164
+ {
1165
+ var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1166
+ // Compute the defaults for this node, with the parent defaults we might
1167
+ // have from a previous run: defaults[key].
1168
+ var computedDefault = computeDefaults(validator, get(schema, [PROPERTIES_KEY, key]), get(defaults, [key]), rootSchema, get(formData, [key]), includeUndefinedValues === true);
1169
+ maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1170
+ return acc;
1171
+ }, {});
1172
+ if (schema.additionalProperties && isObject(defaults)) {
1173
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1174
+ Object.keys(defaults).filter(function (key) {
1175
+ return !schema.properties || !schema.properties[key];
1176
+ }).forEach(function (key) {
1177
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get(defaults, [key]), rootSchema, get(formData, [key]), includeUndefinedValues === true);
1178
+ maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1179
+ });
1141
1180
  }
1142
- return acc;
1143
- }, {});
1181
+ return objectDefaults;
1182
+ }
1144
1183
  case "array":
1145
1184
  // Inject defaults into existing array defaults
1146
1185
  if (Array.isArray(defaults)) {
@@ -1182,8 +1221,8 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1182
1221
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1183
1222
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1184
1223
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1185
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1186
- * object properties.
1224
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1225
+ * false when computing defaults for any nested object properties.
1187
1226
  * @returns - The resulting `formData` with all the defaults provided
1188
1227
  */
1189
1228
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
@@ -1805,6 +1844,36 @@ function dataURItoBlob(dataURI) {
1805
1844
  };
1806
1845
  }
1807
1846
 
1847
+ /** Potentially substitutes all replaceable parameters with the associated value(s) from the `params` if available. When
1848
+ * a `params` array is provided, each value in the array is used to replace any of the replaceable parameters in the
1849
+ * `inputString` using the `%1`, `%2`, etc. replacement specifiers.
1850
+ *
1851
+ * @param inputString - The string which will be potentially updated with replacement parameters
1852
+ * @param params - The optional list of replaceable parameter values to substitute into the english string
1853
+ * @returns - The updated string with any replacement specifiers replaced
1854
+ */
1855
+ function replaceStringParameters(inputString, params) {
1856
+ var output = inputString;
1857
+ if (Array.isArray(params)) {
1858
+ params.forEach(function (param, index) {
1859
+ output = output.replace("%" + (index + 1), param);
1860
+ });
1861
+ }
1862
+ return output;
1863
+ }
1864
+
1865
+ /** Translates a `TranslatableString` value `stringToTranslate` into english. When a `params` array is provided, each
1866
+ * value in the array is used to replace any of the replaceable parameters in the `stringToTranslate` using the `%1`,
1867
+ * `%2`, etc. replacement specifiers.
1868
+ *
1869
+ * @param stringToTranslate - The `TranslatableString` value to convert to english
1870
+ * @param params - The optional list of replaceable parameter values to substitute into the english string
1871
+ * @returns - The `stringToTranslate` itself with any replaceable parameter values substituted
1872
+ */
1873
+ function englishStringTranslator(stringToTranslate, params) {
1874
+ return replaceStringParameters(stringToTranslate, params);
1875
+ }
1876
+
1808
1877
  /** Returns the value(s) from `allEnumOptions` at the index(es) provided by `valueIndex`. If `valueIndex` is not an
1809
1878
  * array AND the index is not valid for `allEnumOptions`, `emptyValue` is returned. If `valueIndex` is an array, AND it
1810
1879
  * contains an invalid index, the returned array will have the resulting undefined values filtered out, leaving only
@@ -2624,5 +2693,75 @@ function utcToLocal(jsonDate) {
2624
2693
  return yyyy + "-" + MM + "-" + dd + "T" + hh + ":" + mm + ":" + ss + "." + SSS;
2625
2694
  }
2626
2695
 
2627
- 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, ITEMS_KEY, NAME_KEY, ONE_OF_KEY, PROPERTIES_KEY, REF_KEY, REQUIRED_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG, SUBMIT_BTN_OPTIONS_KEY, UI_FIELD_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, enumOptionsDeselectValue, enumOptionsIndexForValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, errorId, examplesId, findSchemaDefinition, getClosestMatchingOption, getDefaultFormState, getDisplayLabel, getFirstMatchingOption, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, rangeSpec, retrieveSchema, sanitizeDataForNewSchema, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toIdSchema, toPathSchema, utcToLocal };
2696
+ /** An enumeration of all the translatable strings used by `@rjsf/core` and its themes. The value of each of the
2697
+ * enumeration keys is expected to be the actual english string. Some strings contain replaceable parameter values
2698
+ * as indicated by `%1`, `%2`, etc. The number after the `%` indicates the order of the parameter. The ordering of
2699
+ * parameters is important because some languages may choose to put the second parameter before the first in its
2700
+ * translation. Also, some strings are rendered using `markdown-to-jsx` and thus support markdown and inline html.
2701
+ */
2702
+ var TranslatableString;
2703
+ (function (TranslatableString) {
2704
+ /** Fallback title of an array item, used by ArrayField */
2705
+ TranslatableString["ArrayItemTitle"] = "Item";
2706
+ /** Missing items reason, used by ArrayField */
2707
+ TranslatableString["MissingItems"] = "Missing items definition";
2708
+ /** Yes label, used by BooleanField */
2709
+ TranslatableString["YesLabel"] = "Yes";
2710
+ /** No label, used by BooleanField */
2711
+ TranslatableString["NoLabel"] = "No";
2712
+ /** Close label, used by ErrorList */
2713
+ TranslatableString["CloseLabel"] = "Close";
2714
+ /** Errors label, used by ErrorList */
2715
+ TranslatableString["ErrorsLabel"] = "Errors";
2716
+ /** New additionalProperties string default value, used by ObjectField */
2717
+ TranslatableString["NewStringDefault"] = "New Value";
2718
+ /** Add button title, used by AddButton */
2719
+ TranslatableString["AddButton"] = "Add";
2720
+ /** Add button title, used by AddButton */
2721
+ TranslatableString["AddItemButton"] = "Add Item";
2722
+ /** Move down button title, used by IconButton */
2723
+ TranslatableString["MoveDownButton"] = "Move down";
2724
+ /** Move up button title, used by IconButton */
2725
+ TranslatableString["MoveUpButton"] = "Move up";
2726
+ /** Remove button title, used by IconButton */
2727
+ TranslatableString["RemoveButton"] = "Remove";
2728
+ /** Now label, used by AltDateWidget */
2729
+ TranslatableString["NowLabel"] = "Now";
2730
+ /** Clear label, used by AltDateWidget */
2731
+ TranslatableString["ClearLabel"] = "Clear";
2732
+ /** Aria date label, used by DateWidget */
2733
+ TranslatableString["AriaDateLabel"] = "Select a date";
2734
+ /** Decrement button aria label, used by UpDownWidget */
2735
+ TranslatableString["DecrementAriaLabel"] = "Decrease value by 1";
2736
+ /** Increment button aria label, used by UpDownWidget */
2737
+ TranslatableString["IncrementAriaLabel"] = "Increase value by 1";
2738
+ // Strings with replaceable parameters
2739
+ /** Unknown field type reason, where %1 will be replaced with the type as provided by SchemaField */
2740
+ TranslatableString["UnknownFieldType"] = "Unknown field type %1";
2741
+ /** Option prefix, where %1 will be replaced with the option index as provided by MultiSchemaField */
2742
+ TranslatableString["OptionPrefix"] = "Option %1";
2743
+ /** Option prefix, where %1 and %2 will be replaced by the schema title and option index, respectively as provided by
2744
+ * MultiSchemaField
2745
+ */
2746
+ TranslatableString["TitleOptionPrefix"] = "%1 option %2";
2747
+ /** Key label, where %1 will be replaced by the label as provided by WrapIfAdditionalTemplate */
2748
+ TranslatableString["KeyLabel"] = "%1 Key";
2749
+ // Strings with replaceable parameters AND/OR that support markdown and html
2750
+ /** Unsupported field schema, used by UnsupportedField */
2751
+ TranslatableString["UnsupportedField"] = "Unsupported field schema.";
2752
+ /** Unsupported field schema, where %1 will be replaced by the idSchema.$id as provided by UnsupportedField */
2753
+ TranslatableString["UnsupportedFieldWithId"] = "Unsupported field schema for field <code>%1</code>.";
2754
+ /** Unsupported field schema, where %1 will be replaced by the reason string as provided by UnsupportedField */
2755
+ TranslatableString["UnsupportedFieldWithReason"] = "Unsupported field schema: <em>%1</em>.";
2756
+ /** Unsupported field schema, where %1 and %2 will be replaced by the idSchema.$id and reason strings, respectively,
2757
+ * as provided by UnsupportedField
2758
+ */
2759
+ TranslatableString["UnsupportedFieldWithIdAndReason"] = "Unsupported field schema for field <code>%1</code>: <em>%2</em>.";
2760
+ /** File name, type and size info, where %1, %2 and %3 will be replaced by the file name, file type and file size as
2761
+ * provided by FileWidget
2762
+ */
2763
+ TranslatableString["FilesInfo"] = "<strong>%1</strong> (%2, %3 bytes)";
2764
+ })(TranslatableString || (TranslatableString = {}));
2765
+
2766
+ 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, ITEMS_KEY, NAME_KEY, ONE_OF_KEY, PROPERTIES_KEY, REF_KEY, REQUIRED_KEY, RJSF_ADDITONAL_PROPERTIES_FLAG, SUBMIT_BTN_OPTIONS_KEY, TranslatableString, UI_FIELD_KEY, UI_OPTIONS_KEY, UI_WIDGET_KEY, allowAdditionalItems, ariaDescribedByIds, asNumber, canExpand, createSchemaUtils, dataURItoBlob, deepEquals, descriptionId, englishStringTranslator, enumOptionsDeselectValue, enumOptionsIndexForValue, enumOptionsIsSelected, enumOptionsSelectValue, enumOptionsValueForIndex, errorId, examplesId, findSchemaDefinition, getClosestMatchingOption, getDefaultFormState, getDisplayLabel, getFirstMatchingOption, getInputProps, getMatchingOption, getSchemaType, getSubmitButtonOptions, getTemplate, getUiOptions, getWidget, guessType, hasWidget, helpId, isConstant, isCustomWidget, isFilesArray, isFixedItems, isMultiSelect, isObject, isSelect, localToUTC, mergeDefaultsWithFormData, mergeObjects, mergeSchemas, mergeValidationData, optionId, optionsList, orderProperties, pad, parseDateString, rangeSpec, replaceStringParameters, retrieveSchema, sanitizeDataForNewSchema, schemaRequiresTrueValue, shouldRender, titleId, toConstant, toDateString, toIdSchema, toPathSchema, utcToLocal };
2628
2767
  //# sourceMappingURL=utils.esm.js.map