Skip to content

Commit

Permalink
feat: Support array numbers in pathOfError for ErrorSchemaBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
kylie-marshall authored and nickgros committed Sep 27, 2024
1 parent f92f9f6 commit f7e05bb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/docs/docs/api-reference/utility-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ For more information about how to specify the path see the [eslint lodash plugin
#### Parameters

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

#### Returns

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

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

#### Returns

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

#### Parameters

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

#### Returns

Expand Down
11 changes: 6 additions & 5 deletions packages/utils/src/ErrorSchemaBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cloneDeep from 'lodash/cloneDeep';
import get from 'lodash/get';
import set from 'lodash/set';
import setWith from 'lodash/setWith';

import { ErrorSchema } from './types';
import { ERRORS_KEY } from './constants';
Expand Down Expand Up @@ -37,12 +38,12 @@ export default class ErrorSchemaBuilder<T = any> {
* @returns - The error block for the given `pathOfError` or the root if not provided
* @private
*/
private getOrCreateErrorBlock(pathOfError?: string | string[]) {
private getOrCreateErrorBlock(pathOfError?: string | (string | number)[]) {
const hasPath = (Array.isArray(pathOfError) && pathOfError.length > 0) || typeof pathOfError === 'string';
let errorBlock: ErrorSchema = hasPath ? get(this.errorSchema, pathOfError) : this.errorSchema;
if (!errorBlock && pathOfError) {
errorBlock = {};
set(this.errorSchema, pathOfError, errorBlock);
setWith(this.errorSchema, pathOfError, errorBlock, Object);
}
return errorBlock;
}
Expand All @@ -65,7 +66,7 @@ export default class ErrorSchemaBuilder<T = any> {
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to add the error(s)
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
*/
addErrors(errorOrList: string | string[], pathOfError?: string | string[]) {
addErrors(errorOrList: string | string[], pathOfError?: string | (string | number)[]) {
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
let errorsList = get(errorBlock, ERRORS_KEY);
if (!Array.isArray(errorsList)) {
Expand All @@ -89,7 +90,7 @@ export default class ErrorSchemaBuilder<T = any> {
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to set the error(s)
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
*/
setErrors(errorOrList: string | string[], pathOfError?: string | string[]) {
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];
Expand All @@ -104,7 +105,7 @@ export default class ErrorSchemaBuilder<T = any> {
* @param [pathOfError] - The optional path into the `ErrorSchema` at which to clear the error(s)
* @returns - The `ErrorSchemaBuilder` object for chaining purposes
*/
clearErrors(pathOfError?: string | string[]) {
clearErrors(pathOfError?: string | (string | number)[]) {
const errorBlock: ErrorSchema = this.getOrCreateErrorBlock(pathOfError);
set(errorBlock, ERRORS_KEY, []);
return this;
Expand Down
84 changes: 84 additions & 0 deletions packages/utils/test/ErrorSchemaBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,90 @@ describe('ErrorSchemaBuilder', () => {
},
});
});
it('adding error string with a (string | number)[] path puts it at the path', () => {
expect(builder.addErrors(AN_ERROR, ['arr', 0, 'qux']).ErrorSchema).toEqual({
[ERRORS_KEY]: [],
[STRING_PATH]: {
[ERRORS_KEY]: [],
},
[ARRAY_PATH[0]]: {
[ARRAY_PATH[1]]: {
[ERRORS_KEY]: [],
},
},
another: {
path: {
[ERRORS_KEY]: [AN_ERROR],
},
},
newPath: {
[ERRORS_KEY]: [],
},
arr: {
'0': {
qux: {
[ERRORS_KEY]: [AN_ERROR],
},
},
},
});
});
it('setting error string with a new path with number set errors at the path', () => {
expect(builder.setErrors(SOME_ERRORS, ['arr', 0, 'qux']).ErrorSchema).toEqual({
[ERRORS_KEY]: [],
[STRING_PATH]: {
[ERRORS_KEY]: [],
},
[ARRAY_PATH[0]]: {
[ARRAY_PATH[1]]: {
[ERRORS_KEY]: [],
},
},
another: {
path: {
[ERRORS_KEY]: [AN_ERROR],
},
},
newPath: {
[ERRORS_KEY]: [],
},
arr: {
'0': {
qux: {
[ERRORS_KEY]: SOME_ERRORS,
},
},
},
});
});
it('clearing errors with a (string | number)[] path clears them the path', () => {
expect(builder.clearErrors(['arr', 0, 'qux']).ErrorSchema).toEqual({
[ERRORS_KEY]: [],
[STRING_PATH]: {
[ERRORS_KEY]: [],
},
[ARRAY_PATH[0]]: {
[ARRAY_PATH[1]]: {
[ERRORS_KEY]: [],
},
},
another: {
path: {
[ERRORS_KEY]: [AN_ERROR],
},
},
newPath: {
[ERRORS_KEY]: [],
},
arr: {
'0': {
qux: {
[ERRORS_KEY]: [],
},
},
},
});
});
it('resetting error restores things back to an empty object', () => {
expect(builder.resetAllErrors().ErrorSchema).toEqual({});
});
Expand Down

0 comments on commit f7e05bb

Please sign in to comment.