Skip to content

Commit b25315f

Browse files
committed
fixed array issue and written tests to cover the behavior
1 parent 3f295ab commit b25315f

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export function getArrayDefaults<T = any, S extends StrictRJSFSchema = RJSFSchem
461461

462462
// Check if the schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
463463
const hasConst = isObject(schema) && CONST_KEY in schema;
464-
if (hasConst) {
464+
if (hasConst === false) {
465465
if (neverPopulate) {
466466
return defaults ?? emptyDefault;
467467
}

packages/utils/test/schema/getDefaultFormStateTest.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
4848
foo: 42,
4949
});
5050
});
51+
it('test computeDefaults that is passed a schema with a cont property', () => {
52+
const schema: RJSFSchema = {
53+
type: 'object',
54+
properties: {
55+
test: {
56+
type: 'string',
57+
const: 'test',
58+
},
59+
},
60+
};
61+
expect(computeDefaults(testValidator, schema, { rootSchema: schema })).toEqual({
62+
test: 'test',
63+
});
64+
});
5165
it('test an object with an optional property that has a nested required property', () => {
5266
const schema: RJSFSchema = {
5367
type: 'object',
@@ -848,6 +862,52 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
848862
requiredProperty: 'foo',
849863
});
850864
});
865+
it('test an object const value populate as field defaults', () => {
866+
const schema: RJSFSchema = {
867+
type: 'object',
868+
properties: {
869+
localConst: {
870+
type: 'string',
871+
const: 'local',
872+
},
873+
RootConst: {
874+
type: 'object',
875+
properties: {
876+
attr1: {
877+
type: 'number',
878+
},
879+
attr2: {
880+
type: 'boolean',
881+
},
882+
},
883+
const: {
884+
attr1: 1,
885+
attr2: true,
886+
},
887+
},
888+
RootAndLocalConst: {
889+
type: 'string',
890+
const: 'FromLocal',
891+
},
892+
},
893+
const: {
894+
RootAndLocalConst: 'FromRoot',
895+
},
896+
};
897+
expect(
898+
getObjectDefaults(testValidator, schema, {
899+
rootSchema: schema,
900+
experimental_defaultFormStateBehavior: { emptyObjectFields: 'skipDefaults' },
901+
})
902+
).toEqual({
903+
localConst: 'local',
904+
RootConst: {
905+
attr1: 1,
906+
attr2: true,
907+
},
908+
RootAndLocalConst: 'FromLocal',
909+
});
910+
});
851911
it('test an object with an additionalProperties', () => {
852912
const schema: RJSFSchema = {
853913
type: 'object',
@@ -1065,6 +1125,29 @@ export default function getDefaultFormStateTest(testValidator: TestValidatorType
10651125
)
10661126
).toEqual(['Raphael', 'Michaelangelo', 'Unknown', 'Unknown']);
10671127
});
1128+
it('test an array const value populate as defaults', () => {
1129+
const schema: RJSFSchema = {
1130+
type: 'array',
1131+
minItems: 4,
1132+
const: ['ConstFromRoot', 'ConstFromRoot'],
1133+
items: {
1134+
type: 'string',
1135+
const: 'Constant',
1136+
},
1137+
};
1138+
1139+
expect(
1140+
getArrayDefaults(
1141+
testValidator,
1142+
schema,
1143+
{
1144+
rootSchema: schema,
1145+
includeUndefinedValues: 'excludeObjectChildren',
1146+
},
1147+
['ConstFromRoot', 'ConstFromRoot']
1148+
)
1149+
).toEqual(['ConstFromRoot', 'ConstFromRoot', 'Constant', 'Constant']);
1150+
});
10681151
it('test an array with no defaults', () => {
10691152
const schema: RJSFSchema = {
10701153
type: 'array',

0 commit comments

Comments
 (0)