Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ should change the heading of the (upcoming) version to include a major version b
- `ErrorListProps`, `FieldProps`, `FieldTemplateProps`, `ArrayFieldTemplateProps` and `WidgetProps`
- 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)
- Fixed object reference sharing in arrays with minItems when using oneOf schemas, fixing [#4756](https://github.com/rjsf-team/react-jsonschema-form/issues/4756)
- Updated `getWigets()` to output the `schema` when throwing errors, fixing [#4731](https://github.com/rjsf-team/react-jsonschema-form/issues/4731)

## Dev / docs / playground

Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/getWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function getWidget<T = any, S extends StrictRJSFSchema = RJSFSche
}

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

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

if (typeof type === 'string') {
if (!(type in widgetMap)) {
throw new Error(`No widget for type '${type}'`);
throw new Error(`No widget for type '${type}' in schema: ${JSON.stringify(schema)}`);
}

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

throw new Error(`No widget '${widget}' for type '${type}'`);
throw new Error(`No widget '${widget}' for type '${type}' in schema: ${JSON.stringify(schema)}`);
}
13 changes: 9 additions & 4 deletions packages/utils/test/getWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const subschema: RJSFSchema = {
default: true,
};

const subschemaStr = JSON.stringify(subschema);

const schema: RJSFSchema = {
type: 'object',
properties: {
Expand All @@ -26,6 +28,7 @@ const schema: RJSFSchema = {
},
},
};
const schemaStr = JSON.stringify(schema);

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

describe('getWidget()', () => {
it('should fail if widget has incorrect type', () => {
expect(() => getWidget(schema)).toThrow('Unsupported widget definition: undefined');
expect(() => getWidget(schema)).toThrow(`Unsupported widget definition: undefined in schema: ${schemaStr}`);
});

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

it('should fail if schema `type` has no widget property', () => {
expect(() => getWidget(subschema, 'blabla')).toThrow(`No widget 'blabla' for type 'boolean'`);
expect(() => getWidget(subschema, 'blabla')).toThrow(
`No widget 'blabla' for type 'boolean' in schema: ${subschemaStr}`,
);
});

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

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