diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b285f115d..1f64b8d014 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,10 +18,18 @@ should change the heading of the (upcoming) version to include a major version b # 6.0.0-beta.12 +## @rjsf/core + +- Updated `Form` to store the `schemaUtils.getRootSchema()` into the `state.schema` and use that everywhere as the `schema` + ## @rjsf/shadcn - Updated the building of `shadcn` to use the `lodashReplacer` with `tsc-alias` fixing [#4678](https://github.com/rjsf-team/react-jsonschema-form/issues/4678) +## @rjsf/utils + +- Updated `SchemaUtils` and `createSchemaUtils()` to add a new `getRootSchema()` function + # 6.0.0-beta.11 ## @rjsf/antd diff --git a/packages/core/src/components/Form.tsx b/packages/core/src/components/Form.tsx index caffc08684..b6b26bc8de 100644 --- a/packages/core/src/components/Form.tsx +++ b/packages/core/src/components/Form.tsx @@ -387,11 +387,11 @@ export default class Form< ): FormState { const state: FormState = this.state || {}; const schema = 'schema' in props ? props.schema : this.props.schema; + const validator = 'validator' in props ? props.validator : this.props.validator; const uiSchema: UiSchema = ('uiSchema' in props ? props.uiSchema! : this.props.uiSchema!) || {}; const edit = typeof inputFormData !== 'undefined'; const liveValidate = 'liveValidate' in props ? props.liveValidate : this.props.liveValidate; const mustValidate = edit && !props.noValidate && liveValidate; - const rootSchema = schema; const experimental_defaultFormStateBehavior = 'experimental_defaultFormStateBehavior' in props ? props.experimental_defaultFormStateBehavior @@ -404,22 +404,23 @@ export default class Form< if ( !schemaUtils || schemaUtils.doesSchemaUtilsDiffer( - props.validator, - rootSchema, + validator, + schema, experimental_defaultFormStateBehavior, experimental_customMergeAllOf, ) ) { schemaUtils = createSchemaUtils( - props.validator, - rootSchema, + validator, + schema, experimental_defaultFormStateBehavior, experimental_customMergeAllOf, ); } - const formData: T = schemaUtils.getDefaultFormState(schema, inputFormData) as T; + const rootSchema = schemaUtils.getRootSchema(); + const formData: T = schemaUtils.getDefaultFormState(rootSchema, inputFormData) as T; const _retrievedSchema = this.updateRetrievedSchema( - retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData), + retrievedSchema ?? schemaUtils.retrieveSchema(rootSchema, formData), ); const getCurrentErrors = (): ValidationData => { @@ -443,7 +444,7 @@ export default class Form< let schemaValidationErrors: RJSFValidationError[] = state.schemaValidationErrors; let schemaValidationErrorSchema: ErrorSchema = state.schemaValidationErrorSchema; if (mustValidate) { - const schemaValidation = this.validate(formData, schema, schemaUtils, _retrievedSchema); + const schemaValidation = this.validate(formData, rootSchema, schemaUtils, _retrievedSchema); errors = schemaValidation.errors; // If retrievedSchema is undefined which means the schema or formData has changed, we do not merge state. // Else in the case where it hasn't changed, we merge 'state.errorSchema' with 'schemaValidation.errorSchema.' This done to display the raised field error. @@ -492,7 +493,7 @@ export default class Form< ); const nextState: FormState = { schemaUtils, - schema, + schema: rootSchema, uiSchema, idSchema, formData, @@ -541,7 +542,7 @@ export default class Form< */ validate( formData: T | undefined, - schema = this.props.schema, + schema = this.state.schema, altSchemaUtils?: SchemaUtilsType, retrievedSchema?: S, ): ValidationData { @@ -869,7 +870,7 @@ export default class Form< /** Returns the registry for the form */ getRegistry(): Registry { const { translateString: customTranslateString, uiSchema = {} } = this.props; - const { schemaUtils } = this.state; + const { schema, schemaUtils } = this.state; const { fields, templates, widgets, formContext, translateString } = getDefaultRegistry(); return { fields: { ...fields, ...this.props.fields }, @@ -882,7 +883,7 @@ export default class Form< }, }, widgets: { ...widgets, ...this.props.widgets }, - rootSchema: this.props.schema, + rootSchema: schema, formContext: this.props.formContext || formContext, schemaUtils, translateString: customTranslateString || translateString, diff --git a/packages/core/test/test_utils.js b/packages/core/test/test_utils.js index b859221db5..9011286850 100644 --- a/packages/core/test/test_utils.js +++ b/packages/core/test/test_utils.js @@ -33,7 +33,7 @@ export function createSandbox() { } export function setProps(comp, newProps) { - render(createElement(Form, newProps), { + render(createElement(Form, { validator, ...newProps }), { container: comp.ref.current.formElement.current.parentNode, }); } diff --git a/packages/utils/src/createSchemaUtils.ts b/packages/utils/src/createSchemaUtils.ts index f44687b43a..28a3db9c2d 100644 --- a/packages/utils/src/createSchemaUtils.ts +++ b/packages/utils/src/createSchemaUtils.ts @@ -63,6 +63,14 @@ class SchemaUtils { * set of APIs to the `@rjsf/core` components and the various themes as well. */ export interface SchemaUtilsType { + /** Returns the `rootSchema` in the `SchemaUtilsType` + * + * @returns - The rootSchema + */ + getRootSchema(): S; /** Returns the `ValidatorType` in the `SchemaUtilsType` * * @returns - The `ValidatorType` diff --git a/packages/utils/test/createSchemaUtils.test.ts b/packages/utils/test/createSchemaUtils.test.ts index 3cecc1fe4b..1beced47e4 100644 --- a/packages/utils/test/createSchemaUtils.test.ts +++ b/packages/utils/test/createSchemaUtils.test.ts @@ -15,6 +15,10 @@ describe('createSchemaUtils()', () => { }; const schemaUtils: SchemaUtilsType = createSchemaUtils(testValidator, rootSchema, defaultFormStateBehavior); + it('getRootSchema()', () => { + expect(schemaUtils.getRootSchema()).toEqual(rootSchema); + }); + it('getValidator()', () => { expect(schemaUtils.getValidator()).toBe(testValidator); });