-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathgetDefaultFormState.ts
More file actions
758 lines (720 loc) · 33.1 KB
/
getDefaultFormState.ts
File metadata and controls
758 lines (720 loc) · 33.1 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
import get from 'lodash/get';
import isEmpty from 'lodash/isEmpty';
import { JSONSchema7Object } from 'json-schema';
import {
ALL_OF_KEY,
ANY_OF_KEY,
CONST_KEY,
DEFAULT_KEY,
DEPENDENCIES_KEY,
ONE_OF_KEY,
PROPERTIES_KEY,
REF_KEY,
} from '../constants';
import findSchemaDefinition from '../findSchemaDefinition';
import getClosestMatchingOption from './getClosestMatchingOption';
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema';
import getSchemaType from '../getSchemaType';
import isObject from '../isObject';
import isFixedItems from '../isFixedItems';
import mergeDefaultsWithFormData from '../mergeDefaultsWithFormData';
import mergeObjects from '../mergeObjects';
import mergeSchemas from '../mergeSchemas';
import {
Experimental_CustomMergeAllOf,
Experimental_DefaultFormStateBehavior,
FormContextType,
GenericObjectType,
OverrideFormDataStrategy,
RJSFSchema,
StrictRJSFSchema,
ValidatorType,
} from '../types';
import isMultiSelect from './isMultiSelect';
import isSelect from './isSelect';
import retrieveSchema, { resolveDependencies } from './retrieveSchema';
import isConstant from '../isConstant';
import constIsAjvDataReference from '../constIsAjvDataReference';
import optionsList from '../optionsList';
import deepEquals from '../deepEquals';
const PRIMITIVE_TYPES = ['string', 'number', 'integer', 'boolean', 'null'];
/** 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;
}
/** Either add `computedDefault` at `key` into `obj` or not add it based on its value, the value of
* `includeUndefinedValues`, the value of `emptyObjectFields` and if its parent field is required. Generally undefined
* `computedDefault` values are added only when `includeUndefinedValues` is either true/"excludeObjectChildren". If `
* includeUndefinedValues` is false and `emptyObjectFields` is not "skipDefaults", then non-undefined and non-empty-object
* values will be added based on certain conditions.
*
* @param obj - The object into which the computed default may be added
* @param key - The key into the object at which the computed default may be added
* @param computedDefault - The computed default value that maybe should be added to the obj
* @param includeUndefinedValues - Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
* false when computing defaults for any nested object properties. If "allowEmptyObject", prevents undefined
* values in this object while allow the object itself to be empty and passing `includeUndefinedValues` as
* false when computing defaults for any nested object properties.
* @param isParentRequired - The optional boolean that indicates whether the parent field is required
* @param requiredFields - The list of fields that are required
* @param experimental_defaultFormStateBehavior - Optional configuration object, if provided, allows users to override
* default form state behavior
* @param isConst - Optional flag, if true, indicates that the schema has a const property defined, thus we should always return the computedDefault since it's coming from the const.
*/
function maybeAddDefaultToObject<T = any>(
obj: GenericObjectType,
key: string,
computedDefault: T | T[] | undefined,
includeUndefinedValues: boolean | 'excludeObjectChildren',
isParentRequired?: boolean,
requiredFields: string[] = [],
experimental_defaultFormStateBehavior: Experimental_DefaultFormStateBehavior = {},
isConst = false,
) {
const { emptyObjectFields = 'populateAllDefaults' } = experimental_defaultFormStateBehavior;
if (includeUndefinedValues || isConst) {
// If includeUndefinedValues
// Or if the schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
obj[key] = computedDefault;
} else if (emptyObjectFields !== 'skipDefaults') {
// If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
// the field key itself in the `requiredField` list
const isSelfOrParentRequired = isParentRequired === undefined ? requiredFields.includes(key) : isParentRequired;
if (isObject(computedDefault)) {
// If emptyObjectFields 'skipEmptyDefaults' store computedDefault if it's a non-empty object(e.g. not {})
if (emptyObjectFields === 'skipEmptyDefaults') {
if (!isEmpty(computedDefault)) {
obj[key] = computedDefault;
}
} // Else store computedDefault if it's a non-empty object(e.g. not {}) and satisfies certain conditions
// Condition 1: If computedDefault is not empty or if the key is a required field
// Condition 2: If the parent object is required or emptyObjectFields is not 'populateRequiredDefaults'
else if (
(!isEmpty(computedDefault) || requiredFields.includes(key)) &&
(isSelfOrParentRequired || emptyObjectFields !== 'populateRequiredDefaults')
) {
obj[key] = computedDefault;
}
} else if (
// Store computedDefault if it's a defined primitive (e.g., true) and satisfies certain conditions
// Condition 1: computedDefault is not undefined
// Condition 2: If emptyObjectFields is 'populateAllDefaults' or 'skipEmptyDefaults)
// Or if isSelfOrParentRequired is 'true' and the key is a required field
computedDefault !== undefined &&
(emptyObjectFields === 'populateAllDefaults' ||
emptyObjectFields === 'skipEmptyDefaults' ||
(isSelfOrParentRequired && requiredFields.includes(key)))
) {
obj[key] = computedDefault;
}
}
}
interface ComputeDefaultsProps<T = any, S extends StrictRJSFSchema = RJSFSchema> {
/** Any defaults provided by the parent field in the schema */
parentDefaults?: T;
/** The options root schema, used to primarily to look up `$ref`s */
rootSchema?: S;
/** The current formData, if any, onto which to provide any missing defaults */
rawFormData?: T;
/** Optional flag, if true, cause undefined values to be added as defaults.
* If "excludeObjectChildren", cause undefined values for this object and pass `includeUndefinedValues` as
* false when computing defaults for any nested object properties.
*/
includeUndefinedValues?: boolean | 'excludeObjectChildren';
/** The list of ref names currently being recursed, used to prevent infinite recursion */
_recurseList?: string[];
/** Optional configuration object, if provided, allows users to override default form state behavior */
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior;
/** Optional function that allows for custom merging of `allOf` schemas */
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>;
/** Optional flag, if true, indicates this schema was required in the parent schema. */
required?: boolean;
/** Optional flag, if true, It will merge defaults into formData.
* The formData should take precedence unless it's not valid. This is useful when for example the value from formData does not exist in the schema 'enum' property, in such cases we take the value from the defaults because the value from the formData is not valid.
*/
shouldMergeDefaultsIntoFormData?: boolean;
}
/** 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 rawSchema - The schema for which the default state is desired
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
* @returns - The resulting `formData` with all the defaults provided
*/
export 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 {
const {
parentDefaults,
rawFormData,
rootSchema = {} as S,
includeUndefinedValues = false,
_recurseList = [],
experimental_defaultFormStateBehavior = undefined,
experimental_customMergeAllOf = undefined,
required,
shouldMergeDefaultsIntoFormData = false,
} = computeDefaultsProps;
let formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
// Compute the defaults recursively: give highest priority to deepest nodes.
let defaults: T | T[] | undefined = parentDefaults;
// If we get a new schema, then we need to recompute defaults again for the new schema found.
let schemaToCompute: S | null = null;
let experimental_dfsb_to_compute = experimental_defaultFormStateBehavior;
let updatedRecurseList = _recurseList;
if (
schema[CONST_KEY] !== undefined &&
experimental_defaultFormStateBehavior?.constAsDefaults !== 'never' &&
!constIsAjvDataReference(schema)
) {
defaults = schema[CONST_KEY] as unknown as T;
} else 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 && !schema[ANY_OF_KEY] && !schema[ONE_OF_KEY] && !schema[REF_KEY]) {
// If the schema has a default value, then we should use it as the default.
// And if the schema does not have anyOf or oneOf, this is done because we need to merge the defaults with the formData.
defaults = schema.default as unknown as T;
} else if (REF_KEY in schema) {
const refName = schema[REF_KEY];
// Use referenced schema defaults for this node.
if (!_recurseList.includes(refName!)) {
updatedRecurseList = _recurseList.concat(refName!);
schemaToCompute = findSchemaDefinition<S>(refName, rootSchema);
}
// If the referenced schema exists and parentDefaults is not set
// Then set the defaults from the current schema for the referenced schema
if (schemaToCompute && !defaults) {
defaults = schema.default as T | undefined;
}
// If shouldMergeDefaultsIntoFormData is true
// And the schemaToCompute is set and the rawFormData is not an object
// Then set the formData to the rawFormData
if (shouldMergeDefaultsIntoFormData && schemaToCompute && !isObject(rawFormData)) {
formData = rawFormData as T;
}
} else if (DEPENDENCIES_KEY in schema) {
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
const defaultFormData: T = {
...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults),
...formData,
};
const resolvedSchema = resolveDependencies<T, S, F>(
validator,
schema,
rootSchema,
false,
[],
defaultFormData,
experimental_customMergeAllOf,
);
schemaToCompute = resolvedSchema[0]; // pick the first element from resolve dependencies
} else if (isFixedItems(schema)) {
defaults = (schema.items! as S[]).map((itemSchema: S, idx: number) =>
computeDefaults<T, S>(validator, itemSchema, {
rootSchema,
includeUndefinedValues,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
parentDefaults: Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined,
rawFormData: formData as T,
required,
shouldMergeDefaultsIntoFormData,
}),
) as T[];
} else if (ONE_OF_KEY in schema) {
const { oneOf, ...remaining } = schema;
if (oneOf!.length === 0) {
return undefined;
}
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
const { type = 'null' } = remaining;
if (
!Array.isArray(type) &&
PRIMITIVE_TYPES.includes(type) &&
experimental_dfsb_to_compute?.constAsDefaults === 'skipOneOf'
) {
// If we are in a oneOf of a primitive type, then we want to pass constAsDefaults as 'never' for the recursion
experimental_dfsb_to_compute = {
...experimental_dfsb_to_compute,
constAsDefaults: 'never',
};
}
schemaToCompute = oneOf![
getClosestMatchingOption<T, S, F>(
validator,
rootSchema,
rawFormData ?? (schema.default as T),
oneOf as S[],
0,
discriminator,
experimental_customMergeAllOf,
)
] as S;
schemaToCompute = mergeSchemas(remaining, schemaToCompute) as S;
} else if (ANY_OF_KEY in schema) {
const { anyOf, ...remaining } = schema;
if (anyOf!.length === 0) {
return undefined;
}
const discriminator = getDiscriminatorFieldFromSchema<S>(schema);
schemaToCompute = anyOf![
getClosestMatchingOption<T, S, F>(
validator,
rootSchema,
rawFormData ?? (schema.default as T),
anyOf as S[],
0,
discriminator,
experimental_customMergeAllOf,
)
] as S;
schemaToCompute = mergeSchemas(remaining, schemaToCompute) as S;
}
if (schemaToCompute) {
return computeDefaults<T, S, F>(validator, schemaToCompute, {
rootSchema,
includeUndefinedValues,
_recurseList: updatedRecurseList,
experimental_defaultFormStateBehavior: experimental_dfsb_to_compute,
experimental_customMergeAllOf,
parentDefaults: defaults as T | undefined,
rawFormData: formData as T,
required,
shouldMergeDefaultsIntoFormData,
});
}
// No defaults defined for this node, fallback to generic typed ones.
if (defaults === undefined) {
defaults = schema.default as unknown as T;
}
const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults);
let defaultsWithFormData = defaultBasedOnSchemaType ?? defaults;
// if shouldMergeDefaultsIntoFormData is true, then merge the defaults into the formData.
if (shouldMergeDefaultsIntoFormData) {
const { arrayMinItems = {} } = experimental_defaultFormStateBehavior || {};
const { mergeExtraDefaults } = arrayMinItems;
const matchingFormData = ensureFormDataMatchingSchema(
validator,
schema,
rootSchema,
rawFormData,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
);
if (!isObject(rawFormData) || ALL_OF_KEY in schema) {
// If the formData is not an object which means it's a primitive field, then we need to merge the defaults into the formData.
// Or if the schema has allOf, we need to merge the defaults into the formData because we don't compute the defaults for allOf.
defaultsWithFormData = mergeDefaultsWithFormData<T>(
defaultsWithFormData as T,
matchingFormData as T,
mergeExtraDefaults,
true,
experimental_defaultFormStateBehavior?.mergeDefaultsIntoFormData === 'useDefaultAlways'
? OverrideFormDataStrategy.replace
: OverrideFormDataStrategy.noop,
) as T;
}
}
return defaultsWithFormData;
}
/**
* Ensure that the formData matches the given schema. If it's not matching in the case of a selectField, we change it to match the schema.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param schema - The schema for which the formData state is desired
* @param rootSchema - The root schema, used to primarily to look up `$ref`s
* @param formData - The current formData
* @param [experimental_defaultFormStateBehavior] - Optional configuration object, if provided, allows users to override default form state behavior
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - valid formData that matches schema
*/
export function ensureFormDataMatchingSchema<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
>(
validator: ValidatorType<T, S, F>,
schema: S,
rootSchema: S,
formData: T | undefined,
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior,
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
): T | T[] | undefined {
const isSelectField =
!isConstant<S>(schema) && isSelect<T, S, F>(validator, schema, rootSchema, experimental_customMergeAllOf);
let validFormData: T | T[] | undefined = formData;
if (isSelectField) {
const getOptionsList = optionsList<T, S, F>(schema);
const isValid = getOptionsList?.some((option) => deepEquals(option.value, formData));
validFormData = isValid ? formData : undefined;
}
// Override the formData with the const if the constAsDefaults is set to always
const constTakesPrecedence = schema[CONST_KEY] && experimental_defaultFormStateBehavior?.constAsDefaults === 'always';
if (constTakesPrecedence) {
validFormData = schema.const as T;
}
return validFormData;
}
/** Computes the default value for objects.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param rawSchema - The schema for which the default state is desired
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
* @param defaults - Optional props for this function
* @returns - The default value based on the schema type if they are defined for object or array schemas.
*/
export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
rawSchema: S,
{
rawFormData,
rootSchema = {} as S,
includeUndefinedValues = false,
_recurseList = [],
experimental_defaultFormStateBehavior = undefined,
experimental_customMergeAllOf = undefined,
required,
shouldMergeDefaultsIntoFormData,
}: ComputeDefaultsProps<T, S> = {},
defaults?: T | T[] | undefined,
): T {
{
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
const schema: S = rawSchema;
// This is a custom addition that fixes this issue:
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
const retrievedSchema =
experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData, experimental_customMergeAllOf)
: schema;
const parentConst = retrievedSchema[CONST_KEY];
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
(acc: GenericObjectType, key: string) => {
const propertySchema: S = get(retrievedSchema, [PROPERTIES_KEY, key], {}) as S;
// Check if the parent schema has a const property defined AND we are supporting const as defaults, then we
// should always return the computedDefault since it's coming from the const.
const hasParentConst = isObject(parentConst) && (parentConst as JSONSchema7Object)[key] !== undefined;
const hasConst =
((isObject(propertySchema) && CONST_KEY in propertySchema) || hasParentConst) &&
experimental_defaultFormStateBehavior?.constAsDefaults !== 'never' &&
!constIsAjvDataReference(propertySchema);
// Compute the defaults for this node, with the parent defaults we might
// have from a previous run: defaults[key].
const computedDefault = computeDefaults<T, S, F>(validator, propertySchema, {
rootSchema,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
includeUndefinedValues: includeUndefinedValues === true,
parentDefaults: get(defaults, [key]),
rawFormData: get(formData, [key]),
required: retrievedSchema.required?.includes(key),
shouldMergeDefaultsIntoFormData,
});
maybeAddDefaultToObject<T>(
acc,
key,
computedDefault,
includeUndefinedValues,
required,
retrievedSchema.required,
experimental_defaultFormStateBehavior,
hasConst,
);
return acc;
},
{},
) as T;
if (retrievedSchema.additionalProperties) {
// as per spec additionalProperties may be either schema or boolean
const additionalPropertiesSchema = isObject(retrievedSchema.additionalProperties)
? retrievedSchema.additionalProperties
: {};
const keys = new Set<string>();
if (isObject(defaults)) {
Object.keys(defaults as GenericObjectType)
.filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key])
.forEach((key) => keys.add(key));
}
const formDataRequired: string[] = [];
Object.keys(formData as GenericObjectType)
.filter((key) => !retrievedSchema.properties || !retrievedSchema.properties[key])
.forEach((key) => {
keys.add(key);
formDataRequired.push(key);
});
keys.forEach((key) => {
const computedDefault = computeDefaults(validator, additionalPropertiesSchema as S, {
rootSchema,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
includeUndefinedValues: includeUndefinedValues === true,
parentDefaults: get(defaults, [key]),
rawFormData: get(formData, [key]),
required: retrievedSchema.required?.includes(key),
shouldMergeDefaultsIntoFormData,
});
// Since these are additional properties we don't need to add the `experimental_defaultFormStateBehavior` prop
maybeAddDefaultToObject<T>(
objectDefaults as GenericObjectType,
key,
computedDefault,
includeUndefinedValues,
required,
formDataRequired,
);
});
}
return objectDefaults;
}
}
/** Computes the default value for arrays.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param rawSchema - The schema for which the default state is desired
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
* @param defaults - Optional props for this function
* @returns - The default value based on the schema type if they are defined for object or array schemas.
*/
export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
validator: ValidatorType<T, S, F>,
rawSchema: S,
{
rawFormData,
rootSchema = {} as S,
_recurseList = [],
experimental_defaultFormStateBehavior = undefined,
experimental_customMergeAllOf = undefined,
required,
shouldMergeDefaultsIntoFormData,
}: ComputeDefaultsProps<T, S> = {},
defaults?: T | T[] | undefined,
): T | T[] | undefined {
const schema: S = rawSchema;
const arrayMinItemsStateBehavior = experimental_defaultFormStateBehavior?.arrayMinItems ?? {};
const { populate: arrayMinItemsPopulate, mergeExtraDefaults: arrayMergeExtraDefaults } = arrayMinItemsStateBehavior;
const neverPopulate = arrayMinItemsPopulate === 'never';
const ignoreMinItemsFlagSet = arrayMinItemsPopulate === 'requiredOnly';
const isPopulateAll = arrayMinItemsPopulate === 'all' || (!neverPopulate && !ignoreMinItemsFlagSet);
const computeSkipPopulate = arrayMinItemsStateBehavior?.computeSkipPopulate ?? (() => false);
const isSkipEmptyDefaults = experimental_defaultFormStateBehavior?.emptyObjectFields === 'skipEmptyDefaults';
const emptyDefault = isSkipEmptyDefaults ? undefined : [];
// 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, F>(validator, schemaItem, {
rootSchema,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
parentDefaults: item,
required,
shouldMergeDefaultsIntoFormData,
});
}) as T[];
}
// Deeply inject defaults into already existing form data
if (Array.isArray(rawFormData)) {
const schemaItem: S = getInnerSchemaForArrayItem<S>(schema);
if (neverPopulate) {
defaults = rawFormData;
} else {
const itemDefaults = rawFormData.map((item: T, idx: number) => {
return computeDefaults<T, S, F>(validator, schemaItem, {
rootSchema,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
rawFormData: item,
parentDefaults: get(defaults, [idx]),
required,
shouldMergeDefaultsIntoFormData,
});
}) as T[];
// If the populate 'requiredOnly' flag is set then we only merge and include extra defaults if they are required.
// Or if populate 'all' is set we merge and include extra defaults.
const mergeExtraDefaults = ((ignoreMinItemsFlagSet && required) || isPopulateAll) && arrayMergeExtraDefaults;
defaults = mergeDefaultsWithFormData(defaults, itemDefaults, mergeExtraDefaults);
}
}
// Check if the schema has a const property defined AND we are supporting const as defaults, then we should always
// return the computedDefault since it's coming from the const.
const hasConst =
isObject(schema) && CONST_KEY in schema && experimental_defaultFormStateBehavior?.constAsDefaults !== 'never';
if (hasConst === false) {
if (neverPopulate) {
return defaults ?? emptyDefault;
}
if (ignoreMinItemsFlagSet && !required) {
// If no form data exists or defaults are set leave the field empty/non-existent, otherwise
// return form data/defaults
return defaults ? defaults : undefined;
}
}
const defaultsLength = Array.isArray(defaults) ? defaults.length : 0;
if (
!schema.minItems ||
isMultiSelect<T, S, F>(validator, schema, rootSchema, experimental_customMergeAllOf) ||
computeSkipPopulate<T, S, F>(validator, schema, rootSchema) ||
schema.minItems <= defaultsLength
) {
return defaults ? defaults : emptyDefault;
}
const defaultEntries: T[] = (defaults || []) as T[];
const fillerSchema: S = getInnerSchemaForArrayItem<S>(schema, AdditionalItemsHandling.Invert);
const fillerDefault = fillerSchema.default;
// Calculate filler entries for remaining items (minItems - existing raw data/defaults)
const fillerEntries: T[] = new Array(schema.minItems - defaultsLength).fill(
computeDefaults<any, S, F>(validator, fillerSchema, {
parentDefaults: fillerDefault,
rootSchema,
_recurseList,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
required,
shouldMergeDefaultsIntoFormData,
}),
) as T[];
// then fill up the rest with either the item default or empty, up to minItems
return defaultEntries.concat(fillerEntries);
}
/** Computes the default value based on the schema type.
*
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
* @param rawSchema - The schema for which the default state is desired
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
* @param defaults - Optional props for this function
* @returns - The default value based on the schema type if they are defined for object or array schemas.
*/
export 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 {
switch (getSchemaType<S>(rawSchema)) {
// We need to recurse for object schema inner default values.
case 'object': {
return getObjectDefaults(validator, rawSchema, computeDefaultsProps, defaults);
}
case 'array': {
return getArrayDefaults(validator, rawSchema, computeDefaultsProps, 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", cause undefined values for this object and pass `includeUndefinedValues` as
* false when computing defaults for any nested object properties.
* @param [experimental_defaultFormStateBehavior] Optional configuration object, if provided, allows users to override default form state behavior
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The resulting `formData` with all the defaults provided
*/
export default 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' = false,
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior,
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
) {
if (!isObject(theSchema)) {
throw new Error('Invalid schema: ' + theSchema);
}
const schema = retrieveSchema<T, S, F>(validator, theSchema, rootSchema, formData, experimental_customMergeAllOf);
// Get the computed defaults with 'shouldMergeDefaultsIntoFormData' set to true to merge defaults into formData.
// This is done when for example the value from formData does not exist in the schema 'enum' property, in such
// cases we take the value from the defaults because the value from the formData is not valid.
const defaults = computeDefaults<T, S, F>(validator, schema, {
rootSchema,
includeUndefinedValues,
experimental_defaultFormStateBehavior,
experimental_customMergeAllOf,
rawFormData: formData,
shouldMergeDefaultsIntoFormData: true,
});
if (schema.type !== 'object' && isObject(schema.default)) {
return {
...defaults,
...formData,
} as T;
}
// If the formData is an object or an array, add additional properties from formData and override formData with
// defaults since the defaults are already merged with formData.
if (isObject(formData) || Array.isArray(formData)) {
const { mergeDefaultsIntoFormData } = experimental_defaultFormStateBehavior || {};
const defaultSupercedesUndefined = mergeDefaultsIntoFormData === 'useDefaultIfFormDataUndefined';
const result = mergeDefaultsWithFormData<T | T[]>(
defaults,
formData,
true, // set to true to add any additional default array entries.
defaultSupercedesUndefined,
OverrideFormDataStrategy.merge, // set to 'merge' to override formData with defaults if they exist.
);
return result;
}
return defaults;
}