Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Commit

Permalink
Proper validation of min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi Valikhani committed Mar 18, 2015
1 parent 50aadc5 commit 7c48a60
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/matchers/number.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var s = require('../s');
var s = require('../s');
var utils = require('../utils');
var hasValue = utils.hasValue;

var parseFloatFromString = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?)$/.test(value))
Expand All @@ -21,13 +23,13 @@ module.exports = function (opts) {
}

if (opts) {
if (opts.min && opts.max && (value < opts.min || value > opts.max)) {
if (hasValue(opts.min) && hasValue(opts.max) && (value < opts.min || value > opts.max)) {
return 'should be a number between ' + opts.min + ' and ' + opts.max;
}
if (opts.min && value < opts.min) {
if (hasValue(opts.min) && value < opts.min) {
return 'should be a number >= ' + opts.min;
}
if (opts.max && value > opts.max) {
if (hasValue(opts.max) && value > opts.max) {
return 'should be a number <= ' + opts.max;
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var hasValue = function(val){
return (typeof val !== 'undefined') && (val !== null);
}

module.exports = {
hasValue: hasValue
}
2 changes: 2 additions & 0 deletions test/matchers/number.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('number matcher', function() {

it('supports min and max', function() {
number({min: 3})('', 0).should.have.error(/should be a number >= 3/);
number({min: 0})('', -10).should.have.error(/should be a number >= 0/);
number({max: 0})('', 12).should.have.error(/should be a number <= 0/);
number({max: 3})('', 5).should.have.error(/should be a number <= 3/);
number({min: 3, max: 5})('', 7).should.have.error(/should be a number between 3 and 5/);
});
Expand Down
28 changes: 28 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var utils = require('../lib/utils');
var hasValue = utils.hasValue;

describe("hasValue() util function", function(){
it('Should return false when value is <null>', function(){
hasValue(null).should.be.false;
});

it('Should return false when value is <undefined>', function(){
hasValue(undefined).should.be.false;
});

it('Should return true when value is a number', function(){
hasValue(12).should.be.true;
});

it('Should return true when value is a string', function(){
hasValue('a').should.be.true;
});

it('Should return true when value is <{}>', function(){
hasValue({}).should.be.true;
});

it('Should return true when value is <"">', function(){
hasValue('').should.be.true;
});
});

0 comments on commit 7c48a60

Please sign in to comment.