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