@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.
@@ -1079,17 +1079,48 @@
1079
1079
  }
1080
1080
  return {};
1081
1081
  }
1082
+ /** Either add `computedDefault` at `key` into `obj` or not add it based on its value and the value of
1083
+ * `includeUndefinedValues`. Generally undefined `computedDefault` values are added only when `includeUndefinedValues`
1084
+ * is either true or "excludeObjectChildren". If `includeUndefinedValues` is false, then non-undefined and
1085
+ * non-empty-object values will be added.
1086
+ *
1087
+ * @param obj - The object into which the computed default may be added
1088
+ * @param key - The key into the object at which the computed default may be added
1089
+ * @param computedDefault - The computed default value that maybe should be added to the obj
1090
+ * @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
1091
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1092
+ * false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
1093
+ * values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
1094
+ * false when computing defaults for any nested object properties.
1095
+ * @param requiredFields - The list of fields that are required
1096
+ */
1097
+ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
1098
+ if (requiredFields === void 0) {
1099
+ requiredFields = [];
1100
+ }
1101
+ if (includeUndefinedValues) {
1102
+ obj[key] = computedDefault;
1103
+ } else if (isObject(computedDefault)) {
1104
+ // Store computedDefault if it's a non-empty object (e.g. not {})
1105
+ if (!isEmpty__default["default"](computedDefault) || requiredFields.includes(key)) {
1106
+ obj[key] = computedDefault;
1107
+ }
1108
+ } else if (computedDefault !== undefined) {
1109
+ // Store computedDefault if it's a defined primitive (e.g. true)
1110
+ obj[key] = computedDefault;
1111
+ }
1112
+ }
1082
1113
  /** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
1083
1114
  * each level of the schema, recursively, to fill out every level of defaults provided by the schema.
1084
1115
  *
1085
1116
  * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
1086
- * @param schema - The schema for which the default state is desired
1117
+ * @param rawSchema - The schema for which the default state is desired
1087
1118
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
1088
1119
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
1089
1120
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
1090
1121
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1091
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1092
- * object properties.
1122
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1123
+ * false when computing defaults for any nested object properties.
1093
1124
  * @returns - The resulting `formData` with all the defaults provided
1094
1125
  */
1095
1126
  function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -1121,8 +1152,14 @@
1121
1152
  return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1122
1153
  });
1123
1154
  } else if (ONE_OF_KEY in schema) {
1155
+ if (schema.oneOf.length === 0) {
1156
+ return undefined;
1157
+ }
1124
1158
  schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0)];
1125
1159
  } else if (ANY_OF_KEY in schema) {
1160
+ if (schema.anyOf.length === 0) {
1161
+ return undefined;
1162
+ }
1126
1163
  schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0)];
1127
1164
  }
1128
1165
  // Not defaults defined for this node, fallback to generic typed ones.
@@ -1132,23 +1169,25 @@
1132
1169
  switch (getSchemaType(schema)) {
1133
1170
  // We need to recur for object schema inner default values.
1134
1171
  case "object":
1135
- return Object.keys(schema.properties || {}).reduce(function (acc, key) {
1136
- // Compute the defaults for this node, with the parent defaults we might
1137
- // have from a previous run: defaults[key].
1138
- var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === "excludeObjectChildren" ? false : includeUndefinedValues);
1139
- if (includeUndefinedValues) {
1140
- acc[key] = computedDefault;
1141
- } else if (isObject(computedDefault)) {
1142
- // Store computedDefault if it's a non-empty object (e.g. not {})
1143
- if (!isEmpty__default["default"](computedDefault)) {
1144
- acc[key] = computedDefault;
1145
- }
1146
- } else if (computedDefault !== undefined) {
1147
- // Store computedDefault if it's a defined primitive (e.g. true)
1148
- acc[key] = computedDefault;
1172
+ {
1173
+ var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1174
+ // Compute the defaults for this node, with the parent defaults we might
1175
+ // have from a previous run: defaults[key].
1176
+ var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1177
+ maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1178
+ return acc;
1179
+ }, {});
1180
+ if (schema.additionalProperties && isObject(defaults)) {
1181
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1182
+ Object.keys(defaults).filter(function (key) {
1183
+ return !schema.properties || !schema.properties[key];
1184
+ }).forEach(function (key) {
1185
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1186
+ maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1187
+ });
1149
1188
  }
1150
- return acc;
1151
- }, {});
1189
+ return objectDefaults;
1190
+ }
1152
1191
  case "array":
1153
1192
  // Inject defaults into existing array defaults
1154
1193
  if (Array.isArray(defaults)) {
@@ -1190,8 +1229,8 @@
1190
1229
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1191
1230
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1192
1231
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1193
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1194
- * object properties.
1232
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1233
+ * false when computing defaults for any nested object properties.
1195
1234
  * @returns - The resulting `formData` with all the defaults provided
1196
1235
  */
1197
1236
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
@@ -1813,6 +1852,36 @@
1813
1852
  };
1814
1853
  }
1815
1854
 
1855
+ /** Potentially substitutes all replaceable parameters with the associated value(s) from the `params` if available. When
1856
+ * a `params` array is provided, each value in the array is used to replace any of the replaceable parameters in the
1857
+ * `inputString` using the `%1`, `%2`, etc. replacement specifiers.
1858
+ *
1859
+ * @param inputString - The string which will be potentially updated with replacement parameters
1860
+ * @param params - The optional list of replaceable parameter values to substitute into the english string
1861
+ * @returns - The updated string with any replacement specifiers replaced
1862
+ */
1863
+ function replaceStringParameters(inputString, params) {
1864
+ var output = inputString;
1865
+ if (Array.isArray(params)) {
1866
+ params.forEach(function (param, index) {
1867
+ output = output.replace("%" + (index + 1), param);
1868
+ });
1869
+ }
1870
+ return output;
1871
+ }
1872
+
1873
+ /** Translates a `TranslatableString` value `stringToTranslate` into english. When a `params` array is provided, each
1874
+ * value in the array is used to replace any of the replaceable parameters in the `stringToTranslate` using the `%1`,
1875
+ * `%2`, etc. replacement specifiers.
1876
+ *
1877
+ * @param stringToTranslate - The `TranslatableString` value to convert to english
1878
+ * @param params - The optional list of replaceable parameter values to substitute into the english string
1879
+ * @returns - The `stringToTranslate` itself with any replaceable parameter values substituted
1880
+ */
1881
+ function englishStringTranslator(stringToTranslate, params) {
1882
+ return replaceStringParameters(stringToTranslate, params);
1883
+ }
1884
+
1816
1885
  /** Returns the value(s) from `allEnumOptions` at the index(es) provided by `valueIndex`. If `valueIndex` is not an
1817
1886
  * array AND the index is not valid for `allEnumOptions`, `emptyValue` is returned. If `valueIndex` is an array, AND it
1818
1887
  * contains an invalid index, the returned array will have the resulting undefined values filtered out, leaving only
@@ -2632,6 +2701,76 @@
2632
2701
  return yyyy + "-" + MM + "-" + dd + "T" + hh + ":" + mm + ":" + ss + "." + SSS;
2633
2702
  }
2634
2703
 
2704
+ /** An enumeration of all the translatable strings used by `@rjsf/core` and its themes. The value of each of the
2705
+ * enumeration keys is expected to be the actual english string. Some strings contain replaceable parameter values
2706
+ * as indicated by `%1`, `%2`, etc. The number after the `%` indicates the order of the parameter. The ordering of
2707
+ * parameters is important because some languages may choose to put the second parameter before the first in its
2708
+ * translation. Also, some strings are rendered using `markdown-to-jsx` and thus support markdown and inline html.
2709
+ */
2710
+ exports.TranslatableString = void 0;
2711
+ (function (TranslatableString) {
2712
+ /** Fallback title of an array item, used by ArrayField */
2713
+ TranslatableString["ArrayItemTitle"] = "Item";
2714
+ /** Missing items reason, used by ArrayField */
2715
+ TranslatableString["MissingItems"] = "Missing items definition";
2716
+ /** Yes label, used by BooleanField */
2717
+ TranslatableString["YesLabel"] = "Yes";
2718
+ /** No label, used by BooleanField */
2719
+ TranslatableString["NoLabel"] = "No";
2720
+ /** Close label, used by ErrorList */
2721
+ TranslatableString["CloseLabel"] = "Close";
2722
+ /** Errors label, used by ErrorList */
2723
+ TranslatableString["ErrorsLabel"] = "Errors";
2724
+ /** New additionalProperties string default value, used by ObjectField */
2725
+ TranslatableString["NewStringDefault"] = "New Value";
2726
+ /** Add button title, used by AddButton */
2727
+ TranslatableString["AddButton"] = "Add";
2728
+ /** Add button title, used by AddButton */
2729
+ TranslatableString["AddItemButton"] = "Add Item";
2730
+ /** Move down button title, used by IconButton */
2731
+ TranslatableString["MoveDownButton"] = "Move down";
2732
+ /** Move up button title, used by IconButton */
2733
+ TranslatableString["MoveUpButton"] = "Move up";
2734
+ /** Remove button title, used by IconButton */
2735
+ TranslatableString["RemoveButton"] = "Remove";
2736
+ /** Now label, used by AltDateWidget */
2737
+ TranslatableString["NowLabel"] = "Now";
2738
+ /** Clear label, used by AltDateWidget */
2739
+ TranslatableString["ClearLabel"] = "Clear";
2740
+ /** Aria date label, used by DateWidget */
2741
+ TranslatableString["AriaDateLabel"] = "Select a date";
2742
+ /** Decrement button aria label, used by UpDownWidget */
2743
+ TranslatableString["DecrementAriaLabel"] = "Decrease value by 1";
2744
+ /** Increment button aria label, used by UpDownWidget */
2745
+ TranslatableString["IncrementAriaLabel"] = "Increase value by 1";
2746
+ // Strings with replaceable parameters
2747
+ /** Unknown field type reason, where %1 will be replaced with the type as provided by SchemaField */
2748
+ TranslatableString["UnknownFieldType"] = "Unknown field type %1";
2749
+ /** Option prefix, where %1 will be replaced with the option index as provided by MultiSchemaField */
2750
+ TranslatableString["OptionPrefix"] = "Option %1";
2751
+ /** Option prefix, where %1 and %2 will be replaced by the schema title and option index, respectively as provided by
2752
+ * MultiSchemaField
2753
+ */
2754
+ TranslatableString["TitleOptionPrefix"] = "%1 option %2";
2755
+ /** Key label, where %1 will be replaced by the label as provided by WrapIfAdditionalTemplate */
2756
+ TranslatableString["KeyLabel"] = "%1 Key";
2757
+ // Strings with replaceable parameters AND/OR that support markdown and html
2758
+ /** Unsupported field schema, used by UnsupportedField */
2759
+ TranslatableString["UnsupportedField"] = "Unsupported field schema.";
2760
+ /** Unsupported field schema, where %1 will be replaced by the idSchema.$id as provided by UnsupportedField */
2761
+ TranslatableString["UnsupportedFieldWithId"] = "Unsupported field schema for field <code>%1</code>.";
2762
+ /** Unsupported field schema, where %1 will be replaced by the reason string as provided by UnsupportedField */
2763
+ TranslatableString["UnsupportedFieldWithReason"] = "Unsupported field schema: <em>%1</em>.";
2764
+ /** Unsupported field schema, where %1 and %2 will be replaced by the idSchema.$id and reason strings, respectively,
2765
+ * as provided by UnsupportedField
2766
+ */
2767
+ TranslatableString["UnsupportedFieldWithIdAndReason"] = "Unsupported field schema for field <code>%1</code>: <em>%2</em>.";
2768
+ /** File name, type and size info, where %1, %2 and %3 will be replaced by the file name, file type and file size as
2769
+ * provided by FileWidget
2770
+ */
2771
+ TranslatableString["FilesInfo"] = "<strong>%1</strong> (%2, %3 bytes)";
2772
+ })(exports.TranslatableString || (exports.TranslatableString = {}));
2773
+
2635
2774
  exports.ADDITIONAL_PROPERTIES_KEY = ADDITIONAL_PROPERTIES_KEY;
2636
2775
  exports.ADDITIONAL_PROPERTY_FLAG = ADDITIONAL_PROPERTY_FLAG;
2637
2776
  exports.ALL_OF_KEY = ALL_OF_KEY;
@@ -2663,6 +2802,7 @@
2663
2802
  exports.dataURItoBlob = dataURItoBlob;
2664
2803
  exports.deepEquals = deepEquals;
2665
2804
  exports.descriptionId = descriptionId;
2805
+ exports.englishStringTranslator = englishStringTranslator;
2666
2806
  exports.enumOptionsDeselectValue = enumOptionsDeselectValue;
2667
2807
  exports.enumOptionsIndexForValue = enumOptionsIndexForValue;
2668
2808
  exports.enumOptionsIsSelected = enumOptionsIsSelected;
@@ -2703,6 +2843,7 @@
2703
2843
  exports.pad = pad;
2704
2844
  exports.parseDateString = parseDateString;
2705
2845
  exports.rangeSpec = rangeSpec;
2846
+ exports.replaceStringParameters = replaceStringParameters;
2706
2847
  exports.retrieveSchema = retrieveSchema;
2707
2848
  exports.sanitizeDataForNewSchema = sanitizeDataForNewSchema;
2708
2849
  exports.schemaRequiresTrueValue = schemaRequiresTrueValue;