@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/index.d.ts CHANGED
@@ -742,10 +742,12 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
742
742
  *
743
743
  * @param schema - The schema for which the default state is desired
744
744
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
745
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
745
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
746
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
747
+ * object properties.
746
748
  * @returns - The resulting `formData` with all the defaults provided
747
749
  */
748
- getDefaultFormState(schema: S, formData?: T, includeUndefinedValues?: boolean): T | T[] | undefined;
750
+ getDefaultFormState(schema: S, formData?: T, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
749
751
  /** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
750
752
  * should be displayed in a UI.
751
753
  *
@@ -1026,10 +1028,12 @@ declare function mergeDefaultsWithFormData<T = any>(defaults: T, formData: T): T
1026
1028
  *
1027
1029
  * @param obj1 - The first object to merge
1028
1030
  * @param obj2 - The second object to merge
1029
- * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated
1031
+ * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated. Use
1032
+ * "preventDuplicates" to merge arrays in a manner that prevents any duplicate entries from being merged.
1033
+ * NOTE: Uses shallow comparison for the duplicate checking.
1030
1034
  * @returns - A new object that is the merge of the two given objects
1031
1035
  */
1032
- declare function mergeObjects(obj1: GenericObjectType, obj2: GenericObjectType, concatArrays?: boolean): GenericObjectType;
1036
+ declare function mergeObjects(obj1: GenericObjectType, obj2: GenericObjectType, concatArrays?: boolean | "preventDuplicates"): GenericObjectType;
1033
1037
 
1034
1038
  /** Recursively merge deeply nested schemas. The difference between `mergeSchemas` and `mergeObjects` is that
1035
1039
  * `mergeSchemas` only concats arrays for values under the 'required' keyword, and when it does, it doesn't include
@@ -1180,10 +1184,12 @@ declare const UI_OPTIONS_KEY = "ui:options";
1180
1184
  * @param theSchema - The schema for which the default state is desired
1181
1185
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1182
1186
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1183
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
1187
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1188
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1189
+ * object properties.
1184
1190
  * @returns - The resulting `formData` with all the defaults provided
1185
1191
  */
1186
- declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFSchema>(validator: ValidatorType<T, S>, theSchema: S, formData?: T, rootSchema?: S, includeUndefinedValues?: boolean): T | T[] | undefined;
1192
+ declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFSchema>(validator: ValidatorType<T, S>, theSchema: S, formData?: T, rootSchema?: S, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
1187
1193
 
1188
1194
  /** Determines whether the combination of `schema` and `uiSchema` properties indicates that the label for the `schema`
1189
1195
  * should be displayed in a UI.
@@ -414,7 +414,9 @@ function mergeDefaultsWithFormData(defaults, formData) {
414
414
  *
415
415
  * @param obj1 - The first object to merge
416
416
  * @param obj2 - The second object to merge
417
- * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated
417
+ * @param [concatArrays=false] - Optional flag that, when true, will cause arrays to be concatenated. Use
418
+ * "preventDuplicates" to merge arrays in a manner that prevents any duplicate entries from being merged.
419
+ * NOTE: Uses shallow comparison for the duplicate checking.
418
420
  * @returns - A new object that is the merge of the two given objects
419
421
  */
420
422
  function mergeObjects(obj1, obj2, concatArrays) {
@@ -427,7 +429,16 @@ function mergeObjects(obj1, obj2, concatArrays) {
427
429
  if (obj1 && key in obj1 && isObject(right)) {
428
430
  acc[key] = mergeObjects(left, right, concatArrays);
429
431
  } else if (concatArrays && Array.isArray(left) && Array.isArray(right)) {
430
- acc[key] = left.concat(right);
432
+ let toMerge = right;
433
+ if (concatArrays === "preventDuplicates") {
434
+ toMerge = right.reduce((result, value) => {
435
+ if (!left.includes(value)) {
436
+ result.push(value);
437
+ }
438
+ return result;
439
+ }, []);
440
+ }
441
+ acc[key] = left.concat(toMerge);
431
442
  } else {
432
443
  acc[key] = right;
433
444
  }
@@ -887,7 +898,9 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
887
898
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
888
899
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
889
900
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
890
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
901
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
902
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
903
+ * object properties.
891
904
  * @returns - The resulting `formData` with all the defaults provided
892
905
  */
893
906
  function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -930,8 +943,16 @@ function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormD
930
943
  return Object.keys(schema.properties || {}).reduce((acc, key) => {
931
944
  // Compute the defaults for this node, with the parent defaults we might
932
945
  // have from a previous run: defaults[key].
933
- const computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues);
934
- if (includeUndefinedValues || computedDefault !== undefined) {
946
+ const 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);
947
+ if (includeUndefinedValues) {
948
+ acc[key] = computedDefault;
949
+ } else if (isObject(computedDefault)) {
950
+ // Store computedDefault if it's a non-empty object (e.g. not {})
951
+ if (!isEmpty__default["default"](computedDefault)) {
952
+ acc[key] = computedDefault;
953
+ }
954
+ } else if (computedDefault !== undefined) {
955
+ // Store computedDefault if it's a defined primitive (e.g. true)
935
956
  acc[key] = computedDefault;
936
957
  }
937
958
  return acc;
@@ -976,7 +997,9 @@ function computeDefaults(validator, schema, parentDefaults, rootSchema, rawFormD
976
997
  * @param theSchema - The schema for which the default state is desired
977
998
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
978
999
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
979
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
1000
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1001
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1002
+ * object properties.
980
1003
  * @returns - The resulting `formData` with all the defaults provided
981
1004
  */
982
1005
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {
@@ -1227,7 +1250,9 @@ class SchemaUtils {
1227
1250
  *
1228
1251
  * @param schema - The schema for which the default state is desired
1229
1252
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1230
- * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults
1253
+ * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1254
+ * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1255
+ * object properties.
1231
1256
  * @returns - The resulting `formData` with all the defaults provided
1232
1257
  */
1233
1258
  getDefaultFormState(schema, formData, includeUndefinedValues) {