Skip to content

Commit

Permalink
Merge pull request #498 from Knockout-Contrib/issue-277
Browse files Browse the repository at this point in the history
Fix #277 - parseInputAttributes option may duplicate rules when enabled
  • Loading branch information
crissdev committed Jan 7, 2015
2 parents 9b1ef1e + cdce5c3 commit 1bfb924
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,14 @@ kv.configuration = configuration;
addRule: function (observable, rule) {
observable.extend({ validatable: true });

//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
return item.rule && item.rule === rule.rule;
});

if (!hasRule) {
//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
}
return observable;
},

Expand Down
2 changes: 1 addition & 1 deletion Dist/knockout.validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Dist/knockout.validation.min.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,14 @@
addRule: function (observable, rule) {
observable.extend({ validatable: true });

//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
var hasRule = !!ko.utils.arrayFirst(observable.rules(), function(item) {
return item.rule && item.rule === rule.rule;
});

if (!hasRule) {
//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
}
return observable;
},

Expand Down
21 changes: 21 additions & 0 deletions Tests/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ QUnit.test('clearError clears automatic errors', function(assert) {

//#endregion

//region API Tests

QUnit.module('API Tests');

QUnit.test('Issue #277 - addRule should ignore rule if already defined', function(assert) {

var testObj = ko.observable(1).extend({validatable: true});

testObj.extend({min: 10});
assert.equal(testObj.rules().length, 1, 'rule is added');
assert.equal(testObj.error(), 'Please enter a value greater than or equal to 10.');

testObj.extend({min: 20});
ko.validation.validateObservable(testObj);

assert.equal(testObj.rules().length, 1, 'rule is not added');
assert.equal(testObj.error(), 'Please enter a value greater than or equal to 10.');
});

//endregion

//#region Grouping Tests

QUnit.module('Grouping Tests');
Expand Down
40 changes: 40 additions & 0 deletions Tests/validation-ui-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,46 @@ QUnit.test('selectedOptions Binding Works', function(assert) {
assert.equal(msg, 'Please select at least one item.', msg);
});

QUnit.test('Issue #277 - parseInputAttributes does not duplicate rules when parseInputAttributes=true', function(assert) {
var done = assert.async();

ko.validation.init({parseInputAttributes: true, writeInputAttributes: false}, true);

var testObj = ko.observable('').extend({required: true, email: true});
addTestHtml('<input type="email" required="required" required data-bind="value: email" />');
applyTestBindings({ email: testObj });

setTimeout(function() {
assert.equal(testObj.rules().length, 2, 'rules are not duplicated');
assert.equal(testObj.error(), 'This field is required.');

testObj('abc');
assert.equal(testObj.error(), 'Please enter a proper email address.');

done();
}, 1);
});

QUnit.test('Issue #277 - parseInputAttributes does not duplicate rules when parseInputAttributes=true', function(assert) {
var done = assert.async();

ko.validation.init({parseInputAttributes: true, writeInputAttributes: true}, true);

var testObj = ko.observable('').extend({required: true, email: true});
addTestHtml('<input type="email" required="required" required data-bind="value: email" />');
applyTestBindings({ email: testObj });

setTimeout(function() {
assert.equal(testObj.rules().length, 2, 'rules are not duplicated');
assert.equal(testObj.error(), 'This field is required.');

testObj('abc');
assert.equal(testObj.error(), 'Please enter a proper email address.');

done();
}, 1);
});

//#region Inserting Messages

QUnit.test('Inserting Messages Works', function(assert) {
Expand Down

0 comments on commit 1bfb924

Please sign in to comment.