diff --git a/Document.js b/Document.js index 17d0bf1..40b4a1b 100644 --- a/Document.js +++ b/Document.js @@ -246,7 +246,7 @@ Document.prototype.hasAddress = model.hasChild( 'address_parts' ); // population Document.prototype.setPopulation = model.set( 'population', null, null ) .validate( valid.type('number') ) - .validate( valid.positive() ); + .validate( valid.nonnegative() ); Document.prototype.getPopulation = model.get( 'population' ); @@ -254,7 +254,7 @@ Document.prototype.getPopulation = model.get( 'population' ); Document.prototype.setPopularity = model.set( 'popularity', null, null ) .transform( transform.roundify() ) .validate( valid.type('number') ) - .validate( valid.positive() ); + .validate( valid.nonnegative() ); Document.prototype.getPopularity = model.get( 'popularity' ); diff --git a/test/run.js b/test/run.js index f27312f..5a135ee 100644 --- a/test/run.js +++ b/test/run.js @@ -26,6 +26,7 @@ var tests = [ require('./DocumentMapperStream.js'), require('./util/transform.js'), require('./util/model.js'), + require('./util/valid.js'), require('./serialize/test.js'), ]; diff --git a/test/util/valid.js b/test/util/valid.js new file mode 100644 index 0000000..3ca2d51 --- /dev/null +++ b/test/util/valid.js @@ -0,0 +1,32 @@ +const valid = require('../../util/valid'); + +module.exports.tests = {}; + +module.exports.tests.nonnegative = (test) => { + test('nonnegative should throw error if value is less than 0', (t) => { + t.throws(valid.nonnegative().bind(null, -1), /invalid document type, expecting: 0 or a positive number, got: -1/); + t.end(); + }); + + test('nonnegative should not throw error if value is 0', (t) => { + t.doesNotThrow(valid.nonnegative().bind(null, 0), '0 should be allowed'); + t.end(); + }); + + test('nonnegative should not throw error if value is greater than 0', (t) => { + t.doesNotThrow(valid.nonnegative().bind(null, 1), '1 should be allowed'); + t.end(); + }); + +}; + +module.exports.all = (tape, common) => { + + function test(name, testFunction) { + return tape(`valid: ${name}`, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/util/valid.js b/util/valid.js index bad48ef..d4832f5 100644 --- a/util/valid.js +++ b/util/valid.js @@ -33,7 +33,7 @@ module.exports.truthy = function(){ }; }; -module.exports.positive = function(){ +module.exports.nonnegative = function(){ return function( val ){ if( val < 0 ){ throw new PeliasModelError( 'invalid document type, expecting: 0 or a positive number, got: ' + val );