Skip to content

Commit b5f54b0

Browse files
Fix 4371 by adding schema to thrown errors (rjsf-team#4759)
1 parent 0c6dfa5 commit b5f54b0

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ should change the heading of the (upcoming) version to include a major version b
3939
- `ErrorListProps`, `FieldProps`, `FieldTemplateProps`, `ArrayFieldTemplateProps` and `WidgetProps`
4040
- Update `mergeDefaultsWithFormData` to properly handle overriding `undefined` formData with a `null` default value, fixing [#4734](https://github.com/rjsf-team/react-jsonschema-form/issues/4734)
4141
- Fixed object reference sharing in arrays with minItems when using oneOf schemas, fixing [#4756](https://github.com/rjsf-team/react-jsonschema-form/issues/4756)
42+
- Updated `getWigets()` to output the `schema` when throwing errors, fixing [#4731](https://github.com/rjsf-team/react-jsonschema-form/issues/4731)
4243

4344
## Dev / docs / playground
4445

packages/utils/src/getWidget.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default function getWidget<T = any, S extends StrictRJSFSchema = RJSFSche
110110
}
111111

112112
if (typeof widget !== 'string') {
113-
throw new Error(`Unsupported widget definition: ${typeof widget}`);
113+
throw new Error(`Unsupported widget definition: ${typeof widget} in schema: ${JSON.stringify(schema)}`);
114114
}
115115

116116
if (widget in registeredWidgets) {
@@ -120,7 +120,7 @@ export default function getWidget<T = any, S extends StrictRJSFSchema = RJSFSche
120120

121121
if (typeof type === 'string') {
122122
if (!(type in widgetMap)) {
123-
throw new Error(`No widget for type '${type}'`);
123+
throw new Error(`No widget for type '${type}' in schema: ${JSON.stringify(schema)}`);
124124
}
125125

126126
if (widget in widgetMap[type]) {
@@ -129,5 +129,5 @@ export default function getWidget<T = any, S extends StrictRJSFSchema = RJSFSche
129129
}
130130
}
131131

132-
throw new Error(`No widget '${widget}' for type '${type}'`);
132+
throw new Error(`No widget '${widget}' for type '${type}' in schema: ${JSON.stringify(schema)}`);
133133
}

packages/utils/test/getWidget.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const subschema: RJSFSchema = {
88
default: true,
99
};
1010

11+
const subschemaStr = JSON.stringify(subschema);
12+
1113
const schema: RJSFSchema = {
1214
type: 'object',
1315
properties: {
@@ -26,6 +28,7 @@ const schema: RJSFSchema = {
2628
},
2729
},
2830
};
31+
const schemaStr = JSON.stringify(schema);
2932

3033
const TestRefWidget: Widget = forwardRef<HTMLSpanElement, Partial<WidgetProps>>(function TestRefWidget(
3134
props: Partial<WidgetProps>,
@@ -88,19 +91,21 @@ const widgetProps: WidgetProps = {
8891

8992
describe('getWidget()', () => {
9093
it('should fail if widget has incorrect type', () => {
91-
expect(() => getWidget(schema)).toThrow('Unsupported widget definition: undefined');
94+
expect(() => getWidget(schema)).toThrow(`Unsupported widget definition: undefined in schema: ${schemaStr}`);
9295
});
9396

9497
it('should fail if widget has no type property', () => {
95-
expect(() => getWidget(schema, 'blabla')).toThrow(`No widget for type 'object'`);
98+
expect(() => getWidget(schema, 'blabla')).toThrow(`No widget for type 'object' in schema: ${schemaStr}`);
9699
});
97100

98101
it('should fail if schema `type` has no widget property', () => {
99-
expect(() => getWidget(subschema, 'blabla')).toThrow(`No widget 'blabla' for type 'boolean'`);
102+
expect(() => getWidget(subschema, 'blabla')).toThrow(
103+
`No widget 'blabla' for type 'boolean' in schema: ${subschemaStr}`,
104+
);
100105
});
101106

102107
it('should fail if schema has no type property', () => {
103-
expect(() => getWidget({}, 'blabla')).toThrow(`No widget 'blabla' for type 'undefined'`);
108+
expect(() => getWidget({}, 'blabla')).toThrow(`No widget 'blabla' for type 'undefined' in schema: {}`);
104109
});
105110

106111
it('should return widget if in registered widgets', () => {

0 commit comments

Comments
 (0)