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
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ NgModelController.prototype = {
function processSyncValidators() {
var syncValidatorsValid = true;
forEach(that.$validators, function(validator, name) {
var result = validator(modelValue, viewValue);
var result = Boolean(validator(modelValue, viewValue));
syncValidatorsValid = syncValidatorsValid && result;
setValidity(name, result);
});
Expand Down
32 changes: 32 additions & 0 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

return function() {
return v;
};
};

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().

ctrl.$modelValue = undefined;
ctrl.$validators.a = curry(v);

ctrl.$validate();
expect(ctrl.$valid).toBe(e);
};

// False tests
tester(false, false);
tester(undefined, false);
tester(null, false);
tester(0, false);
tester(NaN, false);
tester('', false);

// True tests
tester(true, true);
tester(1, true);
tester('0', true);
tester('false', true);
tester([], true);
tester({}, true);
});


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