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: Update schema builder add/set actions to have unique errors #4386

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 @@ -27,6 +27,7 @@ should change the heading of the (upcoming) version to include a major version b
- Updated `Experimental_DefaultFormStateBehavior` to add a new `constAsDefaults` option
- Updated `getDefaultFormState()` to use the new `constAsDefaults` option to control how const is used for defaulting, fixing [#4344](https://github.com/rjsf-team/react-jsonschema-form/issues/4344), [#4361](https://github.com/rjsf-team/react-jsonschema-form/issues/4361) and [#4377](https://github.com/rjsf-team/react-jsonschema-form/issues/4377)
- Use `experimental_customMergeAllOf` option in functions that have previously missed it.
- Updated `ErrorSchemaBuilder` methods `addErrors` and `setErrors` to prevent duplicate error messages.

## @rjsf/validator-ajv8

Expand Down
6 changes: 3 additions & 3 deletions packages/utils/src/ErrorSchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export default class ErrorSchemaBuilder<T = any> {
}

if (Array.isArray(errorOrList)) {
errorsList.push(...errorOrList);
set(errorBlock, ERRORS_KEY, [...new Set([...errorsList, ...errorOrList])]);
} else {
errorsList.push(errorOrList);
set(errorBlock, ERRORS_KEY, [...new Set([...errorsList, errorOrList])]);
}
return this;
}
Expand All @@ -93,7 +93,7 @@ export default class ErrorSchemaBuilder<T = any> {
setErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]) {
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
// Effectively clone the array being given to prevent accidental outside manipulation of the given list
const listToAdd = Array.isArray(errorOrList) ? [...errorOrList] : [errorOrList];
const listToAdd = Array.isArray(errorOrList) ? [...new Set([...errorOrList])] : [errorOrList];
set(errorBlock, ERRORS_KEY, listToAdd);
return this;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/utils/test/ErrorSchemaBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ describe('ErrorSchemaBuilder', () => {
it('resetting error restores things back to an empty object', () => {
expect(builder.resetAllErrors().ErrorSchema).toEqual({});
});
it('adding duplicated error string with a path puts only the unique errors at the path', () => {
builder.addErrors([AN_ERROR], STRING_PATH);
builder.addErrors([AN_ERROR], STRING_PATH);
expect(builder.ErrorSchema).toEqual({
[STRING_PATH]: {
[ERRORS_KEY]: [AN_ERROR],
},
});
});

it('setting duplicated error string with a path puts only the unique errors at the path', () => {
builder.setErrors([AN_ERROR, AN_ERROR], STRING_PATH);
expect(builder.ErrorSchema).toEqual({
[STRING_PATH]: {
[ERRORS_KEY]: [AN_ERROR],
},
});
});
});
describe('using initial schema', () => {
let builder: ErrorSchemaBuilder;
Expand Down