The Wayback Machine - https://web.archive.org/web/20250404030211/https://github.com/angular/angular/pull/45793
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

feat(forms): support type set in form validators #45793

Closed

Conversation

jeripeierSBB
Copy link
Contributor

@jeripeierSBB jeripeierSBB commented Apr 28, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Currently, using Validators.required, Validators.minLength and Validators.maxLength validators don't work with sets because a set has a size property instead of a length property.

What is the new behavior?

Validators.required, Validators.minLength and Validators.maxLength validators are working with sets.

Does this PR introduce a breaking change?

  • Yes
  • No

Closes #46101

@pullapprove pullapprove bot requested a review from dylhunn April 28, 2022 07:57
@jeripeierSBB jeripeierSBB force-pushed the support-set-in-validators branch 2 times, most recently from 771d20f to 123c25d Compare April 28, 2022 09:06
@ngbot ngbot bot added this to the Backlog milestone Apr 28, 2022
@dylhunn
Copy link
Contributor

dylhunn commented Apr 28, 2022

I assume this is for custom controls that give a Set when calling .value?

@jeripeierSBB
Copy link
Contributor Author

I assume this is for custom controls that give a Set when calling .value?

yes exactely, eg. const control = new FormControl(new Set(), [Validators.required]).

We often use this in context of a chip input to easy avoid duplicates.

@dylhunn
Copy link
Contributor

dylhunn commented May 2, 2022

I don't think this is traditionally a use case we have thought much about, WDYT @AndrewKushnir?

@dylhunn
Copy link
Contributor

dylhunn commented May 2, 2022

Thank you for contributing @jeripeierSBB, but we are going to put this on the back burner for now. We currently do not support Set or Map anywhere, and if we decide to do so, we probably want to update every API at once.

Can you open a Feature Request instead (e.g. "Forms should accept Set collections in addition to arrays")? Thanks!

@dylhunn dylhunn closed this May 2, 2022
@jeripeierSBB
Copy link
Contributor Author

@dylhunn Thank you for your statement, I will open a feature request. Just for my understanding, can you give me a hint, where at other places in forms as validators the type does matter? Or in other words, where would additional changes be required? Thanks!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 3, 2022
@dylhunn dylhunn reopened this Jun 15, 2022
@angular angular unlocked this conversation Jun 15, 2022
@dylhunn
Copy link
Contributor

dylhunn commented Jun 15, 2022

As discussed in #46101, there is significant community interest in this feature. I will review this PR this week.

2023 edit: oops. Time flies, I'll try to have a look

@dylhunn dylhunn added feature Issue that requests a new feature action: review The PR is still awaiting reviews from at least one requested reviewer state: confirmed forms: validators target: minor This PR is targeted for the next minor release labels Jun 15, 2022
@jeripeierSBB jeripeierSBB force-pushed the support-set-in-validators branch from 123c25d to 7660bdb Compare February 1, 2023 08:30
@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Feb 1, 2023
@jeripeierSBB jeripeierSBB requested review from JeanMeche and removed request for dylhunn February 1, 2023 08:35
@pullapprove pullapprove bot requested review from atscott and dylhunn and removed request for JeanMeche February 1, 2023 08:35
@JeanMeche JeanMeche requested a review from kirjs January 15, 2025 21:02
@JeanMeche
Copy link
Member

We have some legitimate failures in the tests, could you please have a look ?

@jeripeierSBB
Copy link
Contributor Author

We have some legitimate failures in the tests, could you please have a look ?

@JeanMeche Pushed the fix. Sorry, it was a mistake on my part when rebasing.

* Return null else.
* @param value Either an array, set or undefined.
*/
function lengthOrSize(value: any): number | null {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No super strong feelings, but I kinda feel that unknown would be the right type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do so, but then I would need to cast to any or do additional checks when accessing length and size. Would you prefer a change, and if so by casting or introducing IsArray() or instanceOf Set checks?
Performance wise I think only casting would be better.

Copy link
Contributor

@kirjs kirjs Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think casting to any is necessary, woudln't something like this work?
Also see my other comment

if((Array.isArray(value) || typeof value === 'string') && typeof value.length === 'number'){
    ```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type unknown finally introduced with 1f6d76e

@jeripeierSBB jeripeierSBB force-pushed the support-set-in-validators branch from 95a50a6 to b0cb669 Compare January 16, 2025 19:35
@jeripeierSBB jeripeierSBB requested a review from kirjs January 17, 2025 21:02
@kirjs
Copy link
Contributor

kirjs commented Jan 21, 2025

Hey @jeripeierSBB,
I've had some discussions with teammates regarding this PR, and it's not super clear, how it aligns with the bigger picture of forms supporting sets in general, and whether it'd have repercussions for other parts of the forms.

I'm going to bring this up in the next team meeting, to thoroughly discuss this.

I know there's been a lot of back and forth on this PR, and appreciate your patience, hopefully I'll have a final answer for you within the next couple of weeks.

@kirjs
Copy link
Contributor

kirjs commented Jan 28, 2025

Hey @jeripeierSBB, just chatted with the team, let's merge this in!
i'm going to follow up on the code review right now.

@jeripeierSBB jeripeierSBB force-pushed the support-set-in-validators branch from b0cb669 to 1f6d76e Compare January 28, 2025 22:46
@jeripeierSBB jeripeierSBB requested a review from kirjs January 28, 2025 22:52
@kirjs kirjs added the action: presubmit The PR is in need of a google3 presubmit label Feb 3, 2025
@kirjs kirjs added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer action: presubmit The PR is in need of a google3 presubmit labels Feb 3, 2025
@kirjs
Copy link
Contributor

kirjs commented Feb 4, 2025

Ok, this should be good to merge, thanks a lot for addressing all the feedback

@alxhub
Copy link
Member

alxhub commented Feb 5, 2025

This PR was merged into the repository by commit fa0c3e3.

The changes were merged into the following branches: main

@alxhub alxhub closed this in fa0c3e3 Feb 5, 2025
PrajaktaB27 pushed a commit to PrajaktaB27/angular that referenced this pull request Feb 7, 2025
Previously, using `Validators.required`, `Validators.minLength` and `Validators.maxLength` validators don't work with sets because a set has the `size` property instead of the `length` property. This change enables the validators to be working with sets.

PR Close angular#45793
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms detected: feature PR contains a feature commit feature Issue that requests a new feature forms: validators state: confirmed target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Forms should accept Set collections in addition to arrays
5 participants