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

Commit 7bc71ad

Browse files
BobChao87petebacondarwin
authored andcommitted
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 #14734 Closes #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.
1 parent e8aebb3 commit 7bc71ad

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/ng/directive/ngModel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ NgModelController.prototype = {
603603
function processSyncValidators() {
604604
var syncValidatorsValid = true;
605605
forEach(that.$validators, function(validator, name) {
606-
var result = validator(modelValue, viewValue);
606+
var result = Boolean(validator(modelValue, viewValue));
607607
syncValidatorsValid = syncValidatorsValid && result;
608608
setValidity(name, result);
609609
});

test/ng/directive/ngModelSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,32 @@ 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 expectValid = function(value, expected) {
852+
ctrl.$modelValue = undefined;
853+
ctrl.$validators.a = valueFn(value);
854+
855+
ctrl.$validate();
856+
expect(ctrl.$valid).toBe(expected);
857+
};
858+
859+
// False tests
860+
expectValid(false, false);
861+
expectValid(undefined, false);
862+
expectValid(null, false);
863+
expectValid(0, false);
864+
expectValid(NaN, false);
865+
expectValid('', false);
866+
867+
// True tests
868+
expectValid(true, true);
869+
expectValid(1, true);
870+
expectValid('0', true);
871+
expectValid('false', true);
872+
expectValid([], true);
873+
expectValid({}, true);
874+
});
875+
850876

851877
it('should register invalid validations on the $error object', function() {
852878
var curry = function(v) {

0 commit comments

Comments
 (0)