@rjsf/utils 6.4.2 → 6.5.1

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.
Files changed (40) hide show
  1. package/dist/index.cjs +198 -84
  2. package/dist/index.cjs.map +4 -4
  3. package/dist/utils.esm.js +198 -84
  4. package/dist/utils.esm.js.map +4 -4
  5. package/dist/utils.umd.js +185 -80
  6. package/lib/enumOptionSelectedValue.d.ts +15 -0
  7. package/lib/enumOptionSelectedValue.js +28 -0
  8. package/lib/enumOptionSelectedValue.js.map +1 -0
  9. package/lib/enumOptionValueDecoder.d.ts +17 -0
  10. package/lib/enumOptionValueDecoder.js +53 -0
  11. package/lib/enumOptionValueDecoder.js.map +1 -0
  12. package/lib/enumOptionValueEncoder.d.ts +15 -0
  13. package/lib/enumOptionValueEncoder.js +27 -0
  14. package/lib/enumOptionValueEncoder.js.map +1 -0
  15. package/lib/getInputProps.js +9 -0
  16. package/lib/getInputProps.js.map +1 -1
  17. package/lib/getOptionValueFormat.d.ts +16 -0
  18. package/lib/getOptionValueFormat.js +17 -0
  19. package/lib/getOptionValueFormat.js.map +1 -0
  20. package/lib/index.d.ts +7 -2
  21. package/lib/index.js +7 -2
  22. package/lib/index.js.map +1 -1
  23. package/lib/removeOptionalEmptyObjects.d.ts +17 -0
  24. package/lib/removeOptionalEmptyObjects.js +108 -0
  25. package/lib/removeOptionalEmptyObjects.js.map +1 -0
  26. package/lib/resolveUiSchema.d.ts +5 -19
  27. package/lib/resolveUiSchema.js +49 -95
  28. package/lib/resolveUiSchema.js.map +1 -1
  29. package/lib/tsconfig.tsbuildinfo +1 -1
  30. package/lib/types.d.ts +27 -1
  31. package/package.json +4 -4
  32. package/src/enumOptionSelectedValue.ts +39 -0
  33. package/src/enumOptionValueDecoder.ts +64 -0
  34. package/src/enumOptionValueEncoder.ts +33 -0
  35. package/src/getInputProps.ts +10 -0
  36. package/src/getOptionValueFormat.ts +17 -0
  37. package/src/index.ts +11 -2
  38. package/src/removeOptionalEmptyObjects.ts +127 -0
  39. package/src/resolveUiSchema.ts +55 -122
  40. package/src/types.ts +28 -1
package/dist/utils.esm.js CHANGED
@@ -2664,6 +2664,36 @@ function englishStringTranslator(stringToTranslate, params) {
2664
2664
  return replaceStringParameters(stringToTranslate, params);
2665
2665
  }
2666
2666
 
2667
+ // src/enumOptionsIsSelected.ts
2668
+ function enumOptionsIsSelected(value, selected) {
2669
+ if (Array.isArray(selected)) {
2670
+ return selected.some((sel) => deepEquals(sel, value));
2671
+ }
2672
+ return deepEquals(selected, value);
2673
+ }
2674
+
2675
+ // src/enumOptionsIndexForValue.ts
2676
+ function enumOptionsIndexForValue(value, allEnumOptions = [], multiple = false) {
2677
+ const selectedIndexes = allEnumOptions.map((opt, index) => enumOptionsIsSelected(opt.value, value) ? String(index) : void 0).filter((opt) => typeof opt !== "undefined");
2678
+ if (!multiple) {
2679
+ return selectedIndexes[0];
2680
+ }
2681
+ return selectedIndexes;
2682
+ }
2683
+
2684
+ // src/enumOptionSelectedValue.ts
2685
+ function enumOptionSelectedValue(value, enumOptions, multiple, format = "indexed", emptyValue) {
2686
+ const isEmpty9 = typeof value === "undefined" || multiple && Array.isArray(value) && value.length < 1 || !multiple && value === emptyValue;
2687
+ if (isEmpty9) {
2688
+ return emptyValue;
2689
+ }
2690
+ if (format === "realValue") {
2691
+ return multiple ? value.map(String) : String(value);
2692
+ }
2693
+ const indexes = enumOptionsIndexForValue(value, enumOptions, multiple);
2694
+ return typeof indexes === "undefined" ? emptyValue : indexes;
2695
+ }
2696
+
2667
2697
  // src/enumOptionsValueForIndex.ts
2668
2698
  function enumOptionsValueForIndex(valueIndex, allEnumOptions = [], emptyValue) {
2669
2699
  if (Array.isArray(valueIndex)) {
@@ -2674,37 +2704,60 @@ function enumOptionsValueForIndex(valueIndex, allEnumOptions = [], emptyValue) {
2674
2704
  return option ? option.value : emptyValue;
2675
2705
  }
2676
2706
 
2677
- // src/enumOptionsDeselectValue.ts
2678
- function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions = []) {
2679
- const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2680
- if (Array.isArray(selected)) {
2681
- return selected.filter((v) => !deepEquals(v, value));
2707
+ // src/enumOptionValueDecoder.ts
2708
+ function decodeSingle(value, enumOptions, emptyValue) {
2709
+ if (value === "" || !Array.isArray(enumOptions)) {
2710
+ return emptyValue;
2682
2711
  }
2683
- return deepEquals(value, selected) ? void 0 : selected;
2712
+ const match = enumOptions.find((opt) => String(opt.value) === value);
2713
+ if (match) {
2714
+ return match.value;
2715
+ }
2716
+ const index = Number(value);
2717
+ if (!isNaN(index) && index >= 0 && index < enumOptions.length) {
2718
+ return enumOptions[index].value;
2719
+ }
2720
+ return emptyValue;
2721
+ }
2722
+ function enumOptionValueDecoder(value, enumOptions, format = "indexed", emptyValue) {
2723
+ if (format !== "realValue") {
2724
+ return enumOptionsValueForIndex(value, enumOptions, emptyValue);
2725
+ }
2726
+ if (Array.isArray(value)) {
2727
+ return value.map((v) => decodeSingle(v, enumOptions, emptyValue));
2728
+ }
2729
+ return decodeSingle(value, enumOptions, emptyValue);
2684
2730
  }
2685
2731
 
2686
- // src/enumOptionsIsSelected.ts
2687
- function enumOptionsIsSelected(value, selected) {
2688
- if (Array.isArray(selected)) {
2689
- return selected.some((sel) => deepEquals(sel, value));
2732
+ // src/enumOptionValueEncoder.ts
2733
+ import isNil2 from "lodash/isNil";
2734
+ function enumOptionValueEncoder(value, index, format = "indexed") {
2735
+ if (format !== "realValue") {
2736
+ return String(index);
2690
2737
  }
2691
- return deepEquals(selected, value);
2738
+ if (isNil2(value)) {
2739
+ return "";
2740
+ }
2741
+ if (typeof value === "object") {
2742
+ return String(index);
2743
+ }
2744
+ return String(value);
2692
2745
  }
2693
2746
 
2694
- // src/enumOptionsIndexForValue.ts
2695
- function enumOptionsIndexForValue(value, allEnumOptions = [], multiple = false) {
2696
- const selectedIndexes = allEnumOptions.map((opt, index) => enumOptionsIsSelected(opt.value, value) ? String(index) : void 0).filter((opt) => typeof opt !== "undefined");
2697
- if (!multiple) {
2698
- return selectedIndexes[0];
2747
+ // src/enumOptionsDeselectValue.ts
2748
+ function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions = []) {
2749
+ const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2750
+ if (Array.isArray(selected)) {
2751
+ return selected.filter((v) => !deepEquals(v, value));
2699
2752
  }
2700
- return selectedIndexes;
2753
+ return deepEquals(value, selected) ? void 0 : selected;
2701
2754
  }
2702
2755
 
2703
2756
  // src/enumOptionsSelectValue.ts
2704
- import isNil2 from "lodash/isNil";
2757
+ import isNil3 from "lodash/isNil";
2705
2758
  function enumOptionsSelectValue(valueIndex, selected, allEnumOptions = []) {
2706
2759
  const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2707
- if (!isNil2(value)) {
2760
+ if (!isNil3(value)) {
2708
2761
  const index = allEnumOptions.findIndex((opt) => value === opt.value);
2709
2762
  const all = allEnumOptions.map(({ value: val }) => val);
2710
2763
  const updated = selected.slice(0, index).concat(value, selected.slice(index));
@@ -2897,6 +2950,14 @@ function getInputProps(schema, defaultType, options = {}, autoDefaultStepAny = t
2897
2950
  }
2898
2951
  }
2899
2952
  }
2953
+ if (["date", "datetime-local", "time", "week", "month"].includes(inputProps.type)) {
2954
+ if (schema.formatMinimum !== void 0) {
2955
+ inputProps.min = schema.formatMinimum;
2956
+ }
2957
+ if (schema.formatMaximum !== void 0) {
2958
+ inputProps.max = schema.formatMaximum;
2959
+ }
2960
+ }
2900
2961
  if (options.autocomplete) {
2901
2962
  inputProps.autoComplete = options.autocomplete;
2902
2963
  }
@@ -2906,6 +2967,11 @@ function getInputProps(schema, defaultType, options = {}, autoDefaultStepAny = t
2906
2967
  return inputProps;
2907
2968
  }
2908
2969
 
2970
+ // src/getOptionValueFormat.ts
2971
+ function getOptionValueFormat(options) {
2972
+ return options?.optionValueFormat ?? "indexed";
2973
+ }
2974
+
2909
2975
  // src/getSubmitButtonOptions.ts
2910
2976
  var DEFAULT_OPTIONS = {
2911
2977
  props: {
@@ -3126,11 +3192,11 @@ function optionalControlsId(id, element) {
3126
3192
  }
3127
3193
 
3128
3194
  // src/isFormDataAvailable.ts
3129
- import isNil3 from "lodash/isNil";
3195
+ import isNil4 from "lodash/isNil";
3130
3196
  import isEmpty6 from "lodash/isEmpty";
3131
3197
  import isObject5 from "lodash/isObject";
3132
3198
  function isFormDataAvailable(formData) {
3133
- return !isNil3(formData) && (!isObject5(formData) || Array.isArray(formData) || !isEmpty6(formData));
3199
+ return !isNil4(formData) && (!isObject5(formData) || Array.isArray(formData) || !isEmpty6(formData));
3134
3200
  }
3135
3201
 
3136
3202
  // src/isRootSchema.ts
@@ -3169,6 +3235,81 @@ function lookupFromFormContext(regOrFc, toLookup, fallback) {
3169
3235
  return get22(regOrFc, [...lookupPath, toLookup], fallback);
3170
3236
  }
3171
3237
 
3238
+ // src/removeOptionalEmptyObjects.ts
3239
+ import isNil5 from "lodash/isNil";
3240
+ function isValueEmpty(value) {
3241
+ if (isNil5(value) || value === "") {
3242
+ return true;
3243
+ }
3244
+ if (Array.isArray(value)) {
3245
+ return value.length === 0;
3246
+ }
3247
+ if (isObject(value)) {
3248
+ const obj = value;
3249
+ const keys2 = Object.keys(obj);
3250
+ return keys2.every((key) => isValueEmpty(obj[key]));
3251
+ }
3252
+ return false;
3253
+ }
3254
+ function removeOptionalEmptyObjects(validator, schema, rootSchema, formData) {
3255
+ if (!isObject(schema)) {
3256
+ return formData;
3257
+ }
3258
+ const resolvedSchema = retrieveSchema(validator, schema, rootSchema, formData);
3259
+ if (Array.isArray(formData)) {
3260
+ const itemsSchema = resolvedSchema.items;
3261
+ if (!itemsSchema) {
3262
+ return formData;
3263
+ }
3264
+ let hasChanges = false;
3265
+ const mapped = formData.map((item, index) => {
3266
+ let itemSchema = itemsSchema;
3267
+ if (Array.isArray(itemsSchema)) {
3268
+ itemSchema = itemsSchema[index] || resolvedSchema.additionalItems || {};
3269
+ }
3270
+ const cleaned = removeOptionalEmptyObjects(validator, itemSchema, rootSchema, item);
3271
+ if (cleaned !== item) {
3272
+ hasChanges = true;
3273
+ }
3274
+ return cleaned === void 0 ? {} : cleaned;
3275
+ });
3276
+ return hasChanges ? mapped : formData;
3277
+ }
3278
+ const { properties, required: requiredFields = [] } = resolvedSchema;
3279
+ if (!isObject(formData) || !properties) {
3280
+ return formData;
3281
+ }
3282
+ const result = {};
3283
+ const data = formData;
3284
+ let hasAnyValue = false;
3285
+ for (const key of Object.keys(data)) {
3286
+ const value = data[key];
3287
+ const propertySchema = properties[key] || {};
3288
+ const isRequired = requiredFields.includes(key);
3289
+ const isObj = isObject(value);
3290
+ const isArr = Array.isArray(value);
3291
+ if ((isObj || isArr) && properties[key]) {
3292
+ const cleaned = removeOptionalEmptyObjects(validator, propertySchema, rootSchema, value);
3293
+ if (!isRequired && isValueEmpty(cleaned)) {
3294
+ continue;
3295
+ }
3296
+ result[key] = cleaned;
3297
+ hasAnyValue = true;
3298
+ } else if (!isRequired && isValueEmpty(value) && properties[key]) {
3299
+ continue;
3300
+ } else {
3301
+ result[key] = value;
3302
+ if (!isValueEmpty(value)) {
3303
+ hasAnyValue = true;
3304
+ }
3305
+ }
3306
+ }
3307
+ if (!hasAnyValue && Object.keys(result).length === 0) {
3308
+ return void 0;
3309
+ }
3310
+ return result;
3311
+ }
3312
+
3172
3313
  // src/orderProperties.ts
3173
3314
  function orderProperties(properties, order) {
3174
3315
  if (!Array.isArray(order)) {
@@ -3226,63 +3367,43 @@ function parseDateString(dateString, includeTime = true) {
3226
3367
  }
3227
3368
 
3228
3369
  // src/resolveUiSchema.ts
3229
- var SAME_KEY_KEYWORDS = [ITEMS_KEY, ADDITIONAL_PROPERTIES_KEY];
3230
- var ARRAY_KEYWORDS = [ONE_OF_KEY, ANY_OF_KEY, ALL_OF_KEY];
3231
- function expandUiSchemaDefinitions(currentSchema, uiSchema, registry, visited = /* @__PURE__ */ new Set()) {
3232
- const { rootSchema, uiSchemaDefinitions: definitions } = registry;
3233
- let result = { ...uiSchema };
3234
- let resolvedSchema = currentSchema;
3235
- const ref = currentSchema[REF_KEY];
3236
- const isRecursive = ref && visited.has(ref);
3237
- if (ref) {
3238
- visited.add(ref);
3239
- if (definitions && ref in definitions) {
3240
- result = mergeObjects(definitions[ref], result);
3241
- }
3242
- if (isRecursive) {
3243
- return result;
3244
- }
3245
- try {
3246
- resolvedSchema = findSchemaDefinition(ref, rootSchema);
3247
- } catch {
3248
- resolvedSchema = currentSchema;
3249
- }
3370
+ import isEmpty7 from "lodash/isEmpty";
3371
+ function resolveUiSchema(schema, localUiSchema, registry) {
3372
+ const ref = schema[RJSF_REF_KEY] ?? schema[REF_KEY];
3373
+ const definitions = registry.uiSchemaDefinitions;
3374
+ const definitionUiSchema = ref && definitions ? definitions[ref] : void 0;
3375
+ let result;
3376
+ if (!definitionUiSchema) {
3377
+ result = localUiSchema || {};
3378
+ } else if (!localUiSchema || isEmpty7(localUiSchema)) {
3379
+ result = { ...definitionUiSchema };
3380
+ } else {
3381
+ result = mergeObjects(definitionUiSchema, localUiSchema);
3250
3382
  }
3251
- const properties = resolvedSchema[PROPERTIES_KEY];
3252
- if (properties && isObject(properties)) {
3253
- for (const [propName, propSchema] of Object.entries(properties)) {
3254
- const propUiSchema = result[propName] || {};
3255
- const expanded = expandUiSchemaDefinitions(propSchema, propUiSchema, registry, new Set(visited));
3256
- if (Object.keys(expanded).length > 0) {
3257
- result[propName] = expanded;
3383
+ if (definitions) {
3384
+ let resolvedSchema = schema;
3385
+ if (ref && schema[REF_KEY] && !schema[RJSF_REF_KEY]) {
3386
+ try {
3387
+ resolvedSchema = findSchemaDefinition(ref, registry.rootSchema);
3388
+ } catch (e) {
3389
+ console.warn("could not resolve $ref in resolveUiSchema:\n", e);
3390
+ return result;
3258
3391
  }
3259
3392
  }
3260
- }
3261
- for (const keyword of SAME_KEY_KEYWORDS) {
3262
- const subSchema = resolvedSchema[keyword];
3263
- if (subSchema && isObject(subSchema) && !Array.isArray(subSchema)) {
3264
- const currentUiSchema = result[keyword];
3265
- if (typeof currentUiSchema !== "function") {
3266
- const subUiSchema = currentUiSchema || {};
3267
- const expanded = expandUiSchemaDefinitions(subSchema, subUiSchema, registry, new Set(visited));
3268
- if (Object.keys(expanded).length > 0) {
3269
- result[keyword] = expanded;
3270
- }
3393
+ for (const keyword of [ONE_OF_KEY, ANY_OF_KEY]) {
3394
+ const schemaOptions = resolvedSchema[keyword];
3395
+ if (!Array.isArray(schemaOptions) || schemaOptions.length === 0) {
3396
+ continue;
3271
3397
  }
3272
- }
3273
- }
3274
- for (const keyword of ARRAY_KEYWORDS) {
3275
- const schemaOptions = resolvedSchema[keyword];
3276
- if (Array.isArray(schemaOptions) && schemaOptions.length > 0) {
3277
3398
  const currentUiSchemaArray = result[keyword];
3278
3399
  const uiSchemaArray = Array.isArray(currentUiSchemaArray) ? [...currentUiSchemaArray] : [];
3279
3400
  let hasExpanded = false;
3280
3401
  for (let i = 0; i < schemaOptions.length; i++) {
3281
- const optionSchema = schemaOptions[i];
3282
- const optionUiSchema = uiSchemaArray[i] || {};
3283
- const expanded = expandUiSchemaDefinitions(optionSchema, optionUiSchema, registry, new Set(visited));
3284
- if (Object.keys(expanded).length > 0) {
3285
- uiSchemaArray[i] = expanded;
3402
+ const option = schemaOptions[i];
3403
+ const optionRef = option?.[RJSF_REF_KEY] ?? option?.[REF_KEY];
3404
+ if (optionRef && optionRef in definitions) {
3405
+ const optionUiSchema = uiSchemaArray[i] || {};
3406
+ uiSchemaArray[i] = mergeObjects(definitions[optionRef], optionUiSchema);
3286
3407
  hasExpanded = true;
3287
3408
  }
3288
3409
  }
@@ -3293,17 +3414,6 @@ function expandUiSchemaDefinitions(currentSchema, uiSchema, registry, visited =
3293
3414
  }
3294
3415
  return result;
3295
3416
  }
3296
- function resolveUiSchema(schema, localUiSchema, registry) {
3297
- const ref = schema[RJSF_REF_KEY] ?? schema[REF_KEY];
3298
- const definitionUiSchema = ref ? registry.uiSchemaDefinitions?.[ref] : void 0;
3299
- if (!definitionUiSchema) {
3300
- return localUiSchema || {};
3301
- }
3302
- if (!localUiSchema || Object.keys(localUiSchema).length === 0) {
3303
- return { ...definitionUiSchema };
3304
- }
3305
- return mergeObjects(definitionUiSchema, localUiSchema);
3306
- }
3307
3417
 
3308
3418
  // src/schemaRequiresTrueValue.ts
3309
3419
  function schemaRequiresTrueValue(schema) {
@@ -3670,7 +3780,7 @@ function utcToLocal(jsonDate) {
3670
3780
  }
3671
3781
 
3672
3782
  // src/validationDataMerge.ts
3673
- import isEmpty7 from "lodash/isEmpty";
3783
+ import isEmpty8 from "lodash/isEmpty";
3674
3784
  function validationDataMerge(validationData, additionalErrorSchema, preventDuplicates = false) {
3675
3785
  if (!additionalErrorSchema) {
3676
3786
  return validationData;
@@ -3678,7 +3788,7 @@ function validationDataMerge(validationData, additionalErrorSchema, preventDupli
3678
3788
  const { errors: oldErrors, errorSchema: oldErrorSchema } = validationData;
3679
3789
  let errors = toErrorList(additionalErrorSchema);
3680
3790
  let errorSchema = additionalErrorSchema;
3681
- if (!isEmpty7(oldErrorSchema)) {
3791
+ if (!isEmpty8(oldErrorSchema)) {
3682
3792
  errorSchema = mergeObjects(
3683
3793
  oldErrorSchema,
3684
3794
  additionalErrorSchema,
@@ -3956,6 +4066,9 @@ export {
3956
4066
  descriptionId,
3957
4067
  dotNotationNameGenerator,
3958
4068
  englishStringTranslator,
4069
+ enumOptionSelectedValue,
4070
+ enumOptionValueDecoder,
4071
+ enumOptionValueEncoder,
3959
4072
  enumOptionsDeselectValue,
3960
4073
  enumOptionsIndexForValue,
3961
4074
  enumOptionsIsSelected,
@@ -3963,7 +4076,6 @@ export {
3963
4076
  enumOptionsValueForIndex,
3964
4077
  errorId,
3965
4078
  examplesId,
3966
- expandUiSchemaDefinitions,
3967
4079
  findFieldInSchema,
3968
4080
  findSchemaDefinition,
3969
4081
  findSelectedOptionInXxxOf,
@@ -3978,6 +4090,7 @@ export {
3978
4090
  getFromSchema,
3979
4091
  getInputProps,
3980
4092
  getOptionMatchingSimpleDiscriminator,
4093
+ getOptionValueFormat,
3981
4094
  getSchemaType,
3982
4095
  getSubmitButtonOptions,
3983
4096
  getTemplate,
@@ -4014,6 +4127,7 @@ export {
4014
4127
  pad,
4015
4128
  parseDateString,
4016
4129
  rangeSpec,
4130
+ removeOptionalEmptyObjects,
4017
4131
  replaceStringParameters,
4018
4132
  resolveUiSchema,
4019
4133
  retrieveSchema,