@rjsf/utils 5.0.2 → 5.1.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.
package/dist/index.d.ts CHANGED
@@ -776,8 +776,8 @@ interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F ex
776
776
  * @param schema - The schema for which the default state is desired
777
777
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
778
778
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
779
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
780
- * object properties.
779
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
780
+ * false when computing defaults for any nested object properties.
781
781
  * @returns - The resulting `formData` with all the defaults provided
782
782
  */
783
783
  getDefaultFormState(schema: S, formData?: T, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
@@ -1406,8 +1406,8 @@ declare const UI_OPTIONS_KEY = "ui:options";
1406
1406
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1407
1407
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1408
1408
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1409
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1410
- * object properties.
1409
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1410
+ * false when computing defaults for any nested object properties.
1411
1411
  * @returns - The resulting `formData` with all the defaults provided
1412
1412
  */
1413
1413
  declare function getDefaultFormState<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, theSchema: S, formData?: T, rootSchema?: S, includeUndefinedValues?: boolean | "excludeObjectChildren"): T | T[] | undefined;
@@ -1095,17 +1095,48 @@ function getInnerSchemaForArrayItem(schema, additionalItems, idx) {
1095
1095
  }
1096
1096
  return {};
1097
1097
  }
1098
+ /** Either add `computedDefault` at `key` into `obj` or not add it based on its value and the value of
1099
+ * `includeUndefinedValues`. Generally undefined `computedDefault` values are added only when `includeUndefinedValues`
1100
+ * is either true or "excludeObjectChildren". If `includeUndefinedValues` is false, then non-undefined and
1101
+ * non-empty-object values will be added.
1102
+ *
1103
+ * @param obj - The object into which the computed default may be added
1104
+ * @param key - The key into the object at which the computed default may be added
1105
+ * @param computedDefault - The computed default value that maybe should be added to the obj
1106
+ * @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
1107
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1108
+ * false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
1109
+ * values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
1110
+ * false when computing defaults for any nested object properties.
1111
+ * @param requiredFields - The list of fields that are required
1112
+ */
1113
+ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
1114
+ if (requiredFields === void 0) {
1115
+ requiredFields = [];
1116
+ }
1117
+ if (includeUndefinedValues) {
1118
+ obj[key] = computedDefault;
1119
+ } else if (isObject(computedDefault)) {
1120
+ // Store computedDefault if it's a non-empty object (e.g. not {})
1121
+ if (!isEmpty__default["default"](computedDefault) || requiredFields.includes(key)) {
1122
+ obj[key] = computedDefault;
1123
+ }
1124
+ } else if (computedDefault !== undefined) {
1125
+ // Store computedDefault if it's a defined primitive (e.g. true)
1126
+ obj[key] = computedDefault;
1127
+ }
1128
+ }
1098
1129
  /** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
1099
1130
  * each level of the schema, recursively, to fill out every level of defaults provided by the schema.
1100
1131
  *
1101
1132
  * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
1102
- * @param schema - The schema for which the default state is desired
1133
+ * @param rawSchema - The schema for which the default state is desired
1103
1134
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
1104
1135
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
1105
1136
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
1106
1137
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1107
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1108
- * object properties.
1138
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1139
+ * false when computing defaults for any nested object properties.
1109
1140
  * @returns - The resulting `formData` with all the defaults provided
1110
1141
  */
1111
1142
  function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -1137,8 +1168,14 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1137
1168
  return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1138
1169
  });
1139
1170
  } else if (ONE_OF_KEY in schema) {
1171
+ if (schema.oneOf.length === 0) {
1172
+ return undefined;
1173
+ }
1140
1174
  schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0)];
1141
1175
  } else if (ANY_OF_KEY in schema) {
1176
+ if (schema.anyOf.length === 0) {
1177
+ return undefined;
1178
+ }
1142
1179
  schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0)];
1143
1180
  }
1144
1181
  // Not defaults defined for this node, fallback to generic typed ones.
@@ -1148,23 +1185,25 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1148
1185
  switch (getSchemaType(schema)) {
1149
1186
  // We need to recur for object schema inner default values.
1150
1187
  case "object":
1151
- return Object.keys(schema.properties || {}).reduce(function (acc, key) {
1152
- // Compute the defaults for this node, with the parent defaults we might
1153
- // have from a previous run: defaults[key].
1154
- 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);
1155
- if (includeUndefinedValues) {
1156
- acc[key] = computedDefault;
1157
- } else if (isObject(computedDefault)) {
1158
- // Store computedDefault if it's a non-empty object (e.g. not {})
1159
- if (!isEmpty__default["default"](computedDefault)) {
1160
- acc[key] = computedDefault;
1161
- }
1162
- } else if (computedDefault !== undefined) {
1163
- // Store computedDefault if it's a defined primitive (e.g. true)
1164
- acc[key] = computedDefault;
1188
+ {
1189
+ var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1190
+ // Compute the defaults for this node, with the parent defaults we might
1191
+ // have from a previous run: defaults[key].
1192
+ var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1193
+ maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1194
+ return acc;
1195
+ }, {});
1196
+ if (schema.additionalProperties && isObject(defaults)) {
1197
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1198
+ Object.keys(defaults).filter(function (key) {
1199
+ return !schema.properties || !schema.properties[key];
1200
+ }).forEach(function (key) {
1201
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1202
+ maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1203
+ });
1165
1204
  }
1166
- return acc;
1167
- }, {});
1205
+ return objectDefaults;
1206
+ }
1168
1207
  case "array":
1169
1208
  // Inject defaults into existing array defaults
1170
1209
  if (Array.isArray(defaults)) {
@@ -1206,8 +1245,8 @@ function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFo
1206
1245
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1207
1246
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1208
1247
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1209
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1210
- * object properties.
1248
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1249
+ * false when computing defaults for any nested object properties.
1211
1250
  * @returns - The resulting `formData` with all the defaults provided
1212
1251
  */
1213
1252
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {