Skip to content

Commit

Permalink
fix: Update schema builder add/set actions to have unique errors (#4386)
Browse files Browse the repository at this point in the history
* fix: update schema builder add/set actions to have unique errors

* docs: add fix description to changelog

---------

Co-authored-by: Heath C <[email protected]>
  • Loading branch information
guilhermecastros and heath-freenome authored Nov 22, 2024
1 parent 713d04b commit 306f2b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
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

0 comments on commit 306f2b3

Please sign in to comment.