Skip to content

Commit f59ccd3

Browse files
committed
improvement based on feedback
1 parent 7c3f0d8 commit f59ccd3

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche
337337
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
338338
(acc: GenericObjectType, key: string) => {
339339
const propertySchema = get(retrievedSchema, [PROPERTIES_KEY, key]);
340-
// Check if the parent schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
340+
// Check if the parent schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
341341
const hasParentConst = isObject(parentConst) && (parentConst as JSONSchema7Object)[key] !== undefined;
342342
const hasConst = (isObject(propertySchema) && CONST_KEY in propertySchema) || hasParentConst;
343343
// Compute the defaults for this node, with the parent defaults we might
@@ -473,7 +473,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
473473
}
474474
}
475475

476-
// Check if the schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
476+
// Check if the schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
477477
const hasConst = isObject(schema) && CONST_KEY in schema;
478478
if (hasConst === false) {
479479
if (neverPopulate) {

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,62 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
2222
it('throws error when schema is not an object', () => {
2323
expect(() => getDefaultFormState(testValidator, null as unknown as RJSFSchema)).toThrowError('Invalid schema:');
2424
});
25+
it('test an object const value merge with formData', () => {
26+
const schema: RJSFSchema = {
27+
type: 'object',
28+
properties: {
29+
localConst: {
30+
type: 'string',
31+
const: 'local',
32+
},
33+
RootConst: {
34+
type: 'object',
35+
properties: {
36+
attr1: {
37+
type: 'number',
38+
},
39+
attr2: {
40+
type: 'boolean',
41+
},
42+
},
43+
const: {
44+
attr1: 1,
45+
attr2: true,
46+
},
47+
},
48+
RootAndLocalConst: {
49+
type: 'string',
50+
const: 'FromLocal',
51+
},
52+
fromFormData: {
53+
type: 'string',
54+
},
55+
},
56+
const: {
57+
RootAndLocalConst: 'FromRoot',
58+
},
59+
};
60+
expect(
61+
getDefaultFormState(
62+
testValidator,
63+
schema,
64+
{
65+
fromFormData: 'fromFormData',
66+
},
67+
schema,
68+
false,
69+
{ emptyObjectFields: 'skipDefaults' }
70+
)
71+
).toEqual({
72+
localConst: 'local',
73+
RootConst: {
74+
attr1: 1,
75+
attr2: true,
76+
},
77+
RootAndLocalConst: 'FromLocal',
78+
fromFormData: 'fromFormData',
79+
});
80+
});
2581
it('getInnerSchemaForArrayItem() item of type boolean returns empty schema', () => {
2682
expect(getInnerSchemaForArrayItem({ items: [true] }, AdditionalItemsHandling.Ignore, 0)).toEqual({});
2783
});
@@ -48,7 +104,7 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
48104
foo: 42,
49105
});
50106
});
51-
it('test computeDefaults that is passed a schema with a cont property', () => {
107+
it('test computeDefaults that is passed a schema with a const property', () => {
52108
const schema: RJSFSchema = {
53109
type: 'object',
54110
properties: {
@@ -885,6 +941,9 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
885941
attr2: true,
886942
},
887943
},
944+
fromFormData: {
945+
type: 'string',
946+
},
888947
RootAndLocalConst: {
889948
type: 'string',
890949
const: 'FromLocal',
@@ -898,6 +957,9 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
898957
getObjectDefaults(testValidator, schema, {
899958
rootSchema: schema,
900959
experimental_defaultFormStateBehavior: { emptyObjectFields: 'skipDefaults' },
960+
rawFormData: {
961+
fromFormData: 'fromFormData',
962+
},
901963
})
902964
).toEqual({
903965
localConst: 'local',

0 commit comments

Comments
 (0)