Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make *length validations work with array #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions dist/backbone-validation-amd-min.js

Large diffs are not rendered by default.

34 changes: 25 additions & 9 deletions dist/backbone-validation-amd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Backbone.Validation v0.11.5
//
// Copyright (c) 2011-2015 Thomas Pedersen
// Copyright (c) 2011-2016 Thomas Pedersen
// Distributed under MIT License
//
// Documentation and full license available at:
Expand Down Expand Up @@ -486,6 +486,10 @@
minLength: '{0} must be at least {1} characters',
maxLength: '{0} must be at most {1} characters',
rangeLength: '{0} must be between {1} and {2} characters',
lengthArray: '{0} must contain {1} elements',
minLengthArray: '{0} must contain at least {1} elements',
maxLengthArray: '{0} must contain at most {1} elements',
rangeLengthArray: '{0} must contain between {1} and {2} elements',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
digits: '{0} must only contain digits',
Expand Down Expand Up @@ -645,35 +649,47 @@
// Validates that the value has to be a string with length equal to
// the length value specified
length: function(value, attr, length, model) {
if (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length !== length) {
return this.format(isArray ? defaultMessages.lengthArray : defaultMessages.length, this.formatLabel(attr, model), length);
}
},

// Min length validator
// 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 (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < minLength) {
return this.format(isArray ? defaultMessages.minLengthArray : defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},

// Max length validator
// 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 (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length > maxLength) {
return this.format(isArray ? defaultMessages.maxLengthArray : defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},

// Range length validator
// 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 (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < range[0] || value.length > range[1]) {
return this.format(isArray ? defaultMessages.rangeLengthArray : defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},

Expand Down
4 changes: 2 additions & 2 deletions dist/backbone-validation-min.js

Large diffs are not rendered by default.

34 changes: 25 additions & 9 deletions dist/backbone-validation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Backbone.Validation v0.11.5
//
// Copyright (c) 2011-2015 Thomas Pedersen
// Copyright (c) 2011-2016 Thomas Pedersen
// Distributed under MIT License
//
// Documentation and full license available at:
Expand Down Expand Up @@ -479,6 +479,10 @@ Backbone.Validation = (function(_){
minLength: '{0} must be at least {1} characters',
maxLength: '{0} must be at most {1} characters',
rangeLength: '{0} must be between {1} and {2} characters',
lengthArray: '{0} must contain {1} elements',
minLengthArray: '{0} must contain at least {1} elements',
maxLengthArray: '{0} must contain at most {1} elements',
rangeLengthArray: '{0} must contain between {1} and {2} elements',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
digits: '{0} must only contain digits',
Expand Down Expand Up @@ -638,35 +642,47 @@ 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 (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length !== length) {
return this.format(isArray ? defaultMessages.lengthArray : defaultMessages.length, this.formatLabel(attr, model), length);
}
},

// Min length validator
// 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 (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < minLength) {
return this.format(isArray ? defaultMessages.minLengthArray : defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},

// Max length validator
// 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 (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length > maxLength) {
return this.format(isArray ? defaultMessages.maxLengthArray : defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},

// Range length validator
// 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 (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < range[0] || value.length > range[1]) {
return this.format(isArray ? defaultMessages.rangeLengthArray : defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},

Expand Down
32 changes: 24 additions & 8 deletions src/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ Backbone.Validation = (function(_){
minLength: '{0} must be at least {1} characters',
maxLength: '{0} must be at most {1} characters',
rangeLength: '{0} must be between {1} and {2} characters',
lengthArray: '{0} must contain {1} elements',
minLengthArray: '{0} must contain at least {1} elements',
maxLengthArray: '{0} must contain at most {1} elements',
rangeLengthArray: '{0} must contain between {1} and {2} elements',
oneOf: '{0} must be one of: {1}',
equalTo: '{0} must be the same as {1}',
digits: '{0} must only contain digits',
Expand Down Expand Up @@ -631,35 +635,47 @@ 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 (!_.isString(value) || value.length !== length) {
return this.format(defaultMessages.length, this.formatLabel(attr, model), length);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length !== length) {
return this.format(isArray ? defaultMessages.lengthArray : defaultMessages.length, this.formatLabel(attr, model), length);
}
},

// Min length validator
// 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 (!_.isString(value) || value.length < minLength) {
return this.format(defaultMessages.minLength, this.formatLabel(attr, model), minLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < minLength) {
return this.format(isArray ? defaultMessages.minLengthArray : defaultMessages.minLength, this.formatLabel(attr, model), minLength);
}
},

// Max length validator
// 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 (!_.isString(value) || value.length > maxLength) {
return this.format(defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length > maxLength) {
return this.format(isArray ? defaultMessages.maxLengthArray : defaultMessages.maxLength, this.formatLabel(attr, model), maxLength);
}
},

// Range length validator
// 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 (!_.isString(value) || value.length < range[0] || value.length > range[1]) {
return this.format(defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
var isString = _.isString(value);
var isArray = _.isArray(value);

if ((!isString && !isArray) || value.length < range[0] || value.length > range[1]) {
return this.format(isArray ? defaultMessages.rangeLengthArray : defaultMessages.rangeLength, this.formatLabel(attr, model), range[0], range[1]);
}
},

Expand Down
26 changes: 26 additions & 0 deletions tests/validators/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ buster.testCase("length validator", {
this.model.set({postalCode:''}, {validate: true});
},

"has default error message for array": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({postalCode: 'Postal code must contain 2 elements'}, error);
done();
});
this.model.set({postalCode:[]}, {validate: true});
},

"string with length shorter than length is invalid": function() {
refute(this.model.set({
postalCode: 'a'
Expand Down Expand Up @@ -58,6 +66,24 @@ buster.testCase("length validator", {
}, {validate: true}));
},

"array with length shorter than length is invalid": function() {
refute(this.model.set({
postalCode: ['a']
}, {validate: true}));
},

"array with length longer than length is invalid": function() {
refute(this.model.set({
postalCode: ['a', 'a', 'a']
}, {validate: true}));
},

"array with length equal to length is valid": function() {
assert(this.model.set({
postalCode: ['a', 'a']
}, {validate: true}));
},

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

"string with length longer than maxLenght is invalid": function() {
"has default error message for array": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must contain at most 2 elements'}, error);
done();
});
this.model.set({name:['a','a','a']}, {validate: true});
},

"string with length longer than maxLength is invalid": function() {
refute(this.model.set({
name: 'aaa'
}, {validate: true}));
Expand Down Expand Up @@ -58,6 +66,24 @@ buster.testCase("maxLength validator", {
}, {validate: true}));
},

"array with length longer than maxLength is invalid": function() {
refute(this.model.set({
name: ['a', 'a', 'a']
}, {validate: true}));
},

"array with length equal to maxLength is valid": function() {
assert(this.model.set({
name: ['a', 'a']
}, {validate: true}));
},

"array with length shorter than maxLength is valid": function() {
assert(this.model.set({
name: ['a']
}, {validate: true}));
},

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

"string with length shorter than minLenght is invalid": function() {
"has default error message for array": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must contain at least 2 elements'}, error);
done();
});
this.model.set({name:[]}, {validate: true});
},

"string with length shorter than minLength is invalid": function() {
refute(this.model.set({
name: 'a'
}, {validate: true}));
Expand Down Expand Up @@ -58,6 +66,24 @@ buster.testCase("minLength validator", {
}, {validate: true}));
},

"array with length shorter than minLength is invalid": function() {
refute(this.model.set({
name: ['a']
}, {validate: true}));
},

"array with length equal to minLength is valid": function() {
assert(this.model.set({
name: ['a', 'a']
}, {validate: true}));
},

"array with length greater than minLength is valid": function() {
assert(this.model.set({
name: ['a', 'a', 'a', 'a']
}, {validate: true}));
},

"when required is not specified": {
"undefined is invalid": function() {
refute(this.model.set({
Expand Down
38 changes: 38 additions & 0 deletions tests/validators/rangeLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ buster.testCase("rangeLength validator", {
this.model.set({name:'a'}, {validate: true});
},

"has default error message for arrays": function(done) {
this.model.bind('validated:invalid', function(model, error){
assert.equals({name: 'Name must contain between 2 and 4 elements'}, error);
done();
});
this.model.set({name:['a']}, {validate: true});
},

"string with length shorter than first value is invalid": function() {
refute(this.model.set({
name: 'a'
Expand Down Expand Up @@ -58,6 +66,36 @@ buster.testCase("rangeLength validator", {
}, {validate: true}));
},

"array with length shorter than first value is invalid": function() {
refute(this.model.set({
name: ['a']
}, {validate: true}));
},

"array with length equal to first value is valid": function() {
assert(this.model.set({
name: ['a','a']
}, {validate: true}));
},

"array with length longer than last value is invalid": function() {
refute(this.model.set({
name: ['a', 'a', 'a', 'a', 'a']
}, {validate: true}));
},

"array with length equal to last value is valid": function() {
assert(this.model.set({
name: ['a', 'a', 'a', 'a']
}, {validate: true}));
},

"array with length within range is valid": function() {
assert(this.model.set({
name: ['a', 'a', 'a']
}, {validate: true}));
},

"spaces are treated as part of the string (no trimming)": function() {
refute(this.model.set({
name: 'aaaa '
Expand Down