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

fix(forms): handle numeric values properly in the validator #36157

Closed
wants to merge 3 commits into from

Conversation

sonukapoor
Copy link
Contributor

@sonukapoor sonukapoor commented Mar 20, 2020

Previously, the behaviour of the minLength and maxLength validator
caused confusion, and making it appear as they work with numeric values.
This commit fixes this, by always passing the validation when a numeric value
is used.

BREAKING CHANGES:

  • The minLength and maxLength validators now verify that a value has
    numeric length property and invoke validation only if that's the case.
    Previously, falsey values without the length property (such as 0 or
    false values) were triggering validation errors. If your code relies on
    the old behavior, you can include other validators such as min or
    requiredTrue to the list of validators for a particular field.

Closes #35591

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?

Issue Number: #35591

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@petebacondarwin petebacondarwin added area: forms target: patch This PR is targeted for the next patch release type: bug/fix labels Mar 20, 2020
@ngbot ngbot bot modified the milestone: needsTriage Mar 20, 2020
Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

This change is an improvement, generally, since it is always safer (and more performant) to compare against undefined and I am happy for it to land.

But we should be aware, and encourage people (perhaps via documentation) not to use minlength with number fields, precisely for some of the reasons laid out in the comments for the original issue. Use cases where the string length of the number are important seem to me that they should really be considered strings that are restricted to certain patterns, which can be dealt with by the pattern validator. For example a telephone number is not really a "number" in the HTML forms sense of the word. It is really a string of digits, which could be validated with something like Validators.pattern(/\d{9,11}/) for example.

@AndrewKushnir AndrewKushnir added the action: review The PR is still awaiting reviews from at least one requested reviewer label Mar 20, 2020
packages/forms/src/validators.ts Outdated Show resolved Hide resolved
packages/forms/src/validators.ts Outdated Show resolved Hide resolved
@sonukapoor sonukapoor changed the title fix(forms): verify 0 properly in the validator fix(forms): handle numeric values properly in the validator Mar 25, 2020
@sonukapoor
Copy link
Contributor Author

@AndrewKushnir / @petebacondarwin Something is wrong with CI. It's failing with a 403 error.

@petebacondarwin
Copy link
Member

Not your fault @sonukapoor - the setup task makes requests to the Github API in order to check some stuff related to the current master commits, etc. Since this is done via the unauthenticated API, we occasionally overrun the number of allowed requests. I'll re-run the job and see if it gets through.

@sonukapoor sonukapoor force-pushed the forms-min-validator-fix branch 3 times, most recently from 8d1ed82 to 1cc99f2 Compare March 31, 2020 09:45
@AndrewKushnir AndrewKushnir added breaking changes target: major This PR is targeted for the next major release and removed target: patch This PR is targeted for the next patch release labels Apr 6, 2020
@sonukapoor sonukapoor force-pushed the forms-min-validator-fix branch 2 times, most recently from e377758 to c165918 Compare April 8, 2020 01:41
@sonukapoor
Copy link
Contributor Author

@AndrewKushnir Thanks, your version sounds good to me. I will update both, the commit and the PR description.

@sonukapoor
Copy link
Contributor Author

@AndrewKushnir Updated.

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Thanks for updating breaking changes section Sonu! 👍

The changes LGTM, I just left a final comment on the tests, could you please have a look when you get a chance?

packages/forms/test/validators_spec.ts Outdated Show resolved Hide resolved
@AndrewKushnir AndrewKushnir requested a review from kara April 9, 2020 22:11
Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Change LGTM, but can we update the first paragraph of the commit message to read:

Previously, the behavior of the `minLength` and `maxLength` validators
caused confusion, as they appeared to work with numeric values but 
did not in fact produce consistent results. This commit fixes the issue 
by skipping validation altogether when a numeric value is used.

@kara kara added action: presubmit The PR is in need of a google3 presubmit action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels May 4, 2020
@AndrewKushnir
Copy link
Contributor

@sonukapoor I'll start Global Presubmit tonight and if it goes well, we'll be able to add this PR to the merge queue. I noticed that it was rebased 25 days ago, so it'd be great to do one more rebase, so this PR is in the up-to-date state and we have a fresh CI run. Thank you.

@AndrewKushnir
Copy link
Contributor

Previously, the behavior of the `minLength` and `maxLength` validators
caused confusion, as they appeared to work with numeric values but
did not in fact produce consistent results. This commit fixes the issue
by skipping validation altogether when a numeric value is used.

BREAKING CHANGES:

* The `minLength` and `maxLength` validators now verify that a value has
numeric `length` property and invoke validation only if that's the case.
Previously, falsey values without the length property (such as `0` or
`false` values) were triggering validation errors. If your code relies on
the old behavior, you can include other validators such as [min][1] or
[requiredTrue][2] to the list of validators for a particular field.

[1]: https://angular.io/api/forms/Validators#min
[2]: https://angular.io/api/forms/Validators#requiredTrue

Closes angular#35591
@sonukapoor
Copy link
Contributor Author

@kara I have changed the message.

@AndrewKushnir All done and rebased.

@AndrewKushnir AndrewKushnir removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews action: presubmit The PR is in need of a google3 presubmit labels May 5, 2020
@AndrewKushnir
Copy link
Contributor

Thanks for the updates @sonukapoor 👍

FYI, presubmits went well, so I'm marking this PR as ready for merge.

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker risk: low labels May 5, 2020
@alxhub alxhub closed this in 88a235d May 5, 2020
@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 5, 2020
profanis pushed a commit to profanis/angular that referenced this pull request Sep 5, 2020
…36157)

Previously, the behavior of the `minLength` and `maxLength` validators
caused confusion, as they appeared to work with numeric values but
did not in fact produce consistent results. This commit fixes the issue
by skipping validation altogether when a numeric value is used.

BREAKING CHANGES:

* The `minLength` and `maxLength` validators now verify that a value has
numeric `length` property and invoke validation only if that's the case.
Previously, falsey values without the length property (such as `0` or
`false` values) were triggering validation errors. If your code relies on
the old behavior, you can include other validators such as [min][1] or
[requiredTrue][2] to the list of validators for a particular field.

[1]: https://angular.io/api/forms/Validators#min
[2]: https://angular.io/api/forms/Validators#requiredTrue

Closes angular#35591

PR Close angular#36157
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 breaking changes cla: yes risk: low target: major This PR is targeted for the next major release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

minLength(1) validator returns invalid on a valid input
5 participants