Skip to content

Commit 3aff670

Browse files
committed
fixed issue with default on root level.
1 parent 36011f6 commit 3aff670

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

packages/utils/src/schema/getDefaultFormState.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import isMultiSelect from './isMultiSelect';
3232
import retrieveSchema, { resolveDependencies } from './retrieveSchema';
3333
import isConstant from '../isConstant';
34+
import { JSONSchema7Object } from 'json-schema';
3435

3536
/** Enum that indicates how `schema.additionalItems` should be handled by the `getInnerSchemaForArrayItem()` function.
3637
*/
@@ -319,10 +320,13 @@ export function getObjectDefaults<T = any, S extends StrictRJSFSchema = RJSFSche
319320
experimental_defaultFormStateBehavior?.allOf === 'populateDefaults' && ALL_OF_KEY in schema
320321
? retrieveSchema<T, S, F>(validator, schema, rootSchema, formData)
321322
: schema;
323+
const parentConst = retrievedSchema[CONST_KEY];
322324
const objectDefaults = Object.keys(retrievedSchema.properties || {}).reduce(
323325
(acc: GenericObjectType, key: string) => {
324326
const propertySchema = get(retrievedSchema, [PROPERTIES_KEY, key]);
325-
const hasConst = isObject(propertySchema) && CONST_KEY in propertySchema;
327+
// Check if the parent schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
328+
const hasParentConst = isObject(parentConst) && (parentConst as JSONSchema7Object)[key] !== undefined;
329+
const hasConst = (isObject(propertySchema) && CONST_KEY in propertySchema) || hasParentConst;
326330
// Compute the defaults for this node, with the parent defaults we might
327331
// have from a previous run: defaults[key].
328332
const computedDefault = computeDefaults<T, S, F>(validator, propertySchema, {

0 commit comments

Comments
 (0)