Skip to content

Commit bb5bc6e

Browse files
committed
created tests for the new created methods
1 parent add62e2 commit bb5bc6e

File tree

2 files changed

+764
-21
lines changed

2 files changed

+764
-21
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
207207
// Get the default if set from properties to ensure the dependencies conditions are resolved based on it
208208
const defaultFormData: T = {
209209
...formData,
210-
...getDefaultBasedOnSchemaType(validator, schema, defaults, computeDefaultsProps),
210+
...getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults),
211211
};
212212
const resolvedSchema = resolveDependencies<T, S, F>(validator, schema, rootSchema, false, [], defaultFormData);
213213
schemaToCompute = resolvedSchema[0]; // pick the first element from resolve dependencies
@@ -276,7 +276,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
276276
defaults = schema.default as unknown as T;
277277
}
278278

279-
const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType(validator, schema, defaults, computeDefaultsProps);
279+
const defaultBasedOnSchemaType = getDefaultBasedOnSchemaType(validator, schema, computeDefaultsProps, defaults);
280280

281281
return defaultBasedOnSchemaType ?? defaults;
282282
}
@@ -285,25 +285,27 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
285285
*
286286
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
287287
* @param rawSchema - The schema for which the default state is desired
288-
* @param defaults - Optional props for this function
289288
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
289+
* @param defaults - Optional props for this function
290290
* @returns - The default value based on the schema type if they are defined for object or array schemas.
291291
*/
292-
function getDefaultBasedOnSchemaType<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
292+
export function getDefaultBasedOnSchemaType<
293+
T = any,
294+
S extends StrictRJSFSchema = RJSFSchema,
295+
F extends FormContextType = any
296+
>(
293297
validator: ValidatorType<T, S, F>,
294298
rawSchema: S,
295-
defaults: T | T[] | undefined,
296-
computeDefaultsProps: ComputeDefaultsProps<T, S> = {}
299+
computeDefaultsProps: ComputeDefaultsProps<T, S> = {},
300+
defaults?: T | T[] | undefined
297301
): T | T[] | void {
298-
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
299-
300-
switch (getSchemaType<S>(schema)) {
302+
switch (getSchemaType<S>(rawSchema)) {
301303
// We need to recurse for object schema inner default values.
302304
case 'object': {
303-
return getObjectDefaults(validator, schema, defaults, computeDefaultsProps);
305+
return getObjectDefaults(validator, rawSchema, computeDefaultsProps, defaults);
304306
}
305307
case 'array': {
306-
return getArrayDefaults(validator, schema, defaults, computeDefaultsProps);
308+
return getArrayDefaults(validator, rawSchema, computeDefaultsProps, defaults);
307309
}
308310
}
309311
}
@@ -312,26 +314,26 @@ function getDefaultBasedOnSchemaType<T = any, S extends StrictRJSFSchema = RJSFS
312314
*
313315
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
314316
* @param rawSchema - The schema for which the default state is desired
315-
* @param defaults - Optional props for this function
316317
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
318+
* @param defaults - Optional props for this function
317319
* @returns - The default value based on the schema type if they are defined for object or array schemas.
318320
*/
319-
function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
321+
export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
320322
validator: ValidatorType<T, S, F>,
321323
rawSchema: S,
322-
defaults: T | T[] | undefined,
323324
{
324325
rawFormData,
325326
rootSchema = {} as S,
326327
includeUndefinedValues = false,
327328
_recurseList = [],
328329
experimental_defaultFormStateBehavior = undefined,
329330
required,
330-
}: ComputeDefaultsProps<T, S> = {}
331+
}: ComputeDefaultsProps<T, S> = {},
332+
defaults?: T | T[] | undefined
331333
): T {
332334
{
333335
const formData: T = (isObject(rawFormData) ? rawFormData : {}) as T;
334-
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
336+
const schema: S = rawSchema;
335337
// This is a custom addition that fixes this issue:
336338
// https://github.com/rjsf-team/react-jsonschema-form/issues/3832
337339
const retrievedSchema =
@@ -412,23 +414,23 @@ function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F e
412414
*
413415
* @param validator - an implementation of the `ValidatorType` interface that will be used when necessary
414416
* @param rawSchema - The schema for which the default state is desired
415-
* @param defaults - Optional props for this function
416417
* @param {ComputeDefaultsProps} computeDefaultsProps - Optional props for this function
418+
* @param defaults - Optional props for this function
417419
* @returns - The default value based on the schema type if they are defined for object or array schemas.
418420
*/
419-
function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
421+
export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(
420422
validator: ValidatorType<T, S, F>,
421423
rawSchema: S,
422-
defaults: T | T[] | undefined,
423424
{
424425
rawFormData,
425426
rootSchema = {} as S,
426427
_recurseList = [],
427428
experimental_defaultFormStateBehavior = undefined,
428429
required,
429-
}: ComputeDefaultsProps<T, S> = {}
430+
}: ComputeDefaultsProps<T, S> = {},
431+
defaults?: T | T[] | undefined
430432
): T | T[] | undefined {
431-
const schema: S = isObject(rawSchema) ? rawSchema : ({} as S);
433+
const schema: S = rawSchema;
432434

433435
const neverPopulate = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === 'never';
434436
const ignoreMinItemsFlagSet = experimental_defaultFormStateBehavior?.arrayMinItems?.populate === 'requiredOnly';

0 commit comments

Comments
 (0)