Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix(forms): don't mutate validators array (#47830)" #47845

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,12 @@
{
"name": "cleanUpView"
},
{
"name": "coerceToAsyncValidator"
},
{
"name": "coerceToValidator"
},
{
"name": "collectNativeNodes"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,15 @@
{
"name": "cleanUpView"
},
{
"name": "coerceToAsyncValidator"
},
{
"name": "coerceToBoolean"
},
{
"name": "coerceToValidator"
},
{
"name": "collectNativeNodes"
},
Expand Down
22 changes: 12 additions & 10 deletions packages/forms/src/model/abstract_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
*
* @internal
*/
private _composedValidatorFn!: ValidatorFn|null;
private _composedValidatorFn: ValidatorFn|null;

/**
* Contains the result of merging asynchronous validators into a single validator function
* (combined using `Validators.composeAsync`).
*
* @internal
*/
private _composedAsyncValidatorFn!: AsyncValidatorFn|null;
private _composedAsyncValidatorFn: AsyncValidatorFn|null;

/**
* Synchronous validators as they were provided:
Expand All @@ -390,7 +390,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
*
* @internal
*/
private _rawValidators!: ValidatorFn|ValidatorFn[]|null;
private _rawValidators: ValidatorFn|ValidatorFn[]|null;

/**
* Asynchronous validators as they were provided:
Expand All @@ -401,7 +401,7 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
*
* @internal
*/
private _rawAsyncValidators!: AsyncValidatorFn|AsyncValidatorFn[]|null;
private _rawAsyncValidators: AsyncValidatorFn|AsyncValidatorFn[]|null;

/**
* The current value of the control.
Expand All @@ -427,8 +427,10 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
constructor(
validators: ValidatorFn|ValidatorFn[]|null,
asyncValidators: AsyncValidatorFn|AsyncValidatorFn[]|null) {
this.setValidators(validators);
this.setAsyncValidators(asyncValidators);
this._rawValidators = validators;
this._rawAsyncValidators = asyncValidators;
this._composedValidatorFn = coerceToValidator(this._rawValidators);
this._composedAsyncValidatorFn = coerceToAsyncValidator(this._rawAsyncValidators);
}

/**
Expand Down Expand Up @@ -618,8 +620,8 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
* using `addValidators()` method instead.
*/
setValidators(validators: ValidatorFn|ValidatorFn[]|null): void {
this._rawValidators = Array.isArray(validators) ? validators.slice() : validators;
this._composedValidatorFn = coerceToValidator(this._rawValidators);
this._rawValidators = validators;
this._composedValidatorFn = coerceToValidator(validators);
}

/**
Expand All @@ -633,8 +635,8 @@ export abstract class AbstractControl<TValue = any, TRawValue extends TValue = T
* using `addAsyncValidators()` method instead.
*/
setAsyncValidators(validators: AsyncValidatorFn|AsyncValidatorFn[]|null): void {
this._rawAsyncValidators = Array.isArray(validators) ? validators.slice() : validators;
this._composedAsyncValidatorFn = coerceToAsyncValidator(this._rawAsyncValidators);
this._rawAsyncValidators = validators;
this._composedAsyncValidatorFn = coerceToAsyncValidator(validators);
}

/**
Expand Down
44 changes: 0 additions & 44 deletions packages/forms/test/form_control_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,6 @@ describe('FormControl', () => {
expect(c.valid).toEqual(true);
});

it('should not mutate the validators array when overriding using setValidators', () => {
const control = new FormControl('');
const originalValidators = [Validators.required];

control.setValidators(originalValidators);
control.addValidators(Validators.minLength(10));

expect(originalValidators.length).toBe(1);
});

it('should override validators by setting `control.validator` field value', () => {
const c = new FormControl('');
expect(c.valid).toEqual(true);
Expand Down Expand Up @@ -367,30 +357,6 @@ describe('FormControl', () => {
expect(c.hasValidator(Validators.required)).toEqual(false);
});

it('should not mutate the validators array when adding/removing sync validators', () => {
const originalValidators = [Validators.required];
const control = new FormControl('', originalValidators);

control.addValidators(Validators.min(10));
expect(originalValidators.length).toBe(1);

control.removeValidators(Validators.required);
expect(originalValidators.length).toBe(1);
});

it('should not mutate the validators array when adding/removing async validators', () => {
const firstValidator = asyncValidator('one');
const secondValidator = asyncValidator('two');
const originalValidators = [firstValidator];
const control = new FormControl('', null, originalValidators);

control.addAsyncValidators(secondValidator);
expect(originalValidators.length).toBe(1);

control.removeAsyncValidators(firstValidator);
expect(originalValidators.length).toBe(1);
});

it('should return false when checking presence of a validator not identical by reference',
() => {
const minValidator = Validators.min(5);
Expand Down Expand Up @@ -552,16 +518,6 @@ describe('FormControl', () => {
expect(c.valid).toEqual(true);
}));

it('should not mutate the validators array when overriding using setValidators', () => {
const control = new FormControl('');
const originalValidators = [asyncValidator('one')];

control.setAsyncValidators(originalValidators);
control.addAsyncValidators(asyncValidator('two'));

expect(originalValidators.length).toBe(1);
});

it('should override validators by setting `control.asyncValidator` field value',
fakeAsync(() => {
const c = new FormControl('');
Expand Down