Skip to content

Commit 64c6d39

Browse files
committed
fix(ngModel): treat synchronous validators as boolean always
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.
1 parent 5ce3053 commit 64c6d39

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

test/ng/directive/ngModelSpec.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,12 @@ describe('ngModel', function() {
848848
});
849849

850850
it('should treat all responses as boolean for synchronous validators', function() {
851-
// var curry = function(v) {
852-
// return function() {
853-
// return v;
854-
// };
855-
// };
856-
857-
var expectValid = function(v, e) {
851+
var expectValid = function(value, expected) {
858852
ctrl.$modelValue = undefined;
859-
ctrl.$validators.a = valueFn(v);
853+
ctrl.$validators.a = valueFn(value);
860854

861855
ctrl.$validate();
862-
expect(ctrl.$valid).toBe(e);
856+
expect(ctrl.$valid).toBe(expected);
863857
};
864858

865859
// False tests

0 commit comments

Comments
 (0)