Skip to content

Commit

Permalink
length, maxLength, minLength and rangeLength validators no longer sec…
Browse files Browse the repository at this point in the history
…retly trims the string. Fixes #134
  • Loading branch information
thedersen committed Oct 17, 2013
1 parent a23d796 commit 65d2126
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,8 @@ Basic behaviour:
#### Master
* Fixed undefined format function when calling one of the built in validators form within a method validator. Fixes #98
* BREAKING: Added ability to set error message per pattern. This means that if you have custom patterns, or have change the default one, you need to [add/change a default message](http://thedersen.com/projects/backbone-validation/#extending-backbone-validation/adding-custom-patterns) for it.
* BREAKING: Added ability to set error message per pattern. This means that if you have custom patterns, or have change the default one, you need to [add/change a default message](http://thedersen.com/projects/backbone-validation/#extending-backbone-validation/adding-custom-patterns) for it. Fixes #174
* BREAKING: length, maxLength, minLength and rangeLength validators no longer secretly trims the string. Fixes #134
#### v0.8.2 [commits](https://github.com/thedersen/backbone.validation/compare/v0.8.1...v0.8.2)
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-amd-min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/backbone-validation-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@
// Validates that the value has to be a string with length equal to
// the length value specified
length: function(value, attr, length, model) {
if (!hasValue(value) || trim(value).length !== length) {
if (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
}
},
Expand All @@ -579,7 +579,7 @@
// Validates that the value has to be a string with length equal to or greater than
// the min length value specified
minLength: function(value, attr, minLength, model) {
if (!hasValue(value) || trim(value).length < minLength) {
if (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},
Expand All @@ -588,7 +588,7 @@
// Validates that the value has to be a string with length equal to or less than
// the max length value specified
maxLength: function(value, attr, maxLength, model) {
if (!hasValue(value) || trim(value).length > maxLength) {
if (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},
Expand All @@ -597,7 +597,7 @@
// Validates that the value has to be a string and equal to or between
// the two numbers specified
rangeLength: function(value, attr, range, model) {
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) {
if (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},
Expand Down
2 changes: 1 addition & 1 deletion dist/backbone-validation-min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to
// the length value specified
length: function(value, attr, length, model) {
if (!hasValue(value) || trim(value).length !== length) {
if (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
}
},
Expand All @@ -572,7 +572,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to or greater than
// the min length value specified
minLength: function(value, attr, minLength, model) {
if (!hasValue(value) || trim(value).length < minLength) {
if (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},
Expand All @@ -581,7 +581,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to or less than
// the max length value specified
maxLength: function(value, attr, maxLength, model) {
if (!hasValue(value) || trim(value).length > maxLength) {
if (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},
Expand All @@ -590,7 +590,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string and equal to or between
// the two numbers specified
rangeLength: function(value, attr, range, model) {
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) {
if (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},
Expand Down
8 changes: 4 additions & 4 deletions src/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to
// the length value specified
length: function(value, attr, length, model) {
if (!hasValue(value) || trim(value).length !== length) {
if (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
}
},
Expand All @@ -565,7 +565,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to or greater than
// the min length value specified
minLength: function(value, attr, minLength, model) {
if (!hasValue(value) || trim(value).length < minLength) {
if (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},
Expand All @@ -574,7 +574,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string with length equal to or less than
// the max length value specified
maxLength: function(value, attr, maxLength, model) {
if (!hasValue(value) || trim(value).length > maxLength) {
if (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},
Expand All @@ -583,7 +583,7 @@ Backbone.Validation = (function(_){
// Validates that the value has to be a string and equal to or between
// the two numbers specified
rangeLength: function(value, attr, range, model) {
if(!hasValue(value) || trim(value).length < range[0] || trim(value).length > range[1]) {
if (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},
Expand Down
14 changes: 13 additions & 1 deletion tests/validators/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buster.testCase("length validator", {
});
},

"has default error message": function(done) {
"has default error message for string": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({postalCode: 'Postal code must be 2 characters'}, error);
done();
Expand All @@ -46,6 +46,18 @@ buster.testCase("length validator", {
}, {validate: true}));
},

"spaces are treated as part of the string (no trimming)": function() {
refute(this.model.set({
postalCode: 'aa '
}, {validate: true}));
},

"non strings are treated as an error": function() {
refute(this.model.set({
postalCode: 123
}, {validate: true}));
},

"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
Expand Down
14 changes: 13 additions & 1 deletion tests/validators/maxLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buster.testCase("maxLength validator", {
});
},

"has default error message": function(done) {
"has default error message for string": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must be at most 2 characters'}, error);
done();
Expand All @@ -46,6 +46,18 @@ buster.testCase("maxLength validator", {
}, {validate: true}));
},

"spaces are treated as part of the string (no trimming)": function() {
refute(this.model.set({
name: 'a '
}, {validate: true}));
},

"non strings are treated as an error": function() {
refute(this.model.set({
name: 123
}, {validate: true}));
},

"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
Expand Down
14 changes: 13 additions & 1 deletion tests/validators/minLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buster.testCase("minLength validator", {
});
},

"has default error message": function(done) {
"has default error message for string": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must be at least 2 characters'}, error);
done();
Expand All @@ -46,6 +46,18 @@ buster.testCase("minLength validator", {
}, {validate: true}));
},

"spaces are treated as part of the string (no trimming)": function() {
assert(this.model.set({
name: 'a '
}, {validate: true}));
},

"non strings are treated as an error": function() {
refute(this.model.set({
name: 123
}, {validate: true}));
},

"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
Expand Down
14 changes: 13 additions & 1 deletion tests/validators/rangeLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ buster.testCase("rangeLength validator", {
});
},

"has default error message": function(done) {
"has default error message for strings": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must be between 2 and 4 characters'}, error);
done();
Expand Down Expand Up @@ -58,6 +58,18 @@ buster.testCase("rangeLength validator", {
}, {validate: true}));
},

"spaces are treated as part of the string (no trimming)": function() {
refute(this.model.set({
name: 'aaaa '
}, {validate: true}));
},

"non strings are treated as an error": function() {
refute(this.model.set({
name: 123
}, {validate: true}));
},

"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
Expand Down

0 comments on commit 65d2126

Please sign in to comment.