Skip to content

Commit 306f2b3

Browse files
fix: Update schema builder add/set actions to have unique errors (#4386)
* fix: update schema builder add/set actions to have unique errors * docs: add fix description to changelog --------- Co-authored-by: Heath C <[email protected]>
1 parent 713d04b commit 306f2b3

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ should change the heading of the (upcoming) version to include a major version b
2727
- Updated `Experimental_DefaultFormStateBehavior` to add a new `constAsDefaults` option
2828
- 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)
2929
- Use `experimental_customMergeAllOf` option in functions that have previously missed it.
30+
- Updated `ErrorSchemaBuilder` methods `addErrors` and `setErrors` to prevent duplicate error messages.
3031

3132
## @rjsf/validator-ajv8
3233

packages/utils/src/ErrorSchemaBuilder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export default class ErrorSchemaBuilder<T = any> {
7575
}
7676

7777
if (Array.isArray(errorOrList)) {
78-
errorsList.push(...errorOrList);
78+
set(errorBlock, ERRORS_KEY, [...new Set([...errorsList, ...errorOrList])]);
7979
} else {
80-
errorsList.push(errorOrList);
80+
set(errorBlock, ERRORS_KEY, [...new Set([...errorsList, errorOrList])]);
8181
}
8282
return this;
8383
}
@@ -93,7 +93,7 @@ export default class ErrorSchemaBuilder<T = any> {
9393
setErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]) {
9494
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
9595
// Effectively clone the array being given to prevent accidental outside manipulation of the given list
96-
const listToAdd = Array.isArray(errorOrList) ? [...errorOrList] : [errorOrList];
96+
const listToAdd = Array.isArray(errorOrList) ? [...new Set([...errorOrList])] : [errorOrList];
9797
set(errorBlock, ERRORS_KEY, listToAdd);
9898
return this;
9999
}

packages/utils/test/ErrorSchemaBuilder.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ describe('ErrorSchemaBuilder', () => {
240240
it('resetting error restores things back to an empty object', () => {
241241
expect(builder.resetAllErrors().ErrorSchema).toEqual({});
242242
});
243+
it('adding duplicated error string with a path puts only the unique errors at the path', () => {
244+
builder.addErrors([AN_ERROR], STRING_PATH);
245+
builder.addErrors([AN_ERROR], STRING_PATH);
246+
expect(builder.ErrorSchema).toEqual({
247+
[STRING_PATH]: {
248+
[ERRORS_KEY]: [AN_ERROR],
249+
},
250+
});
251+
});
252+
253+
it('setting duplicated error string with a path puts only the unique errors at the path', () => {
254+
builder.setErrors([AN_ERROR, AN_ERROR], STRING_PATH);
255+
expect(builder.ErrorSchema).toEqual({
256+
[STRING_PATH]: {
257+
[ERRORS_KEY]: [AN_ERROR],
258+
},
259+
});
260+
});
243261
});
244262
describe('using initial schema', () => {
245263
let builder: ErrorSchemaBuilder;

0 commit comments

Comments
 (0)