From 16450087ee4ec9942457937f6d6a5d68fea6b228 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 17 Sep 2015 15:54:34 -0400 Subject: [PATCH] Use simpler, clearer error on invalid ids length --- sanitiser/_ids.js | 8 +++----- test/ciao/place/missing_id.coffee | 2 +- test/unit/sanitiser/_ids.js | 7 +++---- test/unit/sanitiser/place.js | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/sanitiser/_ids.js b/sanitiser/_ids.js index df3925d2..98c32946 100644 --- a/sanitiser/_ids.js +++ b/sanitiser/_ids.js @@ -8,9 +8,7 @@ var ID_DELIM = ':'; // id generally looks like 'geoname:4163334' (type:id) // so, both type and id are required fields. -function errorMessage(fieldname, message) { - return message || 'invalid param \''+ fieldname + '\': text length, must be >0'; -} +var lengthError = 'invalid param \'ids\': length must be >0'; var formatError = function(input) { return 'id `' + input + 'is invalid: must be of the format type:id for ex: \'geoname:4163334\''; @@ -28,7 +26,7 @@ function sanitize( raw, clean ){ } if (!check.unemptyString( raw.ids )) { - messages.errors.push( errorMessage( 'ids' )); + messages.errors.push( lengthError); return messages; } @@ -40,7 +38,7 @@ function sanitize( raw, clean ){ // ensure all elements are valid non-empty strings if (!rawIds.every(check.unemptyString)) { - messages.errors.push( errorMessage('ids') ); + messages.errors.push( lengthError ); } // cycle through raw ids and set those which are valid diff --git a/test/ciao/place/missing_id.coffee b/test/ciao/place/missing_id.coffee index f7bc23c7..1fc8ed18 100644 --- a/test/ciao/place/missing_id.coffee +++ b/test/ciao/place/missing_id.coffee @@ -24,7 +24,7 @@ json.features.should.be.instanceof Array #? expected errors should.exist json.geocoding.errors -json.geocoding.errors.should.eql [ 'invalid param \'ids\': text length, must be >0' ] +json.geocoding.errors.should.eql [ 'invalid param \'ids\': length must be >0' ] #? expected warnings should.not.exist json.geocoding.warnings diff --git a/test/unit/sanitiser/_ids.js b/test/unit/sanitiser/_ids.js index c98556eb..02e25f18 100644 --- a/test/unit/sanitiser/_ids.js +++ b/test/unit/sanitiser/_ids.js @@ -10,9 +10,8 @@ var inputs = { var formatError = function(input) { return 'id `' + input + 'is invalid: must be of the format type:id for ex: \'geoname:4163334\''; }; - -var defaultError = 'invalid param \'ids\': text length, must be >0', - defaultMissingTypeError = function(input) { +var lengthError = 'invalid param \'ids\': length must be >0'; +var defaultMissingTypeError = function(input) { var type = input.split(delimiter)[0]; return type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']'; }; @@ -26,7 +25,7 @@ module.exports.tests.invalid_ids = function(test, common) { var messages = sanitize(raw, clean); - t.equal(messages.errors[0], defaultError, 'ids length error returned'); + t.equal(messages.errors[0], lengthError, 'ids length error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); t.end(); }); diff --git a/test/unit/sanitiser/place.js b/test/unit/sanitiser/place.js index 1c0a8a53..ca97cf54 100644 --- a/test/unit/sanitiser/place.js +++ b/test/unit/sanitiser/place.js @@ -74,7 +74,7 @@ module.exports.tests.invalid_params = function(test, common) { test('no params', function(t) { var req = { query: {} }; sanitize( req, function(){ - t.equal( req.errors[0], 'invalid param \'ids\': text length, must be >0', 'error for missing `ids` param'); + t.equal( req.errors[0], 'invalid param \'ids\': length must be >0', 'error for missing `ids` param'); t.deepEqual( req.warnings, [], 'no warnings' ); t.end(); });