Skip to content

Commit 1f03322

Browse files
Improve Form to stash schemaUtils.rootSchema as state.schema (#4687)
To assist potential future transformations of the `rootSchema` in the `schemaUtils` updated `Form` to always use that as the `state.schema` - In `@rjsf/utils`: - Added `getRootSchema()` to the `SchemaUtilsType` and `createSchemaUtils()` - Updated the tests to maintain 100% coverage - In `@rjsf/core`: - Updated `Form` to always pull the `rootSchema` from the `schemaUtils` and put it into the `state.schema`, making sure to use that in the `render()`, `getStateFromProps()` and `getRegistry()` - Updated the `test_utils.js` to pass the `validator` in the `setProps()` function - Updated `CHANGELOG.md` accordingly
1 parent 33fb35f commit 1f03322

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ should change the heading of the (upcoming) version to include a major version b
1818

1919
# 6.0.0-beta.12
2020

21+
## @rjsf/core
22+
23+
- Updated `Form` to store the `schemaUtils.getRootSchema()` into the `state.schema` and use that everywhere as the `schema`
24+
2125
## @rjsf/shadcn
2226

2327
- Updated the building of `shadcn` to use the `lodashReplacer` with `tsc-alias` fixing [#4678](https://github.com/rjsf-team/react-jsonschema-form/issues/4678)
2428

29+
## @rjsf/utils
30+
31+
- Updated `SchemaUtils` and `createSchemaUtils()` to add a new `getRootSchema()` function
32+
2533
# 6.0.0-beta.11
2634

2735
## @rjsf/antd

packages/core/src/components/Form.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,11 @@ export default class Form<
387387
): FormState<T, S, F> {
388388
const state: FormState<T, S, F> = this.state || {};
389389
const schema = 'schema' in props ? props.schema : this.props.schema;
390+
const validator = 'validator' in props ? props.validator : this.props.validator;
390391
const uiSchema: UiSchema<T, S, F> = ('uiSchema' in props ? props.uiSchema! : this.props.uiSchema!) || {};
391392
const edit = typeof inputFormData !== 'undefined';
392393
const liveValidate = 'liveValidate' in props ? props.liveValidate : this.props.liveValidate;
393394
const mustValidate = edit && !props.noValidate && liveValidate;
394-
const rootSchema = schema;
395395
const experimental_defaultFormStateBehavior =
396396
'experimental_defaultFormStateBehavior' in props
397397
? props.experimental_defaultFormStateBehavior
@@ -404,22 +404,23 @@ export default class Form<
404404
if (
405405
!schemaUtils ||
406406
schemaUtils.doesSchemaUtilsDiffer(
407-
props.validator,
408-
rootSchema,
407+
validator,
408+
schema,
409409
experimental_defaultFormStateBehavior,
410410
experimental_customMergeAllOf,
411411
)
412412
) {
413413
schemaUtils = createSchemaUtils<T, S, F>(
414-
props.validator,
415-
rootSchema,
414+
validator,
415+
schema,
416416
experimental_defaultFormStateBehavior,
417417
experimental_customMergeAllOf,
418418
);
419419
}
420-
const formData: T = schemaUtils.getDefaultFormState(schema, inputFormData) as T;
420+
const rootSchema = schemaUtils.getRootSchema();
421+
const formData: T = schemaUtils.getDefaultFormState(rootSchema, inputFormData) as T;
421422
const _retrievedSchema = this.updateRetrievedSchema(
422-
retrievedSchema ?? schemaUtils.retrieveSchema(schema, formData),
423+
retrievedSchema ?? schemaUtils.retrieveSchema(rootSchema, formData),
423424
);
424425

425426
const getCurrentErrors = (): ValidationData<T> => {
@@ -443,7 +444,7 @@ export default class Form<
443444
let schemaValidationErrors: RJSFValidationError[] = state.schemaValidationErrors;
444445
let schemaValidationErrorSchema: ErrorSchema<T> = state.schemaValidationErrorSchema;
445446
if (mustValidate) {
446-
const schemaValidation = this.validate(formData, schema, schemaUtils, _retrievedSchema);
447+
const schemaValidation = this.validate(formData, rootSchema, schemaUtils, _retrievedSchema);
447448
errors = schemaValidation.errors;
448449
// If retrievedSchema is undefined which means the schema or formData has changed, we do not merge state.
449450
// 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<
492493
);
493494
const nextState: FormState<T, S, F> = {
494495
schemaUtils,
495-
schema,
496+
schema: rootSchema,
496497
uiSchema,
497498
idSchema,
498499
formData,
@@ -541,7 +542,7 @@ export default class Form<
541542
*/
542543
validate(
543544
formData: T | undefined,
544-
schema = this.props.schema,
545+
schema = this.state.schema,
545546
altSchemaUtils?: SchemaUtilsType<T, S, F>,
546547
retrievedSchema?: S,
547548
): ValidationData<T> {
@@ -869,7 +870,7 @@ export default class Form<
869870
/** Returns the registry for the form */
870871
getRegistry(): Registry<T, S, F> {
871872
const { translateString: customTranslateString, uiSchema = {} } = this.props;
872-
const { schemaUtils } = this.state;
873+
const { schema, schemaUtils } = this.state;
873874
const { fields, templates, widgets, formContext, translateString } = getDefaultRegistry<T, S, F>();
874875
return {
875876
fields: { ...fields, ...this.props.fields },
@@ -882,7 +883,7 @@ export default class Form<
882883
},
883884
},
884885
widgets: { ...widgets, ...this.props.widgets },
885-
rootSchema: this.props.schema,
886+
rootSchema: schema,
886887
formContext: this.props.formContext || formContext,
887888
schemaUtils,
888889
translateString: customTranslateString || translateString,

packages/core/test/test_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createSandbox() {
3333
}
3434

3535
export function setProps(comp, newProps) {
36-
render(createElement(Form, newProps), {
36+
render(createElement(Form, { validator, ...newProps }), {
3737
container: comp.ref.current.formElement.current.parentNode,
3838
});
3939
}

packages/utils/src/createSchemaUtils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
6363
this.experimental_customMergeAllOf = experimental_customMergeAllOf;
6464
}
6565

66+
/** Returns the `rootSchema` in the `SchemaUtilsType`
67+
*
68+
* @returns - The `rootSchema`
69+
*/
70+
getRootSchema() {
71+
return this.rootSchema;
72+
}
73+
6674
/** Returns the `ValidatorType` in the `SchemaUtilsType`
6775
*
6876
* @returns - The `ValidatorType`

packages/utils/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,11 @@ export interface FoundFieldType<S extends StrictRJSFSchema = RJSFSchema> {
11141114
* set of APIs to the `@rjsf/core` components and the various themes as well.
11151115
*/
11161116
export interface SchemaUtilsType<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> {
1117+
/** Returns the `rootSchema` in the `SchemaUtilsType`
1118+
*
1119+
* @returns - The rootSchema
1120+
*/
1121+
getRootSchema(): S;
11171122
/** Returns the `ValidatorType` in the `SchemaUtilsType`
11181123
*
11191124
* @returns - The `ValidatorType`

packages/utils/test/createSchemaUtils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('createSchemaUtils()', () => {
1515
};
1616
const schemaUtils: SchemaUtilsType = createSchemaUtils(testValidator, rootSchema, defaultFormStateBehavior);
1717

18+
it('getRootSchema()', () => {
19+
expect(schemaUtils.getRootSchema()).toEqual(rootSchema);
20+
});
21+
1822
it('getValidator()', () => {
1923
expect(schemaUtils.getValidator()).toBe(testValidator);
2024
});

0 commit comments

Comments
 (0)