fix(forms): don't mutate validators array#47830
fix(forms): don't mutate validators array#47830crisbeto wants to merge 1 commit intoangular:mainfrom
Conversation
7bce4ac to
88fd6ee
Compare
There was a problem hiding this comment.
I'm not entirely sure why this was dropped since the functions are still used inside setValidators and setAsyncValidators. My guess is because the calls were removed from the constructor.
There was a problem hiding this comment.
I could also initialize these to null, but it seemed redundant since they're guaranteed to be defined in the constructor, TS just isn't able to figure it out statically.
There was a problem hiding this comment.
Should the type be:
| private _rawAsyncValidators!: AsyncValidatorFn|AsyncValidatorFn[]|null; | |
| private _rawAsyncValidators: ReadonlyArray<AsyncValidatorFn> = []; |
Always an array and always readonly
There was a problem hiding this comment.
The array can be mutated later on by the addValidators and removeValidators methods. We just don't want to mutate the original array given to us by the author.
There was a problem hiding this comment.
Always an array and always readonly
There is a code path where _rawValidators and _rawAsyncValidators may become a function:
https://github.com/angular/angular/blob/main/packages/forms/src/model/abstract_model.ts#L441-L446
However, we can do further refactoring (in a followup PR if needed) to replace the implementation of the validator and asyncValidator setters with setValidators and setAsyncValidators calls, in which case the _rawValidators and _rawAsyncValidators would always be arrays.
There was a problem hiding this comment.
I don't think that this code path is relevant for the fix here though. We only need to clone the value when it's being set to an array and the validator getter/setter is a ValidatorFn|null.
AndrewKushnir
left a comment
There was a problem hiding this comment.
LGTM 👍
I've left a proposal on additional refactoring, but we can do that in a followup PR.
There was a problem hiding this comment.
Always an array and always readonly
There is a code path where _rawValidators and _rawAsyncValidators may become a function:
https://github.com/angular/angular/blob/main/packages/forms/src/model/abstract_model.ts#L441-L446
However, we can do further refactoring (in a followup PR if needed) to replace the implementation of the validator and asyncValidator setters with setValidators and setAsyncValidators calls, in which case the _rawValidators and _rawAsyncValidators would always be arrays.
|
This PR was merged into the repository by commit 0329c13. |
This reverts commit 0329c13.
There was a problem hiding this comment.
It seems that better way is to check the deep equality of the array before and after the manipulations.
Fixes that the `AbstractControl` was mutating the validators arrays being passed into the constructor an helper methods like `setValidators`. Fixes angular#47827.
88fd6ee to
93405c2
Compare
|
I've fixed the issue that caused the PR to be reverted previously. I also have a passing TGP for it. |
|
Caretaker Note: Please ignore the |
|
This PR was merged into the repository by commit 779a76f. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Fixes that the `AbstractControl` was mutating the validators arrays being passed into the constructor an helper methods like `setValidators`. Fixes angular#47827. PR Close angular#47830
Fixes that the `AbstractControl` was mutating the validators arrays being passed into the constructor an helper methods like `setValidators`. Fixes angular#47827. PR Close angular#47830
Fixes that the
AbstractControlwas mutating the validators arrays being passed into the constructor an helper methods likesetValidators.Fixes #47827.