@rjsf/utils 5.20.1 → 5.21.1

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.umd.js CHANGED
@@ -941,15 +941,16 @@
941
941
  }
942
942
  }
943
943
  }
944
- function computeDefaults(validator, rawSchema, {
945
- parentDefaults,
946
- rawFormData,
947
- rootSchema = {},
948
- includeUndefinedValues = false,
949
- _recurseList = [],
950
- experimental_defaultFormStateBehavior = void 0,
951
- required
952
- } = {}) {
944
+ function computeDefaults(validator, rawSchema, computeDefaultsProps = {}) {
945
+ const {
946
+ parentDefaults,
947
+ rawFormData,
948
+ rootSchema = {},
949
+ includeUndefinedValues = false,
950
+ _recurseList = [],
951
+ experimental_defaultFormStateBehavior = void 0,
952
+ required
953
+ } = computeDefaultsProps;
953
954
  const formData = isObject(rawFormData) ? rawFormData : {};
954
955
  const schema = isObject(rawSchema) ? rawSchema : {};
955
956
  let defaults = parentDefaults;
@@ -966,7 +967,11 @@
966
967
  schemaToCompute = findSchemaDefinition(refName, rootSchema);
967
968
  }
968
969
  } else if (DEPENDENCIES_KEY in schema) {
969
- const resolvedSchema = resolveDependencies(validator, schema, rootSchema, false, [], formData);
970
+ const defaultFormData = {
971
+ ...formData,
972
+ ...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults)
973
+ };
974
+ const resolvedSchema = resolveDependencies(validator, schema, rootSchema, false, [], defaultFormData);
970
975
  schemaToCompute = resolvedSchema[0];
971
976
  } else if (isFixedItems(schema)) {
972
977
  defaults = schema.items.map(
@@ -1025,127 +1030,154 @@
1025
1030
  if (defaults === void 0) {
1026
1031
  defaults = schema.default;
1027
1032
  }
1028
- switch (getSchemaType(schema)) {
1029
- case "object": {
1030
- const retrievedSchema = experimental_defaultFormStateBehavior?.allOf === "populateDefaults" && ALL_OF_KEY in schema ? retrieveSchema(validator, schema, rootSchema, formData) : schema;
1031
- const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
1032
- (acc, key) => {
1033
- const computedDefault = computeDefaults(validator, get8(retrievedSchema, [PROPERTIES_KEY, key]), {
1034
- rootSchema,
1035
- _recurseList,
1036
- experimental_defaultFormStateBehavior,
1037
- includeUndefinedValues: includeUndefinedValues === true,
1038
- parentDefaults: get8(defaults, [key]),
1039
- rawFormData: get8(formData, [key]),
1040
- required: retrievedSchema.required?.includes(key)
1041
- });
1042
- maybeAddDefaultToObject(
1043
- acc,
1044
- key,
1045
- computedDefault,
1046
- includeUndefinedValues,
1047
- required,
1048
- retrievedSchema.required,
1049
- experimental_defaultFormStateBehavior
1050
- );
1051
- return acc;
1052
- },
1053
- {}
1054
- );
1055
- if (retrievedSchema.additionalProperties) {
1056
- const additionalPropertiesSchema = isObject(retrievedSchema.additionalProperties) ? retrievedSchema.additionalProperties : {};
1057
- const keys = /* @__PURE__ */ new Set();
1058
- if (isObject(defaults)) {
1059
- Object.keys(defaults).filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key]).forEach((key) => keys.add(key));
1060
- }
1061
- const formDataRequired = [];
1062
- Object.keys(formData).filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key]).forEach((key) => {
1063
- keys.add(key);
1064
- formDataRequired.push(key);
1065
- });
1066
- keys.forEach((key) => {
1067
- const computedDefault = computeDefaults(validator, additionalPropertiesSchema, {
1068
- rootSchema,
1069
- _recurseList,
1070
- experimental_defaultFormStateBehavior,
1071
- includeUndefinedValues: includeUndefinedValues === true,
1072
- parentDefaults: get8(defaults, [key]),
1073
- rawFormData: get8(formData, [key]),
1074
- required: retrievedSchema.required?.includes(key)
1075
- });
1076
- maybeAddDefaultToObject(
1077
- objectDefaults,
1078
- key,
1079
- computedDefault,
1080
- includeUndefinedValues,
1081
- required,
1082
- formDataRequired
1083
- );
1033
+ const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults);
1034
+ return defaultBasedOnSchemaType ?? defaults;
1035
+ }
1036
+ function getObjectDefaults(validator, rawSchema, {
1037
+ rawFormData,
1038
+ rootSchema = {},
1039
+ includeUndefinedValues = false,
1040
+ _recurseList = [],
1041
+ experimental_defaultFormStateBehavior = void 0,
1042
+ required
1043
+ } = {}, defaults) {
1044
+ {
1045
+ const formData = isObject(rawFormData) ? rawFormData : {};
1046
+ const schema = rawSchema;
1047
+ const retrievedSchema = experimental_defaultFormStateBehavior?.allOf === "populateDefaults" && ALL_OF_KEY in schema ? retrieveSchema(validator, schema, rootSchema, formData) : schema;
1048
+ const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
1049
+ (acc, key) => {
1050
+ const computedDefault = computeDefaults(validator, get8(retrievedSchema, [PROPERTIES_KEY, key]), {
1051
+ rootSchema,
1052
+ _recurseList,
1053
+ experimental_defaultFormStateBehavior,
1054
+ includeUndefinedValues: includeUndefinedValues === true,
1055
+ parentDefaults: get8(defaults, [key]),
1056
+ rawFormData: get8(formData, [key]),
1057
+ required: retrievedSchema.required?.includes(key)
1084
1058
  });
1059
+ maybeAddDefaultToObject(
1060
+ acc,
1061
+ key,
1062
+ computedDefault,
1063
+ includeUndefinedValues,
1064
+ required,
1065
+ retrievedSchema.required,
1066
+ experimental_defaultFormStateBehavior
1067
+ );
1068
+ return acc;
1069
+ },
1070
+ {}
1071
+ );
1072
+ if (retrievedSchema.additionalProperties) {
1073
+ const additionalPropertiesSchema = isObject(retrievedSchema.additionalProperties) ? retrievedSchema.additionalProperties : {};
1074
+ const keys = /* @__PURE__ */ new Set();
1075
+ if (isObject(defaults)) {
1076
+ Object.keys(defaults).filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key]).forEach((key) => keys.add(key));
1085
1077
  }
1086
- return objectDefaults;
1087
- }
1088
- case "array": {
1089
- const neverPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === "never";
1090
- const ignoreMinItemsFlagSet = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === "requiredOnly";
1091
- const isSkipEmptyDefaults = experimental_defaultFormStateBehavior?.emptyObjectFields === "skipEmptyDefaults";
1092
- const computeSkipPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.computeSkipPopulate ?? (() => false);
1093
- const emptyDefault = isSkipEmptyDefaults ? void 0 : [];
1094
- if (Array.isArray(defaults)) {
1095
- defaults = defaults.map((item, idx) => {
1096
- const schemaItem = getInnerSchemaForArrayItem(schema, 2 /* Fallback */, idx);
1097
- return computeDefaults(validator, schemaItem, {
1098
- rootSchema,
1099
- _recurseList,
1100
- experimental_defaultFormStateBehavior,
1101
- parentDefaults: item,
1102
- required
1103
- });
1078
+ const formDataRequired = [];
1079
+ Object.keys(formData).filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key]).forEach((key) => {
1080
+ keys.add(key);
1081
+ formDataRequired.push(key);
1082
+ });
1083
+ keys.forEach((key) => {
1084
+ const computedDefault = computeDefaults(validator, additionalPropertiesSchema, {
1085
+ rootSchema,
1086
+ _recurseList,
1087
+ experimental_defaultFormStateBehavior,
1088
+ includeUndefinedValues: includeUndefinedValues === true,
1089
+ parentDefaults: get8(defaults, [key]),
1090
+ rawFormData: get8(formData, [key]),
1091
+ required: retrievedSchema.required?.includes(key)
1104
1092
  });
1105
- }
1106
- if (Array.isArray(rawFormData)) {
1107
- const schemaItem = getInnerSchemaForArrayItem(schema);
1108
- if (neverPopulate) {
1109
- defaults = rawFormData;
1110
- } else {
1111
- defaults = rawFormData.map((item, idx) => {
1112
- return computeDefaults(validator, schemaItem, {
1113
- rootSchema,
1114
- _recurseList,
1115
- experimental_defaultFormStateBehavior,
1116
- rawFormData: item,
1117
- parentDefaults: get8(defaults, [idx]),
1118
- required
1119
- });
1120
- });
1121
- }
1122
- }
1123
- if (neverPopulate) {
1124
- return defaults ?? emptyDefault;
1125
- }
1126
- if (ignoreMinItemsFlagSet && !required) {
1127
- return defaults ? defaults : void 0;
1128
- }
1129
- const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
1130
- if (!schema.minItems || isMultiSelect(validator, schema, rootSchema) || computeSkipPopulate(validator, schema, rootSchema) || schema.minItems <= defaultsLength) {
1131
- return defaults ? defaults : emptyDefault;
1132
- }
1133
- const defaultEntries = defaults || [];
1134
- const fillerSchema = getInnerSchemaForArrayItem(schema, 1 /* Invert */);
1135
- const fillerDefault = fillerSchema.default;
1136
- const fillerEntries = new Array(schema.minItems - defaultsLength).fill(
1137
- computeDefaults(validator, fillerSchema, {
1138
- parentDefaults: fillerDefault,
1093
+ maybeAddDefaultToObject(
1094
+ objectDefaults,
1095
+ key,
1096
+ computedDefault,
1097
+ includeUndefinedValues,
1098
+ required,
1099
+ formDataRequired
1100
+ );
1101
+ });
1102
+ }
1103
+ return objectDefaults;
1104
+ }
1105
+ }
1106
+ function getArrayDefaults(validator, rawSchema, {
1107
+ rawFormData,
1108
+ rootSchema = {},
1109
+ _recurseList = [],
1110
+ experimental_defaultFormStateBehavior = void 0,
1111
+ required
1112
+ } = {}, defaults) {
1113
+ const schema = rawSchema;
1114
+ const neverPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === "never";
1115
+ const ignoreMinItemsFlagSet = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === "requiredOnly";
1116
+ const isSkipEmptyDefaults = experimental_defaultFormStateBehavior?.emptyObjectFields === "skipEmptyDefaults";
1117
+ const computeSkipPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.computeSkipPopulate ?? (() => false);
1118
+ const emptyDefault = isSkipEmptyDefaults ? void 0 : [];
1119
+ if (Array.isArray(defaults)) {
1120
+ defaults = defaults.map((item, idx) => {
1121
+ const schemaItem = getInnerSchemaForArrayItem(schema, 2 /* Fallback */, idx);
1122
+ return computeDefaults(validator, schemaItem, {
1123
+ rootSchema,
1124
+ _recurseList,
1125
+ experimental_defaultFormStateBehavior,
1126
+ parentDefaults: item,
1127
+ required
1128
+ });
1129
+ });
1130
+ }
1131
+ if (Array.isArray(rawFormData)) {
1132
+ const schemaItem = getInnerSchemaForArrayItem(schema);
1133
+ if (neverPopulate) {
1134
+ defaults = rawFormData;
1135
+ } else {
1136
+ defaults = rawFormData.map((item, idx) => {
1137
+ return computeDefaults(validator, schemaItem, {
1139
1138
  rootSchema,
1140
1139
  _recurseList,
1141
1140
  experimental_defaultFormStateBehavior,
1141
+ rawFormData: item,
1142
+ parentDefaults: get8(defaults, [idx]),
1142
1143
  required
1143
- })
1144
- );
1145
- return defaultEntries.concat(fillerEntries);
1144
+ });
1145
+ });
1146
+ }
1147
+ }
1148
+ if (neverPopulate) {
1149
+ return defaults ?? emptyDefault;
1150
+ }
1151
+ if (ignoreMinItemsFlagSet && !required) {
1152
+ return defaults ? defaults : void 0;
1153
+ }
1154
+ const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
1155
+ if (!schema.minItems || isMultiSelect(validator, schema, rootSchema) || computeSkipPopulate(validator, schema, rootSchema) || schema.minItems <= defaultsLength) {
1156
+ return defaults ? defaults : emptyDefault;
1157
+ }
1158
+ const defaultEntries = defaults || [];
1159
+ const fillerSchema = getInnerSchemaForArrayItem(schema, 1 /* Invert */);
1160
+ const fillerDefault = fillerSchema.default;
1161
+ const fillerEntries = new Array(schema.minItems - defaultsLength).fill(
1162
+ computeDefaults(validator, fillerSchema, {
1163
+ parentDefaults: fillerDefault,
1164
+ rootSchema,
1165
+ _recurseList,
1166
+ experimental_defaultFormStateBehavior,
1167
+ required
1168
+ })
1169
+ );
1170
+ return defaultEntries.concat(fillerEntries);
1171
+ }
1172
+ function getDefaultBasedOnSchemaType(validator, rawSchema, computeDefaultsProps = {}, defaults) {
1173
+ switch (getSchemaType(rawSchema)) {
1174
+ case "object": {
1175
+ return getObjectDefaults(validator, rawSchema, computeDefaultsProps, defaults);
1176
+ }
1177
+ case "array": {
1178
+ return getArrayDefaults(validator, rawSchema, computeDefaultsProps, defaults);
1146
1179
  }
1147
1180
  }
1148
- return defaults;
1149
1181
  }
1150
1182
  function getDefaultFormState(validator, theSchema, formData, rootSchema, includeUndefinedValues = false, experimental_defaultFormStateBehavior) {
1151
1183
  if (!isObject(theSchema)) {
@@ -23,12 +23,22 @@ export declare enum AdditionalItemsHandling {
23
23
  */
24
24
  export declare function getInnerSchemaForArrayItem<S extends StrictRJSFSchema = RJSFSchema>(schema: S, additionalItems?: AdditionalItemsHandling, idx?: number): S;
25
25
  interface ComputeDefaultsProps<T = any, S extends StrictRJSFSchema = RJSFSchema> {
26
+ /** Any defaults provided by the parent field in the schema */
26
27
  parentDefaults?: T;
28
+ /** The options root schema, used to primarily to look up `$ref`s */
27
29
  rootSchema?: S;
30
+ /** The current formData, if any, onto which to provide any missing defaults */
28
31
  rawFormData?: T;
32
+ /** Optional flag, if true, cause undefined values to be added as defaults.
33
+ * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
34
+ * false when computing defaults for any nested object properties.
35
+ */
29
36
  includeUndefinedValues?: boolean | 'excludeObjectChildren';
37
+ /** The list of ref names currently being recursed, used to prevent infinite recursion */
30
38
  _recurseList?: string[];
39
+ /** Optional configuration object, if provided, allows users to override default form state behavior */
31
40
  experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior;
41
+ /** Optional flag, if true, indicates this schema was required in the parent schema. */
32
42
  required?: boolean;
33
43
  }
34
44
  /** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
@@ -36,19 +46,37 @@ interface ComputeDefaultsProps<T = any, S extends StrictRJSFSchema = RJSFSchema>
36
46
  *
37
47
  * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
38
48
  * @param rawSchema - The schema for which the default state is desired
39
- * @param [props] - Optional props for this function
40
- * @param [props.parentDefaults] - Any defaults provided by the parent field in the schema
41
- * @param [props.rootSchema] - The options root schema, used to primarily to look up `$ref`s
42
- * @param [props.rawFormData] - The current formData, if any, onto which to provide any missing defaults
43
- * @param [props.includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
44
- * If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
45
- * false when computing defaults for any nested object properties.
46
- * @param [props._recurseList=[]] - The list of ref names currently being recursed, used to prevent infinite recursion
47
- * @param [props.experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
48
- * @param [props.required] - Optional flag, if true, indicates this schema was required in the parent schema.
49
+ * @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
49
50
  * @returns - The resulting `formData` with all the defaults provided
50
51
  */
51
- export declare function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rawSchema: S, { parentDefaults, rawFormData, rootSchema, includeUndefinedValues, _recurseList, experimental_defaultFormStateBehavior, required, }?: ComputeDefaultsProps<T, S>): T | T[] | undefined;
52
+ export declare function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rawSchema: S, computeDefaultsProps?: ComputeDefaultsProps<T, S>): T | T[] | undefined;
53
+ /** Computes the default value for objects.
54
+ *
55
+ * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
56
+ * @param rawSchema - The schema for which the default state is desired
57
+ * @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
58
+ * @param defaults - Optional props for this function
59
+ * @returns - The default value based on the schema type if they are defined for object or array schemas.
60
+ */
61
+ export declare function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rawSchema: S, { rawFormData, rootSchema, includeUndefinedValues, _recurseList, experimental_defaultFormStateBehavior, required, }?: ComputeDefaultsProps<T, S>, defaults?: T | T[] | undefined): T;
62
+ /** Computes the default value for arrays.
63
+ *
64
+ * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
65
+ * @param rawSchema - The schema for which the default state is desired
66
+ * @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
67
+ * @param defaults - Optional props for this function
68
+ * @returns - The default value based on the schema type if they are defined for object or array schemas.
69
+ */
70
+ export declare function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rawSchema: S, { rawFormData, rootSchema, _recurseList, experimental_defaultFormStateBehavior, required, }?: ComputeDefaultsProps<T, S>, defaults?: T | T[] | undefined): T | T[] | undefined;
71
+ /** Computes the default value based on the schema type.
72
+ *
73
+ * @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
74
+ * @param rawSchema - The schema for which the default state is desired
75
+ * @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
76
+ * @param defaults - Optional props for this function
77
+ * @returns - The default value based on the schema type if they are defined for object or array schemas.
78
+ */
79
+ export declare function getDefaultBasedOnSchemaType<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, rawSchema: S, computeDefaultsProps?: ComputeDefaultsProps<T, S>, defaults?: T | T[] | undefined): T | T[] | void;
52
80
  /** Returns the superset of `formData` that includes the given set updated to include any missing fields that have
53
81
  * computed to have defaults provided in the `schema`.
54
82
  *