Skip to content

Commit bfe373d

Browse files
committed
fix: ensure validator passed in Form rerenders & prevent null schema errors
This commit fixes two potential issues: 1. Updates `createComponent` test utility to ensure Form components always receive a validator prop when rerendering, preventing potential test failures 2. Adds null checks for rootSchema in SchemaUtils to prevent errors when schema is undefined or null
1 parent 5280e5a commit bfe373d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/core/test/test_utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ export function createComponent(Component, props) {
1616
const comp = <Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />;
1717
const { container, rerender } = render(comp);
1818

19-
const rerenderFunction = (props) =>
20-
rerender(<Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...props} />);
19+
const rerenderFunction = (newProps) => {
20+
// For Form components, ensure validator is always passed
21+
const propsWithValidator = Component === Form && !newProps.validator ? { validator, ...newProps } : newProps;
22+
return rerender(<Component onSubmit={onSubmit} onError={onError} onChange={onChange} {...propsWithValidator} />);
23+
};
2124
const node = findDOMNode(container).firstElementChild;
2225

2326
return { comp, node, onChange, onError, onSubmit, rerender: rerenderFunction };

packages/utils/src/createSchemaUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
6060
experimental_defaultFormStateBehavior: Experimental_DefaultFormStateBehavior,
6161
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
6262
) {
63-
if (rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
63+
if (rootSchema && rootSchema[SCHEMA_KEY] === JSON_SCHEMA_DRAFT_2020_12) {
6464
this.rootSchema = makeAllReferencesAbsolute(rootSchema, get(rootSchema, ID_KEY, '#'));
6565
} else {
6666
this.rootSchema = rootSchema;
@@ -102,9 +102,12 @@ class SchemaUtils<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends Fo
102102
experimental_defaultFormStateBehavior = {},
103103
experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>,
104104
): boolean {
105+
// If either validator or rootSchema are falsy, return false to prevent the creation
106+
// of a new SchemaUtilsType with incomplete properties.
105107
if (!validator || !rootSchema) {
106108
return false;
107109
}
110+
108111
return (
109112
this.validator !== validator ||
110113
!deepEquals(this.rootSchema, rootSchema) ||

0 commit comments

Comments
 (0)