forked from rjsf-team/react-jsonschema-form
/
getDefaultFormState.ts
324 lines (315 loc) · 11.3 KB
/
getDefaultFormState.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import get from "lodash/get";
import isEmpty from "lodash/isEmpty";
import {
ANY_OF_KEY,
DEFAULT_KEY,
DEPENDENCIES_KEY,
PROPERTIES_KEY,
ONE_OF_KEY,
REF_KEY,
} from "../constants";
import findSchemaDefinition from "../findSchemaDefinition";
import getMatchingOption from "./getMatchingOption";
import getSchemaType from "../getSchemaType";
import isObject from "../isObject";
import isFixedItems from "../isFixedItems";
import mergeDefaultsWithFormData from "../mergeDefaultsWithFormData";
import mergeObjects from "../mergeObjects";
import {
GenericObjectType,
RJSFSchema,
StrictRJSFSchema,
ValidatorType,
} from "../types";
import isMultiSelect from "./isMultiSelect";
import retrieveSchema, { resolveDependencies } from "./retrieveSchema";
/** Enum that indicates how `schema.additionalItems` should be handled by the `getInnerSchemaForArrayItem()` function.
*/
export enum AdditionalItemsHandling {
Ignore,
Invert,
Fallback,
}
/** Given a `schema` will return an inner schema that for an array item. This is computed differently based on the
* `additionalItems` enum and the value of `idx`. There are four possible returns:
* 1. If `idx` is >= 0, then if `schema.items` is an array the `idx`th element of the array is returned if it is a valid
* index and not a boolean, otherwise it falls through to 3.
* 2. If `schema.items` is not an array AND truthy and not a boolean, then `schema.items` is returned since it actually
* is a schema, otherwise it falls through to 3.
* 3. If `additionalItems` is not `AdditionalItemsHandling.Ignore` and `schema.additionalItems` is an object, then
* `schema.additionalItems` is returned since it actually is a schema, otherwise it falls through to 4.
* 4. {} is returned representing an empty schema
*
* @param schema - The schema from which to get the particular item
* @param [additionalItems=AdditionalItemsHandling.Ignore] - How do we want to handle additional items?
* @param [idx=-1] - Index, if non-negative, will be used to return the idx-th element in a `schema.items` array
* @returns - The best fit schema object from the `schema` given the `additionalItems` and `idx` modifiers
*/
export function getInnerSchemaForArrayItem<
S extends StrictRJSFSchema = RJSFSchema
>(
schema: S,
additionalItems: AdditionalItemsHandling = AdditionalItemsHandling.Ignore,
idx = -1
): S {
if (idx >= 0) {
if (Array.isArray(schema.items) && idx < schema.items.length) {
const item = schema.items[idx];
if (typeof item !== "boolean") {
return item as S;
}
}
} else if (
schema.items &&
!Array.isArray(schema.items) &&
typeof schema.items !== "boolean"
) {
return schema.items as S;
}
if (
additionalItems !== AdditionalItemsHandling.Ignore &&
isObject(schema.additionalItems)
) {
return schema.additionalItems as S;
}
return {} as S;
}
/** Computes the defaults for the current `schema` given the `rawFormData` and `parentDefaults` if any. This drills into
* each level of the schema, recursively, to fill out every level of defaults provided by the schema.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param schema - The schema for which the default state is desired
* @param [parentDefaults] - Any defaults provided by the parent field in the schema
* @param [rootSchema] - The options root schema, used to primarily to look up `$ref`s
* @param [rawFormData] - The current formData, if any, onto which to provide any missing defaults
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
export function computeDefaults<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
rawSchema: S,
parentDefaults?: T,
rootSchema: S = {} as S,
rawFormData?: T,
includeUndefinedValues: boolean | "excludeObjectChildren" = false
): T | T[] | undefined {
const formData = isObject(rawFormData) ? rawFormData : {};
let schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
// Compute the defaults recursively: give highest priority to deepest nodes.
let defaults: T | T[] | undefined = parentDefaults;
if (isObject(defaults) && isObject(schema.default)) {
// For object defaults, only override parent defaults that are defined in
// schema.default.
defaults = mergeObjects(
defaults!,
schema.default as GenericObjectType
) as T;
} else if (DEFAULT_KEY in schema) {
defaults = schema.default as unknown as T;
} else if (REF_KEY in schema) {
// Use referenced schema defaults for this node.
const refSchema = findSchemaDefinition<S>(schema[REF_KEY]!, rootSchema);
return computeDefaults<T, S>(
validator,
refSchema,
defaults,
rootSchema,
formData as T,
includeUndefinedValues
);
} else if (DEPENDENCIES_KEY in schema) {
const resolvedSchema = resolveDependencies(
validator,
schema,
rootSchema,
formData
);
return computeDefaults<T, S>(
validator,
resolvedSchema,
defaults,
rootSchema,
formData as T,
includeUndefinedValues
);
} else if (isFixedItems(schema)) {
defaults = (schema.items! as S[]).map((itemSchema: S, idx: number) =>
computeDefaults<T, S>(
validator,
itemSchema,
Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined,
rootSchema,
formData as T,
includeUndefinedValues
)
) as T[];
} else if (ONE_OF_KEY in schema) {
schema = schema.oneOf![
getMatchingOption(
validator,
isEmpty(formData) ? undefined : formData,
schema.oneOf as S[],
rootSchema
)
] as S;
} else if (ANY_OF_KEY in schema) {
schema = schema.anyOf![
getMatchingOption(
validator,
isEmpty(formData) ? undefined : formData,
schema.anyOf as S[],
rootSchema
)
] as S;
}
// Not defaults defined for this node, fallback to generic typed ones.
if (typeof defaults === "undefined") {
defaults = schema.default as unknown as T;
}
switch (getSchemaType(schema)) {
// We need to recur for object schema inner default values.
case "object":
return Object.keys(schema.properties || {}).reduce(
(acc: GenericObjectType, key: string) => {
// Compute the defaults for this node, with the parent defaults we might
// have from a previous run: defaults[key].
const computedDefault = computeDefaults<T, S>(
validator,
get(schema, [PROPERTIES_KEY, key]),
get(defaults, [key]),
rootSchema,
get(formData, [key]),
includeUndefinedValues === "excludeObjectChildren"
? false
: includeUndefinedValues
);
if (includeUndefinedValues) {
acc[key] = computedDefault;
} else if (isObject(computedDefault)) {
// Store computedDefault if it's a non-empty object (e.g. not {})
if (!isEmpty(computedDefault)) {
acc[key] = computedDefault;
}
} else if (computedDefault !== undefined) {
// Store computedDefault if it's a defined primitive (e.g. true)
acc[key] = computedDefault;
}
return acc;
},
{}
) as T;
case "array":
// Inject defaults into existing array defaults
if (Array.isArray(defaults)) {
defaults = defaults.map((item, idx) => {
const schemaItem: S = getInnerSchemaForArrayItem<S>(
schema,
AdditionalItemsHandling.Fallback,
idx
);
return computeDefaults<T, S>(validator, schemaItem, item, rootSchema);
}) as T[];
}
// Deeply inject defaults into already existing form data
if (Array.isArray(rawFormData)) {
const schemaItem: S = getInnerSchemaForArrayItem<S>(schema);
defaults = rawFormData.map((item: T, idx: number) => {
return computeDefaults<T, S>(
validator,
schemaItem,
get(defaults, [idx]),
rootSchema,
item
);
}) as T[];
}
if (schema.minItems) {
if (!isMultiSelect<T>(validator, schema, rootSchema)) {
const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
if (schema.minItems > defaultsLength) {
const defaultEntries: T[] = (defaults || []) as T[];
// populate the array with the defaults
const fillerSchema: S = getInnerSchemaForArrayItem<S>(
schema,
AdditionalItemsHandling.Invert
);
const fillerDefault = fillerSchema.default;
const fillerEntries: T[] = new Array(
schema.minItems - defaultsLength
).fill(
computeDefaults<any>(
validator,
fillerSchema,
fillerDefault,
rootSchema
)
) as T[];
// then fill up the rest with either the item default or empty, up to minItems
return defaultEntries.concat(fillerEntries);
}
}
return defaults ? defaults : [];
}
}
return defaults;
}
/** Returns the superset of `formData` that includes the given set updated to include any missing fields that have
* computed to have defaults provided in the `schema`.
*
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
* @param theSchema - The schema for which the default state is desired
* @param [formData] - The current formData, if any, onto which to provide any missing defaults
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s
* @param [includeUndefinedValues=false] - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", pass `includeUndefinedValues` as false when computing defaults for any nested
* object properties.
* @returns - The resulting `formData` with all the defaults provided
*/
export default function getDefaultFormState<
T = any,
S extends StrictRJSFSchema = RJSFSchema
>(
validator: ValidatorType<T, S>,
theSchema: S,
formData?: T,
rootSchema?: S,
includeUndefinedValues: boolean | "excludeObjectChildren" = false
) {
if (!isObject(theSchema)) {
throw new Error("Invalid schema: " + theSchema);
}
const schema = retrieveSchema<T, S>(
validator,
theSchema,
rootSchema,
formData
);
const defaults = computeDefaults<T, S>(
validator,
schema,
undefined,
rootSchema,
formData,
includeUndefinedValues
);
if (
typeof formData === "undefined" ||
formData === null ||
(typeof formData === "number" && isNaN(formData))
) {
// No form data? Use schema defaults.
return defaults;
}
if (isObject(formData)) {
return mergeDefaultsWithFormData<T>(defaults as T, formData);
}
if (Array.isArray(formData)) {
return mergeDefaultsWithFormData<T[]>(defaults as T[], formData);
}
return formData;
}