Skip to content

Commit 7e4bf60

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 1a1d0fb commit 7e4bf60

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

test/ng/directive/ngModelSpec.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -847,36 +847,36 @@ describe('ngModel', function() {
847847
expect(ctrl.$valid).toBe(true);
848848
});
849849

850-
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 tester = function(v, e) {
850+
fit('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) {
858858
ctrl.$modelValue = undefined;
859-
ctrl.$validators.a = curry(v);
859+
ctrl.$validators.a = valueFn(v);
860860

861861
ctrl.$validate();
862862
expect(ctrl.$valid).toBe(e);
863863
};
864864

865865
// False tests
866-
tester(false, false);
867-
tester(undefined, false);
868-
tester(null, false);
869-
tester(0, false);
870-
tester(NaN, false);
871-
tester('', false);
866+
expectValid(false, false);
867+
expectValid(undefined, false);
868+
expectValid(null, false);
869+
expectValid(0, false);
870+
expectValid(NaN, false);
871+
expectValid('', false);
872872

873873
// True tests
874-
tester(true, true);
875-
tester(1, true);
876-
tester('0', true);
877-
tester('false', true);
878-
tester([], true);
879-
tester({}, true);
874+
expectValid(true, true);
875+
expectValid(1, true);
876+
expectValid('0', true);
877+
expectValid('false', true);
878+
expectValid([], true);
879+
expectValid({}, true);
880880
});
881881

882882

0 commit comments

Comments
 (0)