Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixes 1295 Duplicate Error Messages for Anyof/Oneof #3795

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ should change the heading of the (upcoming) version to include a major version b
-->
# 5.11.1

## @rjsf/core

- Updated `SchemaField` to ignore errors for `anyOf`/`oneOf` parent schema, fixing [1295](https://github.com/rjsf-team/react-jsonschema-form/issues/1295)

## @rjsf/utils

- Created new `resolveAllReferences()` function to resolve all references within a schema's properties and array items.
Expand Down
24 changes: 14 additions & 10 deletions packages/core/src/components/fields/SchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,20 @@ function SchemaFieldRender<T = any, S extends StrictRJSFSchema = RJSFSchema, F e
registry={registry}
/>
);
const errorsComponent = hideError ? undefined : (
<FieldErrorTemplate
errors={__errors}
errorSchema={errorSchema}
idSchema={idSchema}
schema={schema}
uiSchema={uiSchema}
registry={registry}
/>
);
/*
* AnyOf/OneOf errors handled by child schema
*/
const errorsComponent =
cwendtxealth marked this conversation as resolved.
Show resolved Hide resolved
hideError || schema.anyOf || schema.oneOf ? undefined : (
<FieldErrorTemplate
errors={__errors}
errorSchema={errorSchema}
idSchema={idSchema}
schema={schema}
uiSchema={uiSchema}
registry={registry}
/>
);
const fieldProps: Omit<FieldTemplateProps<T, S, F>, 'children'> = {
description: (
<DescriptionFieldTemplate
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/SchemaField.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,37 @@ describe('SchemaField', () => {
expect(matches[0].textContent).to.contain('test');
});

it('should ignore errors for top level anyOf/oneOf and show only one in child schema', () => {
const testSchema = {
type: 'object',
properties: {
foo: {
anyOf: [
{
type: 'boolean',
},
{
type: 'array',
items: {
type: 'string',
},
},
],
},
},
};
const { node } = createFormComponent({
schema: testSchema,
uiSchema,
customValidate,
});
Simulate.submit(node);

const matches = node.querySelectorAll('form .form-group .form-group .text-danger');
expect(matches).to.have.length.of(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before fix matches would have length 2

expect(matches[0].textContent).to.contain('test');
});

it('should pass errors to custom FieldErrorTemplate', () => {
const customFieldError = (props) => {
return <div className='custom-field-error'>{props.errors}</div>;
Expand Down