Skip to content

Commit cecbe97

Browse files
committed
Override the formData with the const if the constAsDefaults is set to always
1 parent 3571dc9 commit cecbe97

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,13 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
331331
const { arrayMinItems = {} } = experimental_defaultFormStateBehavior || {};
332332
const { mergeExtraDefaults } = arrayMinItems;
333333

334-
const matchingFormData = ensureFormDataMatchingSchema(validator, schema, rootSchema, rawFormData);
334+
const matchingFormData = ensureFormDataMatchingSchema(
335+
validator,
336+
schema,
337+
rootSchema,
338+
rawFormData,
339+
experimental_defaultFormStateBehavior
340+
);
335341
if (!isObject(rawFormData)) {
336342
defaultsWithFormData = mergeDefaultsWithFormData<T>(
337343
defaultsWithFormData as T,
@@ -351,21 +357,34 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
351357
* @param schema - The schema for which the formData state is desired
352358
* @param rootSchema - The root schema, used to primarily to look up `$ref`s
353359
* @param formData - The current formData
360+
* @param experimental_defaultFormStateBehavior - Optional configuration object, if provided, allows users to override default form state behavior
354361
* @returns - valid formData that matches schema
355362
*/
356363
export function ensureFormDataMatchingSchema<
357364
T = any,
358365
S extends StrictRJSFSchema = RJSFSchema,
359366
F extends FormContextType = any
360-
>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, formData: T | undefined): T | T[] | undefined {
367+
>(
368+
validator: ValidatorType<T, S, F>,
369+
schema: S,
370+
rootSchema: S,
371+
formData: T | undefined,
372+
experimental_defaultFormStateBehavior?: Experimental_DefaultFormStateBehavior
373+
): T | T[] | undefined {
361374
const isSelectField = !isConstant(schema) && isSelect(validator, schema, rootSchema);
362375
let validFormData: T | T[] | undefined = formData;
363-
364376
if (isSelectField) {
365377
const getOptionsList = optionsList(schema);
366378
const isValid = getOptionsList?.some((option) => isEqual(option.value, formData));
367379
validFormData = isValid ? formData : undefined;
368380
}
381+
382+
// Override the formData with the const if the constAsDefaults is set to always
383+
const constTakesPrecedence = schema[CONST_KEY] && experimental_defaultFormStateBehavior?.constAsDefaults === 'always';
384+
if (constTakesPrecedence) {
385+
validFormData = schema.const as any;
386+
}
387+
369388
return validFormData;
370389
}
371390

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type ObjectDefaultExpectList = [
4848
IExpectType,
4949
IExpectType,
5050
IExpectType,
51+
IExpectType,
5152
IExpectType
5253
];
5354

@@ -620,6 +621,26 @@ const testObjectDefault = (testValidator: TestValidatorType, expectList: ObjectD
620621
},
621622
});
622623
});
624+
it('Test an object with invalid formData const and constAsDefault set to always', () => {
625+
schema = {
626+
type: 'object',
627+
properties: {
628+
stringField: {
629+
type: 'string',
630+
const: 'fromConst',
631+
},
632+
},
633+
};
634+
635+
validateBasedOnIndex(24, expectList, schema, {
636+
rawFormData: {
637+
stringField: 'fromFormData',
638+
},
639+
experimental_defaultFormStateBehavior: {
640+
constAsDefaults: 'always',
641+
},
642+
});
643+
});
623644
});
624645
};
625646

@@ -976,6 +997,20 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
976997
),
977998
toEqual: {},
978999
},
1000+
{
1001+
expectedCB: (schema, options) =>
1002+
getDefaultFormState(
1003+
testValidator,
1004+
schema,
1005+
options.rawFormData,
1006+
schema,
1007+
undefined,
1008+
options.experimental_defaultFormStateBehavior
1009+
),
1010+
toEqual: {
1011+
stringField: 'fromConst',
1012+
},
1013+
},
9791014
]);
9801015
// test array defaults
9811016
testArrayDefault(testValidator, [
@@ -1356,6 +1391,16 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
13561391
}),
13571392
toEqual: {},
13581393
},
1394+
{
1395+
expectedCB: (schema, options) =>
1396+
computeDefaults(testValidator, schema, {
1397+
rootSchema: schema,
1398+
...options,
1399+
}),
1400+
toEqual: {
1401+
stringField: 'fromConst',
1402+
},
1403+
},
13591404
]);
13601405
// test array defaults
13611406
testArrayDefault(testValidator, [
@@ -1663,6 +1708,16 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
16631708
}),
16641709
toEqual: {},
16651710
},
1711+
{
1712+
expectedCB: (schema, options) =>
1713+
getDefaultBasedOnSchemaType(testValidator, schema, {
1714+
rootSchema: schema,
1715+
...options,
1716+
}),
1717+
toEqual: {
1718+
stringField: 'fromConst',
1719+
},
1720+
},
16661721
]);
16671722
// test array defaults
16681723
testArrayDefault(testValidator, [
@@ -1985,6 +2040,16 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
19852040
}),
19862041
toEqual: {},
19872042
},
2043+
{
2044+
expectedCB: (schema, options) =>
2045+
getObjectDefaults(testValidator, schema, {
2046+
rootSchema: schema,
2047+
...options,
2048+
}),
2049+
toEqual: {
2050+
stringField: 'fromConst',
2051+
},
2052+
},
19882053
]);
19892054
});
19902055
describe('getArrayDefaults()', () => {

0 commit comments

Comments
 (0)