@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.
@@ -1079,17 +1079,48 @@
1079
1079
  }
1080
1080
  return {};
1081
1081
  }
1082
+ /** Either add `computedDefault` at `key` into `obj` or not add it based on its value and the value of
1083
+ * `includeUndefinedValues`. Generally undefined `computedDefault` values are added only when `includeUndefinedValues`
1084
+ * is either true or "excludeObjectChildren". If `includeUndefinedValues` is false, then non-undefined and
1085
+ * non-empty-object values will be added.
1086
+ *
1087
+ * @param obj - The object into which the computed default may be added
1088
+ * @param key - The key into the object at which the computed default may be added
1089
+ * @param computedDefault - The computed default value that maybe should be added to the obj
1090
+ * @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
1091
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1092
+ * false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
1093
+ * values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
1094
+ * false when computing defaults for any nested object properties.
1095
+ * @param requiredFields - The list of fields that are required
1096
+ */
1097
+ function maybeAddDefaultToObject(obj, key, computedDefault, includeUndefinedValues, requiredFields) {
1098
+ if (requiredFields === void 0) {
1099
+ requiredFields = [];
1100
+ }
1101
+ if (includeUndefinedValues) {
1102
+ obj[key] = computedDefault;
1103
+ } else if (isObject(computedDefault)) {
1104
+ // Store computedDefault if it's a non-empty object (e.g. not {})
1105
+ if (!isEmpty__default["default"](computedDefault) || requiredFields.includes(key)) {
1106
+ obj[key] = computedDefault;
1107
+ }
1108
+ } else if (computedDefault !== undefined) {
1109
+ // Store computedDefault if it's a defined primitive (e.g. true)
1110
+ obj[key] = computedDefault;
1111
+ }
1112
+ }
1082
1113
  /** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
1083
1114
  * each level of the schema, recursively, to fill out every level of defaults provided by the schema.
1084
1115
  *
1085
1116
  * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
1086
- * @param schema - The schema for which the default state is desired
1117
+ * @param rawSchema - The schema for which the default state is desired
1087
1118
  * @param [parentDefaults] - Any defaults provided by the parent field in the schema
1088
1119
  * @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
1089
1120
  * @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
1090
1121
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1091
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1092
- * object properties.
1122
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1123
+ * false when computing defaults for any nested object properties.
1093
1124
  * @returns - The resulting `formData` with all the defaults provided
1094
1125
  */
1095
1126
  function computeDefaults(validator, rawSchema, parentDefaults, rootSchema, rawFormData, includeUndefinedValues) {
@@ -1121,8 +1152,14 @@
1121
1152
  return computeDefaults(validator, itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
1122
1153
  });
1123
1154
  } else if (ONE_OF_KEY in schema) {
1155
+ if (schema.oneOf.length === 0) {
1156
+ return undefined;
1157
+ }
1124
1158
  schema = schema.oneOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.oneOf, 0)];
1125
1159
  } else if (ANY_OF_KEY in schema) {
1160
+ if (schema.anyOf.length === 0) {
1161
+ return undefined;
1162
+ }
1126
1163
  schema = schema.anyOf[getClosestMatchingOption(validator, rootSchema, isEmpty__default["default"](formData) ? undefined : formData, schema.anyOf, 0)];
1127
1164
  }
1128
1165
  // Not defaults defined for this node, fallback to generic typed ones.
@@ -1132,23 +1169,25 @@
1132
1169
  switch (getSchemaType(schema)) {
1133
1170
  // We need to recur for object schema inner default values.
1134
1171
  case "object":
1135
- return Object.keys(schema.properties || {}).reduce(function (acc, key) {
1136
- // Compute the defaults for this node, with the parent defaults we might
1137
- // have from a previous run: defaults[key].
1138
- 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);
1139
- if (includeUndefinedValues) {
1140
- acc[key] = computedDefault;
1141
- } else if (isObject(computedDefault)) {
1142
- // Store computedDefault if it's a non-empty object (e.g. not {})
1143
- if (!isEmpty__default["default"](computedDefault)) {
1144
- acc[key] = computedDefault;
1145
- }
1146
- } else if (computedDefault !== undefined) {
1147
- // Store computedDefault if it's a defined primitive (e.g. true)
1148
- acc[key] = computedDefault;
1172
+ {
1173
+ var objectDefaults = Object.keys(schema.properties || {}).reduce(function (acc, key) {
1174
+ // Compute the defaults for this node, with the parent defaults we might
1175
+ // have from a previous run: defaults[key].
1176
+ var computedDefault = computeDefaults(validator, get__default["default"](schema, [PROPERTIES_KEY, key]), get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1177
+ maybeAddDefaultToObject(acc, key, computedDefault, includeUndefinedValues, schema.required);
1178
+ return acc;
1179
+ }, {});
1180
+ if (schema.additionalProperties && isObject(defaults)) {
1181
+ var additionalPropertiesSchema = isObject(schema.additionalProperties) ? schema.additionalProperties : {}; // as per spec additionalProperties may be either schema or boolean
1182
+ Object.keys(defaults).filter(function (key) {
1183
+ return !schema.properties || !schema.properties[key];
1184
+ }).forEach(function (key) {
1185
+ var computedDefault = computeDefaults(validator, additionalPropertiesSchema, get__default["default"](defaults, [key]), rootSchema, get__default["default"](formData, [key]), includeUndefinedValues === true);
1186
+ maybeAddDefaultToObject(objectDefaults, key, computedDefault, includeUndefinedValues);
1187
+ });
1149
1188
  }
1150
- return acc;
1151
- }, {});
1189
+ return objectDefaults;
1190
+ }
1152
1191
  case "array":
1153
1192
  // Inject defaults into existing array defaults
1154
1193
  if (Array.isArray(defaults)) {
@@ -1190,8 +1229,8 @@
1190
1229
  * @param [formData] - The current formData, if any, onto which to provide any missing defaults
1191
1230
  * @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
1192
1231
  * @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
1193
- * If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
1194
- * object properties.
1232
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
1233
+ * false when computing defaults for any nested object properties.
1195
1234
  * @returns - The resulting `formData` with all the defaults provided
1196
1235
  */
1197
1236
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues) {