Skip to content

Commit

Permalink
feat: add a helper to remove error by key
Browse files Browse the repository at this point in the history
also factorize duplicated code in utils
  • Loading branch information
MarioArnt committed Aug 26, 2020
1 parent e6c9a82 commit 64cabc6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 25 deletions.
7 changes: 7 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ describe('FormArray', () => {
expect(control.errors).toEqual({ isInvalid: true, customError: true });
});

it('should removeError', () => {
const control = createArray();
control.setErrors({ customError: true, otherError: true });
control.removeError('otherError');
expect(control.errors).toEqual({ customError: true });
});

it('should setEnable', () => {
const control = createArray();

Expand Down
14 changes: 6 additions & 8 deletions projects/ngneat/reactive-forms/src/lib/formArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Validator,
ValidatorOrOpts
} from './types';
import { coerceArray } from './utils';
import { coerceArray, mergeErrors, removeError } from './utils';

export class FormArray<T = any, E extends object = any> extends NgFormArray {
readonly value: T[];
Expand Down Expand Up @@ -172,13 +172,11 @@ export class FormArray<T = any, E extends object = any> extends NgFormArray {
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
this.setErrors(mergeErrors<E>(this.errors, errors), opts);
}

removeError(key: keyof E, opts: EmitEvent = {}): void {
this.setErrors(removeError<E>(this.errors, key), opts);
}

getError<K extends ExtractStrings<E>>(errorCode: K, path?: ControlPath) {
Expand Down
29 changes: 28 additions & 1 deletion projects/ngneat/reactive-forms/src/lib/formControl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,39 @@ describe('FormControl', () => {
expect(control.errors).toEqual({ customError: true });
});

it('should mergeErrors', () => {
it('should mergeErrors with previous errors', () => {
const control = new FormControl<string>('', Validators.required);
control.mergeErrors({ customError: true });
expect(control.errors).toEqual({ required: true, customError: true });
});

it('should mergeErrors when no previous errors', () => {
const control = new FormControl<string>('');
control.mergeErrors({ customError: true });
expect(control.errors).toEqual({ customError: true });
});

it('should removeError correctly with two existing errors', () => {
const control = new FormControl<string>('');
control.setErrors({ customError: true, otherError: true });
control.removeError('otherError');
expect(control.errors).toEqual({ customError: true });
});

it('should removeError correctly last error', () => {
const control = new FormControl<string>('');
control.setErrors({ customError: true });
control.removeError('customError');
expect(control.errors).toEqual(null);
});

it('should removeError with not existing errors', () => {
const control = new FormControl<string>('');
control.setErrors({ customError: true });
control.removeError('notExisting');
expect(control.errors).toEqual({ customError: true });
});

it('should setDisable', () => {
const control = new FormControl<string>();
control.setDisable();
Expand Down
14 changes: 6 additions & 8 deletions projects/ngneat/reactive-forms/src/lib/formControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Validator,
ValidatorOrOpts
} from './types';
import { coerceArray } from './utils';
import { coerceArray, mergeErrors, removeError } from './utils';

export class FormControl<T = any, E extends object = any> extends NgFormControl {
readonly value: T;
Expand Down Expand Up @@ -147,13 +147,11 @@ export class FormControl<T = any, E extends object = any> extends NgFormControl
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
this.setErrors(mergeErrors<E>(this.errors, errors), opts);
}

removeError(key: keyof E, opts: EmitEvent = {}): void {
this.setErrors(removeError<E>(this.errors, key), opts);
}

hasErrorAndTouched(error: ExtractStrings<E>): boolean {
Expand Down
7 changes: 7 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/formGroup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ describe('FormGroup', () => {
expect(control.errors).toEqual({ isInvalid: true, customError: true });
});

it('should removeError', () => {
const control = createGroup();
control.setErrors({ customError: true, otherError: true });
control.removeError('otherError');
expect(control.errors).toEqual({ customError: true });
});

it('should setValidators', () => {
const control = createGroup();
spyOn(control, 'setValidators');
Expand Down
14 changes: 6 additions & 8 deletions projects/ngneat/reactive-forms/src/lib/formGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
PersistOptions,
ControlFactoryMap
} from './types';
import { coerceArray, wrapIntoObservable } from './utils';
import { coerceArray, wrapIntoObservable, mergeErrors, removeError } from './utils';
import { PersistManager } from './persistManager';
import { LocalStorageManager } from './localStorageManager';
import { FormArray } from './formArray';
Expand Down Expand Up @@ -215,13 +215,11 @@ export class FormGroup<T extends Obj = any, E extends object = any> extends NgFo
}

mergeErrors(errors: Partial<E>, opts: EmitEvent = {}): void {
this.setErrors(
{
...this.errors,
...errors
},
opts
);
this.setErrors(mergeErrors<E>(this.errors, errors), opts);
}

removeError(key: keyof E, opts: EmitEvent = {}): void {
this.setErrors(removeError<E>(this.errors, key), opts);
}

getError<K extends keyof E, K1 extends keyof T>(errorCode: K, path?: [K1]): E[K] | null;
Expand Down
20 changes: 20 additions & 0 deletions projects/ngneat/reactive-forms/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ export function wrapIntoObservable<T>(value: T | Promise<T> | Observable<T>): Ob

return of(value);
}

export function mergeErrors<E>(existing: Partial<E>, toAdd: Partial<E>) {
return existing
? {
...existing,
...toAdd
}
: toAdd;
}

export function removeError<E>(errors: E, key: keyof E) {
if (!errors) {
return null;
}
const updatedErrors = {
...errors
};
delete updatedErrors[key];
return Object.keys(updatedErrors).length > 0 ? updatedErrors : null;
}

0 comments on commit 64cabc6

Please sign in to comment.