diff --git a/lib/matchers/number.js b/lib/matchers/number.js index 3f5f15f..1824bfa 100644 --- a/lib/matchers/number.js +++ b/lib/matchers/number.js @@ -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)) @@ -7,9 +9,19 @@ var parseFloatFromString = function (value) { } module.exports = function (opts) { - if (!opts) opts = {}; + var hasMinValue = hasValue(opts.min); + var hasMaxValue = hasValue(opts.max); + + if(hasMinValue && typeof opts.min !== 'number') { + throw new Error('Invalid minimum option: ' + opts.min); + } + + if(hasMaxValue && typeof opts.max !== 'number') { + throw new Error('Invalid maximum option: ' + opts.max); + } + return s(function(value) { if (opts.parse) { @@ -21,13 +33,13 @@ module.exports = function (opts) { } if (opts) { - if (opts.min && opts.max && (value < opts.min || value > opts.max)) { + if (hasMinValue && hasMaxValue && (value < opts.min || value > opts.max)) { return 'should be a number between ' + opts.min + ' and ' + opts.max; } - if (opts.min && value < opts.min) { + if (hasMinValue && value < opts.min) { return 'should be a number >= ' + opts.min; } - if (opts.max && value > opts.max) { + if (hasMaxValue && value > opts.max) { return 'should be a number <= ' + opts.max; } } diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..2cbd76f --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,7 @@ +var hasValue = function(val){ + return (typeof val !== 'undefined') && (val !== null); +} + +module.exports = { + hasValue: hasValue +} diff --git a/test/matchers/number.spec.js b/test/matchers/number.spec.js index 1be0bc5..f49261e 100644 --- a/test/matchers/number.spec.js +++ b/test/matchers/number.spec.js @@ -16,10 +16,27 @@ 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/); }); + it('fails for invalid min or max values', function(){ + var shouldFail = function(val) { + (function(){ + number({min: val}); + }).should.throw('Invalid minimum option: ' + val); + + (function(){ + number({max: val}); + }).should.throw('Invalid maximum option: ' + val); + } + + var invalidValues = ['a', '', {}, []]; + invalidValues.forEach(shouldFail); + }); + it('can parse string into number', function() { number({parse: true})('', 0).should.not.have.error(); number({parse: true})('', 3).should.not.have.error(); diff --git a/test/util.spec.js b/test/util.spec.js new file mode 100644 index 0000000..875e310 --- /dev/null +++ b/test/util.spec.js @@ -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 ', function(){ + hasValue(null).should.be.false; + }); + + it('Should return false when value is ', 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; + }); +});