@rjsf/utils 5.0.0-beta.13 → 5.0.0-beta.14

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
@@ -397,7 +397,9 @@ function mergeDefaultsWithFormData(defaults, formData) {
397
397
  *
398
398
  * @param obj1 - The first object to merge
399
399
  * @param obj2 - The second object to merge
400
- * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated
400
+ * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated. Use
401
+ * "preventDuplicates" to merge arrays in a manner that prevents any duplicate entries from being merged.
402
+ * NOTE: Uses shallow comparison for the duplicate checking.
401
403
  * @returns - A new object that is the merge of the two given objects
402
404
  */
403
405
  function mergeObjects(obj1, obj2, concatArrays) {
@@ -410,7 +412,16 @@ function mergeObjects(obj1, obj2, concatArrays) {
410
412
  if (obj1 && key in obj1 && isObject(right)) {
411
413
  acc[key] = mergeObjects(left, right, concatArrays);
412
414
  } else if (concatArrays && Array.isArray(left) && Array.isArray(right)) {
413
- acc[key] = left.concat(right);
415
+ let toMerge = right;
416
+ if (concatArrays === "preventDuplicates") {
417
+ toMerge = right.reduce((result, value) => {
418
+ if (!left.includes(value)) {
419
+ result.push(value);
420
+ }
421
+ return result;
422
+ }, []);
423
+ }
424
+ acc[key] = left.concat(toMerge);
414
425
  } else {
415
426
  acc[key] = right;
416
427
  }
@@ -870,7 +881,9 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
870
881
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
871
882
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
872
883
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
873
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
884
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
885
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
886
+ * object properties.
874
887
  * @returns - The resulting `formData` with all the defaults provided
875
888
  */
876
889
  function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -913,8 +926,16 @@ function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormD
913
926
  return Object.keys(schema.properties || {}).reduce((acc, key) => {
914
927
  // Compute the defaults for this node, with the parent defaults we might
915
928
  // have from a previous run: defaults[key].
916
- const computedDefault = computeDefaults(validator, get(schema, [PROPERTIES_KEY, key]), get(defaults, [key]), rootSchema, get(formData, [key]), includeUndefinedValues);
917
- if (includeUndefinedValues || computedDefault !== undefined) {
929
+ const computedDefault = computeDefaults(validator, get(schema, [PROPERTIES_KEY, key]), get(defaults, [key]), rootSchema, get(formData, [key]), includeUndefinedValues === "excludeObjectChildren" ? false : includeUndefinedValues);
930
+ if (includeUndefinedValues) {
931
+ acc[key] = computedDefault;
932
+ } else if (isObject(computedDefault)) {
933
+ // Store computedDefault if it's a non-empty object (e.g. not {})
934
+ if (!isEmpty(computedDefault)) {
935
+ acc[key] = computedDefault;
936
+ }
937
+ } else if (computedDefault !== undefined) {
938
+ // Store computedDefault if it's a defined primitive (e.g. true)
918
939
  acc[key] = computedDefault;
919
940
  }
920
941
  return acc;
@@ -959,7 +980,9 @@ function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormD
959
980
  * @param theSchema - The schema for which the default state is desired
960
981
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
961
982
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
962
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
983
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
984
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
985
+ * object properties.
963
986
  * @returns - The resulting `formData` with all the defaults provided
964
987
  */
965
988
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
@@ -1210,7 +1233,9 @@ class SchemaUtils {
1210
1233
  *
1211
1234
  * @param schema - The schema for which the default state is desired
1212
1235
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1213
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
1236
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1237
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1238
+ * object properties.
1214
1239
  * @returns - The resulting `formData` with all the defaults provided
1215
1240
  */
1216
1241
  getDefaultFormState(schema, formData, includeUndefinedValues) {