Skip to content

Commit

Permalink
Does not validate fields that are not in options.attributes but defin…
Browse files Browse the repository at this point in the history
…ed in the models
  • Loading branch information
blikblum committed Jan 26, 2015
1 parent 63c2514 commit 3cd4b53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/backbone-validation.js
Expand Up @@ -159,12 +159,12 @@ Backbone.Validation = (function(_){
// Loops through the model's attributes and validates them all.
// Returns and object containing names of invalid attributes
// as well as error messages.
var validateModel = function(model, attrs) {
var validateModel = function(model, attrs, validatedKeys) {
var error,
invalidAttrs = {},
isValid = true,
computed = _.clone(attrs),
flattened = flatten(attrs);
flattened = _.pick(flatten(attrs), validatedKeys);

_.each(flattened, function(val, attr) {
error = validateAttr(model, attr, val, computed);
Expand Down Expand Up @@ -239,7 +239,7 @@ Backbone.Validation = (function(_){
allAttrs = _.extend({}, validatedAttrs, model.attributes, attrs),
changedAttrs = flatten(attrs || allAttrs),

result = validateModel(model, allAttrs);
result = validateModel(model, allAttrs, _.keys(validatedAttrs));

model._isValid = result.isValid;

Expand Down
20 changes: 15 additions & 5 deletions tests/attributesOption.js
Expand Up @@ -17,6 +17,9 @@ buster.testCase("Setting options.attributes", {
},
password: {
required: true
},
email: {
pattern: 'email'
}
}
});
Expand Down Expand Up @@ -51,13 +54,15 @@ buster.testCase("Setting options.attributes", {
assert.defined(errors.name);
assert.defined(errors.age);
refute.defined(errors.password);
refute.defined(errors.email);
},

"when all the attributes in array are valid": {
setUp: function () {
this.model.set({
age: 1,
name: 'hello'
name: 'hello',
email: 'invalidemail'
});
},
"validation will pass": function () {
Expand All @@ -84,13 +89,15 @@ buster.testCase("Setting options.attributes", {
assert.defined(errors.name);
assert.defined(errors.age);
refute.defined(errors.password);
refute.defined(errors.email);
},

"when all the attributes returned by the function are valid": {
setUp: function () {
this.model.set({
age: 1,
name: 'hello'
name: 'hello',
email: 'invalidemail'
});
},
"validation will pass": function () {
Expand All @@ -115,6 +122,7 @@ buster.testCase("Setting options.attributes", {
assert.defined(errors.name);
refute.defined(errors.age);
refute.defined(errors.password);
refute.defined(errors.email);
},

"submit and buttons should not be included in attribute array": function () {
Expand All @@ -127,7 +135,8 @@ buster.testCase("Setting options.attributes", {
"when all the attributes present in form are valid": {
setUp: function () {
this.model.set({
name: 'hello'
name: 'hello',
email: 'invalidemail'
});
},
"validation will pass": function () {
Expand Down Expand Up @@ -159,10 +168,11 @@ buster.testCase("Setting options.attributes", {
refute.defined(errors.password);
},

"when all the attributes returned by registered the function are valid": {
"when all the attributes returned by the registered function are valid": {
setUp: function () {
this.model.set({
age: 1
age: 1,
email: 'invalidemail'
});
},
"validation will pass": function () {
Expand Down

0 comments on commit 3cd4b53

Please sign in to comment.