Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 3a44bed

Browse files
committed
Merge pull request #19 from bluebilt/master
Avoid corrupting the NgModelController and the FormController when ui-validator returns undefined
2 parents 6ef6a17 + 87d8197 commit 3a44bed

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/validate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ angular.module('ui.validate',[])
6565
// Return as valid for now. Validity is updated when promise resolves.
6666
return true;
6767
} else {
68-
return expression;
68+
return !!expression; // Transform 'undefined' to false (to avoid corrupting the NgModelController and the FormController)
6969
}
7070
};
7171
ctrl.$validators[key] = validateFn;

test/validateSpec.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('uiValidate', function () {
1515
return valueToValidate;
1616
};
1717

18+
var undefinedValidator = function () {
19+
return undefined;
20+
};
21+
1822
beforeEach(module('ui.validate'));
1923
beforeEach(inject(function ($rootScope, $compile) {
2024

@@ -36,17 +40,27 @@ describe('uiValidate', function () {
3640

3741
scope.validate = trueValidator;
3842
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
39-
expect(scope.form.input.$valid).toBeTruthy();
43+
expect(scope.form.input.$valid).toBe(true);
4044
expect(scope.form.input.$error).toEqual({});
4145
}));
4246

4347
it('should mark input as invalid if initial model is invalid', inject(function () {
4448

4549
scope.validate = falseValidator;
4650
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
47-
expect(scope.form.input.$valid).toBeFalsy();
51+
expect(scope.form.input.$valid).toBe(false);
4852
expect(scope.form.input.$error).toEqual({ validator: true });
4953
}));
54+
55+
it('should not corrupt the NgModelController and the FormController if the validator returns undefined', inject(function () {
56+
57+
scope.validate = undefinedValidator;
58+
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
59+
expect(scope.form.input.$valid).toBe(false);
60+
expect(scope.form.$valid).toBe(false);
61+
expect(scope.form.input.$error).toEqual({ validator: true });
62+
}));
63+
5064
});
5165

5266
describe('validation on model change', function () {
@@ -56,10 +70,10 @@ describe('uiValidate', function () {
5670
scope.value = false;
5771
scope.validate = passedValueValidator;
5872
compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
59-
expect(scope.form.input.$valid).toBeFalsy();
73+
expect(scope.form.input.$valid).toBe(false);
6074

6175
scope.$apply('value = true');
62-
expect(scope.form.input.$valid).toBeTruthy();
76+
expect(scope.form.input.$valid).toBe(true);
6377
}));
6478
});
6579

@@ -75,12 +89,12 @@ describe('uiValidate', function () {
7589
scope.value = false;
7690
scope.validate = passedValueValidator;
7791
var inputElm = compileAndDigest('<input name="input" ng-model="value" ui-validate="\'validate($value)\'">', scope);
78-
expect(scope.form.input.$valid).toBeFalsy();
92+
expect(scope.form.input.$valid).toBe(false);
7993

8094
inputElm.val('true');
8195
inputElm.trigger((sniffer.hasEvent('input') ? 'input' : 'change'));
8296

83-
expect(scope.form.input.$valid).toBeTruthy();
97+
expect(scope.form.input.$valid).toBe(true);
8498
});
8599
});
86100

@@ -92,9 +106,9 @@ describe('uiValidate', function () {
92106
scope.validate2 = falseValidator;
93107

94108
compileAndDigest('<input name="input" ng-model="value" ui-validate="{key1 : \'validate1($value)\', key2 : \'validate2($value)\'}">', scope);
95-
expect(scope.form.input.$valid).toBeFalsy();
96-
expect(scope.form.input.$error.key1).toBeFalsy();
97-
expect(scope.form.input.$error.key2).toBeTruthy();
109+
expect(scope.form.input.$valid).toBe(false);
110+
expect(scope.form.input.$error.key1).toBeUndefined();
111+
expect(scope.form.input.$error.key2).toBe(true);
98112
});
99113
});
100114

0 commit comments

Comments
 (0)