Skip to content

Commit

Permalink
Merge pull request validatorjs#47 from foxbunny/master
Browse files Browse the repository at this point in the history
#isDivisibleBy()
  • Loading branch information
chriso committed Nov 24, 2011
2 parents 4ee9379 + 40b7c20 commit ce636da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/validator.js
Expand Up @@ -130,6 +130,13 @@ Validator.prototype.isFloat = function() {
return this.isDecimal();
}

Validator.prototype.isDivisibleBy = function(n) {
if (parseFloat(this.str) % n) {
return this.error(this.msg || 'Not divisible by ' + n);
}
return this;
}

Validator.prototype.notNull = function() {
if (this.str === '') {
return this.error(this.msg || 'Invalid characters');
Expand Down Expand Up @@ -273,6 +280,7 @@ Validator.prototype.notIn = function(options) {

Validator.prototype.min = function(val) {
var number = parseFloat(this.str);

if (!isNaN(number) && number < val) {
return this.error(this.msg || 'Invalid number');
}
Expand All @@ -281,6 +289,7 @@ Validator.prototype.min = function(val) {

Validator.prototype.max = function(val) {
var number = parseFloat(this.str);

if (!isNaN(number) && number > val) {
return this.error(this.msg || 'Invalid number');
}
Expand Down
27 changes: 26 additions & 1 deletion test/validator.test.js
Expand Up @@ -479,6 +479,19 @@ module.exports = {
}, /invalid/i
);
},

'test #isDate()': function() {
assert.ok(Validator.check('2011-08-04').isDate());
assert.ok(Validator.check('04. 08. 2011.').isDate());
assert.ok(Validator.check('08/04/2011').isDate());
assert.ok(Validator.check('2011.08.04').isDate());
assert.ok(Validator.check('4. 8. 2011. GMT').isDate());
assert.ok(Validator.check('2011-08-04 12:00').isDate());

assert.throws(Validator.check('foo').isDate);
assert.throws(Validator.check('2011-foo-04').isDate);
assert.throws(Validator.check('GMT').isDate);
},

'test #min()': function() {
assert.ok(Validator.check('4').min(2));
Expand All @@ -492,7 +505,6 @@ module.exports = {
assert.throws(function() {
Validator.check('5.1').min(5.11);
});

},

'test #max()': function() {
Expand Down Expand Up @@ -575,5 +587,18 @@ module.exports = {
assert.throws(function() {
Validator.check(f.tomorrow).isBefore();
});
},

'test #isDivisibleBy()': function() {
assert.ok(Validator.check('10').isDivisibleBy(2));
assert.ok(Validator.check('6').isDivisibleBy(3));

assert.throws(function() {
Validator.check('5').isDivisibleBy(2);
});

assert.throws(function() {
Validator.check('6.7').isDivisibleBy(3);
});
}
}

0 comments on commit ce636da

Please sign in to comment.