Skip to content

Commit f21e3df

Browse files
feat: Support array numbers in pathOfError for ErrorSchemaBuilder
1 parent d3af307 commit f21e3df

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

packages/docs/docs/api-reference/utility-functions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ For more information about how to specify the path see the [eslint lodash plugin
11721172
#### Parameters
11731173

11741174
- errorOrList: string | string[] - The error or list of errors to add into the `ErrorSchema`
1175-
- [pathOfError]: string | string[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
1175+
- [pathOfError]: string | (string | number)[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
11761176

11771177
#### Returns
11781178

@@ -1186,7 +1186,7 @@ For more information about how to specify the path see the [eslint lodash plugin
11861186
#### Parameters
11871187

11881188
- errorOrList: string | string[] - The error or list of errors to add into the `ErrorSchema`
1189-
- [pathOfError]: string | string[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
1189+
- [pathOfError]: string | (string | number)[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
11901190

11911191
#### Returns
11921192

@@ -1199,7 +1199,7 @@ For more information about how to specify the path see the [eslint lodash plugin
11991199

12001200
#### Parameters
12011201

1202-
- [pathOfError]: string | string[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
1202+
- [pathOfError]: string | (string | number)[] | undefined - The optional path into the `ErrorSchema` at which to add the error(s)
12031203

12041204
#### Returns
12051205

packages/utils/src/ErrorSchemaBuilder.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep';
22
import get from 'lodash/get';
33
import set from 'lodash/set';
4+
import setWith from 'lodash/setWith';
45

56
import { ErrorSchema } from './types';
67
import { ERRORS_KEY } from './constants';
@@ -37,12 +38,12 @@ export default class ErrorSchemaBuilder<T = any> {
3738
* @returns - The error block for the given `pathOfError` or the root if not provided
3839
* @private
3940
*/
40-
private getOrCreateErrorBlock(pathOfError?: string | string[]) {
41+
private getOrCreateErrorBlock(pathOfError?: string | (string | number)[]) {
4142
const hasPath = (Array.isArray(pathOfError) && pathOfError.length > 0) || typeof pathOfError === 'string';
4243
let errorBlock: ErrorSchema = hasPath ? get(this.errorSchema, pathOfError) : this.errorSchema;
4344
if (!errorBlock && pathOfError) {
4445
errorBlock = {};
45-
set(this.errorSchema, pathOfError, errorBlock);
46+
setWith(this.errorSchema, pathOfError, errorBlock, Object);
4647
}
4748
return errorBlock;
4849
}
@@ -65,7 +66,7 @@ export default class ErrorSchemaBuilder<T = any> {
6566
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to add the error(s)
6667
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
6768
*/
68-
addErrors(errorOrList: string | string[], pathOfError?: string | string[]) {
69+
addErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]) {
6970
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
7071
let errorsList = get(errorBlock, ERRORS_KEY);
7172
if (!Array.isArray(errorsList)) {
@@ -89,7 +90,7 @@ export default class ErrorSchemaBuilder<T = any> {
8990
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to set the error(s)
9091
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
9192
*/
92-
setErrors(errorOrList: string | string[], pathOfError?: string | string[]) {
93+
setErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]) {
9394
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
9495
// Effectively clone the array being given to prevent accidental outside manipulation of the given list
9596
const listToAdd = Array.isArray(errorOrList) ? [...errorOrList] : [errorOrList];
@@ -104,7 +105,7 @@ export default class ErrorSchemaBuilder<T = any> {
104105
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to clear the error(s)
105106
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
106107
*/
107-
clearErrors(pathOfError?: string | string[]) {
108+
clearErrors(pathOfError?: string | (string | number)[]) {
108109
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
109110
set(errorBlock, ERRORS_KEY, []);
110111
return this;

packages/utils/test/ErrorSchemaBuilder.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,90 @@ describe('ErrorSchemaBuilder', () => {
153153
},
154154
});
155155
});
156+
it('adding error string with a (string | number)[] path puts it at the path', () => {
157+
expect(builder.addErrors(AN_ERROR, ['arr', 0, 'qux']).ErrorSchema).toEqual({
158+
[ERRORS_KEY]: [],
159+
[STRING_PATH]: {
160+
[ERRORS_KEY]: [],
161+
},
162+
[ARRAY_PATH[0]]: {
163+
[ARRAY_PATH[1]]: {
164+
[ERRORS_KEY]: [],
165+
},
166+
},
167+
another: {
168+
path: {
169+
[ERRORS_KEY]: [AN_ERROR],
170+
},
171+
},
172+
newPath: {
173+
[ERRORS_KEY]: [],
174+
},
175+
arr: {
176+
'0': {
177+
qux: {
178+
[ERRORS_KEY]: [AN_ERROR],
179+
},
180+
},
181+
},
182+
});
183+
});
184+
it('setting error string with a new path with number set errors at the path', () => {
185+
expect(builder.setErrors(SOME_ERRORS, ['arr', 0, 'qux']).ErrorSchema).toEqual({
186+
[ERRORS_KEY]: [],
187+
[STRING_PATH]: {
188+
[ERRORS_KEY]: [],
189+
},
190+
[ARRAY_PATH[0]]: {
191+
[ARRAY_PATH[1]]: {
192+
[ERRORS_KEY]: [],
193+
},
194+
},
195+
another: {
196+
path: {
197+
[ERRORS_KEY]: [AN_ERROR],
198+
},
199+
},
200+
newPath: {
201+
[ERRORS_KEY]: [],
202+
},
203+
arr: {
204+
'0': {
205+
qux: {
206+
[ERRORS_KEY]: SOME_ERRORS,
207+
},
208+
},
209+
},
210+
});
211+
});
212+
it('clearing errors with a (string | number)[] path clears them the path', () => {
213+
expect(builder.clearErrors(['arr', 0, 'qux']).ErrorSchema).toEqual({
214+
[ERRORS_KEY]: [],
215+
[STRING_PATH]: {
216+
[ERRORS_KEY]: [],
217+
},
218+
[ARRAY_PATH[0]]: {
219+
[ARRAY_PATH[1]]: {
220+
[ERRORS_KEY]: [],
221+
},
222+
},
223+
another: {
224+
path: {
225+
[ERRORS_KEY]: [AN_ERROR],
226+
},
227+
},
228+
newPath: {
229+
[ERRORS_KEY]: [],
230+
},
231+
arr: {
232+
'0': {
233+
qux: {
234+
[ERRORS_KEY]: [],
235+
},
236+
},
237+
},
238+
});
239+
});
156240
it('resetting error restores things back to an empty object', () => {
157241
expect(builder.resetAllErrors().ErrorSchema).toEqual({});
158242
});

0 commit comments

Comments
 (0)