Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Patch 14734 boolean sync validators #15208

Closed

Conversation

BobChao87
Copy link
Contributor

@BobChao87 BobChao87 commented Oct 3, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Fix/clarification of how synchronous validators work.

What is the current behavior? (You can also link to an open issue here)
#14734 All values except false literal and undefined are treated as a pass. undefined is treated as pending, which makes no sense for synchronous operation.

What is the new behavior (if this is a feature change)?

All falsy value returns to a synchronous validator are treated as fails, including undefined.

Does this PR introduce a breaking change?

Yes, see commit message.

Please check if the PR fulfills these requirements

Other information:

I believe a change to docs in unnecessary. We still expect true and false to be the primary returns and don't want to encourage otherwise. This is mostly clarifying a niche, undocumented, edge-case to behave more intuitively.

Change synchronous validators to convert the return to boolean value.
Prevent unexpected behavior when returning `undefined`.

Closes angular#14734
BREAKING CHANGE: Previously, only a literal `false` return would resolve as the
synchronous validator failing. Now, all traditionally false JavaScript values
are treated as failing the validator, as one would naturally expect.

To migrate, make sure your synchronous validators are returning either a
literal `true` or a literal `false` value. For most code, we expect this to
already be the case. Only a very small subset of projects will affected.

Namely, anyone using `undefined` or any falsy value as a return will now see
their validation failing, whereas previously falsy values other than `undefined`
would have been seen as passing and `undefined` would have been seen as pending.
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

Apart from a couple of nitpicks, LGTM.

I would add a little more details to the commit message (regarding what was happening before) - can be done while merging.

@@ -847,6 +847,38 @@ describe('ngModel', function() {
expect(ctrl.$valid).toBe(true);
});

it('should treat all responses as boolean for synchronous validators', function() {
var curry = function(v) {
Copy link
Member

@gkalpak gkalpak Oct 3, 2016

Choose a reason for hiding this comment

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

You don't need a new function for this. You can use the internal helper: valueFn()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, I'll make the switch. I was actually hoping to talk about the curry() anyways, because it's now used three times in this file (hence why I used it in the first place). I'll submit a separate PR to fix the others.

Copy link
Member

Choose a reason for hiding this comment

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

Good catch! Feel free to change all other instances too.

};
};

var tester = function(v, e) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I would prefer a more descriptive name, such as expectValid().

Change synchronous validators to convert the return to boolean value.
Prevent unexpected behavior when returning `undefined`.

Closes angular#14734
BREAKING CHANGE: Previously, only a literal `false` return would resolve as the
synchronous validator failing. Now, all traditionally false JavaScript values
are treated as failing the validator, as one would naturally expect.

Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the
empty string) used to considered valid (passing) and they are now considered
invalid (failing). The value `undefined` was treated similarly to a pending
asynchronous validator, causing the validation to be pending. `undefined` is
also now considered invalid.

To migrate, make sure your synchronous validators are returning either a
literal `true` or a literal `false` value. For most code, we expect this to
already be the case. Only a very small subset of projects will be affected.

Namely, anyone using `undefined` or any falsy value as a return will now see
their validation failing, whereas previously falsy values other than `undefined`
would have been seen as passing and `undefined` would have been seen as pending.
Change synchronous validators to convert the return to boolean value.
Prevent unexpected behavior when returning `undefined`.

Closes angular#14734
BREAKING CHANGE: Previously, only a literal `false` return would resolve as the
synchronous validator failing. Now, all traditionally false JavaScript values
are treated as failing the validator, as one would naturally expect.

Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the
empty string) used to considered valid (passing) and they are now considered
invalid (failing). The value `undefined` was treated similarly to a pending
asynchronous validator, causing the validation to be pending. `undefined` is
also now considered invalid.

To migrate, make sure your synchronous validators are returning either a
literal `true` or a literal `false` value. For most code, we expect this to
already be the case. Only a very small subset of projects will be affected.

Namely, anyone using `undefined` or any falsy value as a return will now see
their validation failing, whereas previously falsy values other than `undefined`
would have been seen as passing and `undefined` would have been seen as pending.
Change synchronous validators to convert the return to boolean value.
Prevent unexpected behavior when returning `undefined`.

Closes angular#14734
BREAKING CHANGE: Previously, only a literal `false` return would resolve as the
synchronous validator failing. Now, all traditionally false JavaScript values
are treated as failing the validator, as one would naturally expect.

Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the
empty string) used to considered valid (passing) and they are now considered
invalid (failing). The value `undefined` was treated similarly to a pending
asynchronous validator, causing the validation to be pending. `undefined` is
also now considered invalid.

To migrate, make sure your synchronous validators are returning either a
literal `true` or a literal `false` value. For most code, we expect this to
already be the case. Only a very small subset of projects will be affected.

Namely, anyone using `undefined` or any falsy value as a return will now see
their validation failing, whereas previously falsy values other than `undefined`
would have been seen as passing and `undefined` would have been seen as pending.
@BobChao87
Copy link
Contributor Author

Hopefully those changes to the commit message are in the direction you wanted.

Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

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

LGTM

@petebacondarwin
Copy link
Contributor

Landed thanks.

ellimist pushed a commit to ellimist/angular.js that referenced this pull request Mar 15, 2017
Change synchronous validators to convert the return to boolean value.
Prevent unexpected behavior when returning `undefined`.

Closes angular#14734
Closes angular#15208

BREAKING CHANGE: Previously, only a literal `false` return would resolve as the
synchronous validator failing. Now, all traditionally false JavaScript values
are treated as failing the validator, as one would naturally expect.

Specifically, the values `0` (the number zero), `null`, `NaN` and `''` (the
empty string) used to considered valid (passing) and they are now considered
invalid (failing). The value `undefined` was treated similarly to a pending
asynchronous validator, causing the validation to be pending. `undefined` is
also now considered invalid.

To migrate, make sure your synchronous validators are returning either a
literal `true` or a literal `false` value. For most code, we expect this to
already be the case. Only a very small subset of projects will be affected.

Namely, anyone using `undefined` or any falsy value as a return will now see
their validation failing, whereas previously falsy values other than `undefined`
would have been seen as passing and `undefined` would have been seen as pending.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants