@rjsf/utils 6.4.2 → 6.5.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.
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/index.cjs CHANGED
@@ -85,6 +85,9 @@ __export(index_exports, {
85
85
  descriptionId: () => descriptionId,
86
86
  dotNotationNameGenerator: () => dotNotationNameGenerator,
87
87
  englishStringTranslator: () => englishStringTranslator,
88
+ enumOptionSelectedValue: () => enumOptionSelectedValue,
89
+ enumOptionValueDecoder: () => enumOptionValueDecoder,
90
+ enumOptionValueEncoder: () => enumOptionValueEncoder,
88
91
  enumOptionsDeselectValue: () => enumOptionsDeselectValue,
89
92
  enumOptionsIndexForValue: () => enumOptionsIndexForValue,
90
93
  enumOptionsIsSelected: () => enumOptionsIsSelected,
@@ -92,7 +95,6 @@ __export(index_exports, {
92
95
  enumOptionsValueForIndex: () => enumOptionsValueForIndex,
93
96
  errorId: () => errorId,
94
97
  examplesId: () => examplesId,
95
- expandUiSchemaDefinitions: () => expandUiSchemaDefinitions,
96
98
  findFieldInSchema: () => findFieldInSchema,
97
99
  findSchemaDefinition: () => findSchemaDefinition,
98
100
  findSelectedOptionInXxxOf: () => findSelectedOptionInXxxOf,
@@ -107,6 +109,7 @@ __export(index_exports, {
107
109
  getFromSchema: () => getFromSchema,
108
110
  getInputProps: () => getInputProps,
109
111
  getOptionMatchingSimpleDiscriminator: () => getOptionMatchingSimpleDiscriminator,
112
+ getOptionValueFormat: () => getOptionValueFormat,
110
113
  getSchemaType: () => getSchemaType,
111
114
  getSubmitButtonOptions: () => getSubmitButtonOptions,
112
115
  getTemplate: () => getTemplate,
@@ -143,6 +146,7 @@ __export(index_exports, {
143
146
  pad: () => pad,
144
147
  parseDateString: () => parseDateString,
145
148
  rangeSpec: () => rangeSpec,
149
+ removeOptionalEmptyObjects: () => removeOptionalEmptyObjects,
146
150
  replaceStringParameters: () => replaceStringParameters,
147
151
  resolveUiSchema: () => resolveUiSchema,
148
152
  retrieveSchema: () => retrieveSchema,
@@ -2836,6 +2840,36 @@ function englishStringTranslator(stringToTranslate, params) {
2836
2840
  return replaceStringParameters(stringToTranslate, params);
2837
2841
  }
2838
2842
 
2843
+ // src/enumOptionsIsSelected.ts
2844
+ function enumOptionsIsSelected(value, selected) {
2845
+ if (Array.isArray(selected)) {
2846
+ return selected.some((sel) => deepEquals(sel, value));
2847
+ }
2848
+ return deepEquals(selected, value);
2849
+ }
2850
+
2851
+ // src/enumOptionsIndexForValue.ts
2852
+ function enumOptionsIndexForValue(value, allEnumOptions = [], multiple = false) {
2853
+ const selectedIndexes = allEnumOptions.map((opt, index) => enumOptionsIsSelected(opt.value, value) ? String(index) : void 0).filter((opt) => typeof opt !== "undefined");
2854
+ if (!multiple) {
2855
+ return selectedIndexes[0];
2856
+ }
2857
+ return selectedIndexes;
2858
+ }
2859
+
2860
+ // src/enumOptionSelectedValue.ts
2861
+ function enumOptionSelectedValue(value, enumOptions, multiple, format = "indexed", emptyValue) {
2862
+ const isEmpty9 = typeof value === "undefined" || multiple && Array.isArray(value) && value.length < 1 || !multiple && value === emptyValue;
2863
+ if (isEmpty9) {
2864
+ return emptyValue;
2865
+ }
2866
+ if (format === "realValue") {
2867
+ return multiple ? value.map(String) : String(value);
2868
+ }
2869
+ const indexes = enumOptionsIndexForValue(value, enumOptions, multiple);
2870
+ return typeof indexes === "undefined" ? emptyValue : indexes;
2871
+ }
2872
+
2839
2873
  // src/enumOptionsValueForIndex.ts
2840
2874
  function enumOptionsValueForIndex(valueIndex, allEnumOptions = [], emptyValue) {
2841
2875
  if (Array.isArray(valueIndex)) {
@@ -2846,37 +2880,60 @@ function enumOptionsValueForIndex(valueIndex, allEnumOptions = [], emptyValue) {
2846
2880
  return option ? option.value : emptyValue;
2847
2881
  }
2848
2882
 
2849
- // src/enumOptionsDeselectValue.ts
2850
- function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions = []) {
2851
- const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2852
- if (Array.isArray(selected)) {
2853
- return selected.filter((v) => !deepEquals(v, value));
2883
+ // src/enumOptionValueDecoder.ts
2884
+ function decodeSingle(value, enumOptions, emptyValue) {
2885
+ if (value === "" || !Array.isArray(enumOptions)) {
2886
+ return emptyValue;
2854
2887
  }
2855
- return deepEquals(value, selected) ? void 0 : selected;
2888
+ const match = enumOptions.find((opt) => String(opt.value) === value);
2889
+ if (match) {
2890
+ return match.value;
2891
+ }
2892
+ const index = Number(value);
2893
+ if (!isNaN(index) && index >= 0 && index < enumOptions.length) {
2894
+ return enumOptions[index].value;
2895
+ }
2896
+ return emptyValue;
2897
+ }
2898
+ function enumOptionValueDecoder(value, enumOptions, format = "indexed", emptyValue) {
2899
+ if (format !== "realValue") {
2900
+ return enumOptionsValueForIndex(value, enumOptions, emptyValue);
2901
+ }
2902
+ if (Array.isArray(value)) {
2903
+ return value.map((v) => decodeSingle(v, enumOptions, emptyValue));
2904
+ }
2905
+ return decodeSingle(value, enumOptions, emptyValue);
2856
2906
  }
2857
2907
 
2858
- // src/enumOptionsIsSelected.ts
2859
- function enumOptionsIsSelected(value, selected) {
2860
- if (Array.isArray(selected)) {
2861
- return selected.some((sel) => deepEquals(sel, value));
2908
+ // src/enumOptionValueEncoder.ts
2909
+ var import_isNil2 = __toESM(require("lodash/isNil"), 1);
2910
+ function enumOptionValueEncoder(value, index, format = "indexed") {
2911
+ if (format !== "realValue") {
2912
+ return String(index);
2862
2913
  }
2863
- return deepEquals(selected, value);
2914
+ if ((0, import_isNil2.default)(value)) {
2915
+ return "";
2916
+ }
2917
+ if (typeof value === "object") {
2918
+ return String(index);
2919
+ }
2920
+ return String(value);
2864
2921
  }
2865
2922
 
2866
- // src/enumOptionsIndexForValue.ts
2867
- function enumOptionsIndexForValue(value, allEnumOptions = [], multiple = false) {
2868
- const selectedIndexes = allEnumOptions.map((opt, index) => enumOptionsIsSelected(opt.value, value) ? String(index) : void 0).filter((opt) => typeof opt !== "undefined");
2869
- if (!multiple) {
2870
- return selectedIndexes[0];
2923
+ // src/enumOptionsDeselectValue.ts
2924
+ function enumOptionsDeselectValue(valueIndex, selected, allEnumOptions = []) {
2925
+ const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2926
+ if (Array.isArray(selected)) {
2927
+ return selected.filter((v) => !deepEquals(v, value));
2871
2928
  }
2872
- return selectedIndexes;
2929
+ return deepEquals(value, selected) ? void 0 : selected;
2873
2930
  }
2874
2931
 
2875
2932
  // src/enumOptionsSelectValue.ts
2876
- var import_isNil2 = __toESM(require("lodash/isNil"), 1);
2933
+ var import_isNil3 = __toESM(require("lodash/isNil"), 1);
2877
2934
  function enumOptionsSelectValue(valueIndex, selected, allEnumOptions = []) {
2878
2935
  const value = enumOptionsValueForIndex(valueIndex, allEnumOptions);
2879
- if (!(0, import_isNil2.default)(value)) {
2936
+ if (!(0, import_isNil3.default)(value)) {
2880
2937
  const index = allEnumOptions.findIndex((opt) => value === opt.value);
2881
2938
  const all = allEnumOptions.map(({ value: val }) => val);
2882
2939
  const updated = selected.slice(0, index).concat(value, selected.slice(index));
@@ -3069,6 +3126,14 @@ function getInputProps(schema, defaultType, options = {}, autoDefaultStepAny = t
3069
3126
  }
3070
3127
  }
3071
3128
  }
3129
+ if (["date", "datetime-local", "time", "week", "month"].includes(inputProps.type)) {
3130
+ if (schema.formatMinimum !== void 0) {
3131
+ inputProps.min = schema.formatMinimum;
3132
+ }
3133
+ if (schema.formatMaximum !== void 0) {
3134
+ inputProps.max = schema.formatMaximum;
3135
+ }
3136
+ }
3072
3137
  if (options.autocomplete) {
3073
3138
  inputProps.autoComplete = options.autocomplete;
3074
3139
  }
@@ -3078,6 +3143,11 @@ function getInputProps(schema, defaultType, options = {}, autoDefaultStepAny = t
3078
3143
  return inputProps;
3079
3144
  }
3080
3145
 
3146
+ // src/getOptionValueFormat.ts
3147
+ function getOptionValueFormat(options) {
3148
+ return options?.optionValueFormat ?? "indexed";
3149
+ }
3150
+
3081
3151
  // src/getSubmitButtonOptions.ts
3082
3152
  var DEFAULT_OPTIONS = {
3083
3153
  props: {
@@ -3298,11 +3368,11 @@ function optionalControlsId(id, element) {
3298
3368
  }
3299
3369
 
3300
3370
  // src/isFormDataAvailable.ts
3301
- var import_isNil3 = __toESM(require("lodash/isNil"), 1);
3371
+ var import_isNil4 = __toESM(require("lodash/isNil"), 1);
3302
3372
  var import_isEmpty6 = __toESM(require("lodash/isEmpty"), 1);
3303
3373
  var import_isObject12 = __toESM(require("lodash/isObject"), 1);
3304
3374
  function isFormDataAvailable(formData) {
3305
- return !(0, import_isNil3.default)(formData) && (!(0, import_isObject12.default)(formData) || Array.isArray(formData) || !(0, import_isEmpty6.default)(formData));
3375
+ return !(0, import_isNil4.default)(formData) && (!(0, import_isObject12.default)(formData) || Array.isArray(formData) || !(0, import_isEmpty6.default)(formData));
3306
3376
  }
3307
3377
 
3308
3378
  // src/isRootSchema.ts
@@ -3341,6 +3411,81 @@ function lookupFromFormContext(regOrFc, toLookup, fallback) {
3341
3411
  return (0, import_get22.default)(regOrFc, [...lookupPath, toLookup], fallback);
3342
3412
  }
3343
3413
 
3414
+ // src/removeOptionalEmptyObjects.ts
3415
+ var import_isNil5 = __toESM(require("lodash/isNil"), 1);
3416
+ function isValueEmpty(value) {
3417
+ if ((0, import_isNil5.default)(value) || value === "") {
3418
+ return true;
3419
+ }
3420
+ if (Array.isArray(value)) {
3421
+ return value.length === 0;
3422
+ }
3423
+ if (isObject(value)) {
3424
+ const obj = value;
3425
+ const keys2 = Object.keys(obj);
3426
+ return keys2.every((key) => isValueEmpty(obj[key]));
3427
+ }
3428
+ return false;
3429
+ }
3430
+ function removeOptionalEmptyObjects(validator, schema, rootSchema, formData) {
3431
+ if (!isObject(schema)) {
3432
+ return formData;
3433
+ }
3434
+ const resolvedSchema = retrieveSchema(validator, schema, rootSchema, formData);
3435
+ if (Array.isArray(formData)) {
3436
+ const itemsSchema = resolvedSchema.items;
3437
+ if (!itemsSchema) {
3438
+ return formData;
3439
+ }
3440
+ let hasChanges = false;
3441
+ const mapped = formData.map((item, index) => {
3442
+ let itemSchema = itemsSchema;
3443
+ if (Array.isArray(itemsSchema)) {
3444
+ itemSchema = itemsSchema[index] || resolvedSchema.additionalItems || {};
3445
+ }
3446
+ const cleaned = removeOptionalEmptyObjects(validator, itemSchema, rootSchema, item);
3447
+ if (cleaned !== item) {
3448
+ hasChanges = true;
3449
+ }
3450
+ return cleaned === void 0 ? {} : cleaned;
3451
+ });
3452
+ return hasChanges ? mapped : formData;
3453
+ }
3454
+ const { properties, required: requiredFields = [] } = resolvedSchema;
3455
+ if (!isObject(formData) || !properties) {
3456
+ return formData;
3457
+ }
3458
+ const result = {};
3459
+ const data = formData;
3460
+ let hasAnyValue = false;
3461
+ for (const key of Object.keys(data)) {
3462
+ const value = data[key];
3463
+ const propertySchema = properties[key] || {};
3464
+ const isRequired = requiredFields.includes(key);
3465
+ const isObj = isObject(value);
3466
+ const isArr = Array.isArray(value);
3467
+ if ((isObj || isArr) && properties[key]) {
3468
+ const cleaned = removeOptionalEmptyObjects(validator, propertySchema, rootSchema, value);
3469
+ if (!isRequired && isValueEmpty(cleaned)) {
3470
+ continue;
3471
+ }
3472
+ result[key] = cleaned;
3473
+ hasAnyValue = true;
3474
+ } else if (!isRequired && isValueEmpty(value) && properties[key]) {
3475
+ continue;
3476
+ } else {
3477
+ result[key] = value;
3478
+ if (!isValueEmpty(value)) {
3479
+ hasAnyValue = true;
3480
+ }
3481
+ }
3482
+ }
3483
+ if (!hasAnyValue && Object.keys(result).length === 0) {
3484
+ return void 0;
3485
+ }
3486
+ return result;
3487
+ }
3488
+
3344
3489
  // src/orderProperties.ts
3345
3490
  function orderProperties(properties, order) {
3346
3491
  if (!Array.isArray(order)) {
@@ -3398,63 +3543,43 @@ function parseDateString(dateString, includeTime = true) {
3398
3543
  }
3399
3544
 
3400
3545
  // src/resolveUiSchema.ts
3401
- var SAME_KEY_KEYWORDS = [ITEMS_KEY, ADDITIONAL_PROPERTIES_KEY];
3402
- var ARRAY_KEYWORDS = [ONE_OF_KEY, ANY_OF_KEY, ALL_OF_KEY];
3403
- function expandUiSchemaDefinitions(currentSchema, uiSchema, registry, visited = /* @__PURE__ */ new Set()) {
3404
- const { rootSchema, uiSchemaDefinitions: definitions } = registry;
3405
- let result = { ...uiSchema };
3406
- let resolvedSchema = currentSchema;
3407
- const ref = currentSchema[REF_KEY];
3408
- const isRecursive = ref && visited.has(ref);
3409
- if (ref) {
3410
- visited.add(ref);
3411
- if (definitions && ref in definitions) {
3412
- result = mergeObjects(definitions[ref], result);
3413
- }
3414
- if (isRecursive) {
3415
- return result;
3416
- }
3417
- try {
3418
- resolvedSchema = findSchemaDefinition(ref, rootSchema);
3419
- } catch {
3420
- resolvedSchema = currentSchema;
3421
- }
3546
+ var import_isEmpty7 = __toESM(require("lodash/isEmpty"), 1);
3547
+ function resolveUiSchema(schema, localUiSchema, registry) {
3548
+ const ref = schema[RJSF_REF_KEY] ?? schema[REF_KEY];
3549
+ const definitions = registry.uiSchemaDefinitions;
3550
+ const definitionUiSchema = ref && definitions ? definitions[ref] : void 0;
3551
+ let result;
3552
+ if (!definitionUiSchema) {
3553
+ result = localUiSchema || {};
3554
+ } else if (!localUiSchema || (0, import_isEmpty7.default)(localUiSchema)) {
3555
+ result = { ...definitionUiSchema };
3556
+ } else {
3557
+ result = mergeObjects(definitionUiSchema, localUiSchema);
3422
3558
  }
3423
- const properties = resolvedSchema[PROPERTIES_KEY];
3424
- if (properties && isObject(properties)) {
3425
- for (const [propName, propSchema] of Object.entries(properties)) {
3426
- const propUiSchema = result[propName] || {};
3427
- const expanded = expandUiSchemaDefinitions(propSchema, propUiSchema, registry, new Set(visited));
3428
- if (Object.keys(expanded).length > 0) {
3429
- result[propName] = expanded;
3559
+ if (definitions) {
3560
+ let resolvedSchema = schema;
3561
+ if (ref && schema[REF_KEY] && !schema[RJSF_REF_KEY]) {
3562
+ try {
3563
+ resolvedSchema = findSchemaDefinition(ref, registry.rootSchema);
3564
+ } catch (e) {
3565
+ console.warn("could not resolve $ref in resolveUiSchema:\n", e);
3566
+ return result;
3430
3567
  }
3431
3568
  }
3432
- }
3433
- for (const keyword of SAME_KEY_KEYWORDS) {
3434
- const subSchema = resolvedSchema[keyword];
3435
- if (subSchema && isObject(subSchema) && !Array.isArray(subSchema)) {
3436
- const currentUiSchema = result[keyword];
3437
- if (typeof currentUiSchema !== "function") {
3438
- const subUiSchema = currentUiSchema || {};
3439
- const expanded = expandUiSchemaDefinitions(subSchema, subUiSchema, registry, new Set(visited));
3440
- if (Object.keys(expanded).length > 0) {
3441
- result[keyword] = expanded;
3442
- }
3569
+ for (const keyword of [ONE_OF_KEY, ANY_OF_KEY]) {
3570
+ const schemaOptions = resolvedSchema[keyword];
3571
+ if (!Array.isArray(schemaOptions) || schemaOptions.length === 0) {
3572
+ continue;
3443
3573
  }
3444
- }
3445
- }
3446
- for (const keyword of ARRAY_KEYWORDS) {
3447
- const schemaOptions = resolvedSchema[keyword];
3448
- if (Array.isArray(schemaOptions) && schemaOptions.length > 0) {
3449
3574
  const currentUiSchemaArray = result[keyword];
3450
3575
  const uiSchemaArray = Array.isArray(currentUiSchemaArray) ? [...currentUiSchemaArray] : [];
3451
3576
  let hasExpanded = false;
3452
3577
  for (let i = 0; i < schemaOptions.length; i++) {
3453
- const optionSchema = schemaOptions[i];
3454
- const optionUiSchema = uiSchemaArray[i] || {};
3455
- const expanded = expandUiSchemaDefinitions(optionSchema, optionUiSchema, registry, new Set(visited));
3456
- if (Object.keys(expanded).length > 0) {
3457
- uiSchemaArray[i] = expanded;
3578
+ const option = schemaOptions[i];
3579
+ const optionRef = option?.[RJSF_REF_KEY] ?? option?.[REF_KEY];
3580
+ if (optionRef && optionRef in definitions) {
3581
+ const optionUiSchema = uiSchemaArray[i] || {};
3582
+ uiSchemaArray[i] = mergeObjects(definitions[optionRef], optionUiSchema);
3458
3583
  hasExpanded = true;
3459
3584
  }
3460
3585
  }
@@ -3465,17 +3590,6 @@ function expandUiSchemaDefinitions(currentSchema, uiSchema, registry, visited =
3465
3590
  }
3466
3591
  return result;
3467
3592
  }
3468
- function resolveUiSchema(schema, localUiSchema, registry) {
3469
- const ref = schema[RJSF_REF_KEY] ?? schema[REF_KEY];
3470
- const definitionUiSchema = ref ? registry.uiSchemaDefinitions?.[ref] : void 0;
3471
- if (!definitionUiSchema) {
3472
- return localUiSchema || {};
3473
- }
3474
- if (!localUiSchema || Object.keys(localUiSchema).length === 0) {
3475
- return { ...definitionUiSchema };
3476
- }
3477
- return mergeObjects(definitionUiSchema, localUiSchema);
3478
- }
3479
3593
 
3480
3594
  // src/schemaRequiresTrueValue.ts
3481
3595
  function schemaRequiresTrueValue(schema) {
@@ -3842,7 +3956,7 @@ function utcToLocal(jsonDate) {
3842
3956
  }
3843
3957
 
3844
3958
  // src/validationDataMerge.ts
3845
- var import_isEmpty7 = __toESM(require("lodash/isEmpty"), 1);
3959
+ var import_isEmpty8 = __toESM(require("lodash/isEmpty"), 1);
3846
3960
  function validationDataMerge(validationData, additionalErrorSchema, preventDuplicates = false) {
3847
3961
  if (!additionalErrorSchema) {
3848
3962
  return validationData;
@@ -3850,7 +3964,7 @@ function validationDataMerge(validationData, additionalErrorSchema, preventDupli
3850
3964
  const { errors: oldErrors, errorSchema: oldErrorSchema } = validationData;
3851
3965
  let errors = toErrorList(additionalErrorSchema);
3852
3966
  let errorSchema = additionalErrorSchema;
3853
- if (!(0, import_isEmpty7.default)(oldErrorSchema)) {
3967
+ if (!(0, import_isEmpty8.default)(oldErrorSchema)) {
3854
3968
  errorSchema = mergeObjects(
3855
3969
  oldErrorSchema,
3856
3970
  additionalErrorSchema,