Skip to content

Commit 971c236

Browse files
fix: update schema builder add/set actions to have unique errors
1 parent 011659d commit 971c236

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/utils/src/ErrorSchemaBuilder.ts

Lines changed: 3 additions & 3 deletions
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

Lines changed: 18 additions & 0 deletions
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)