Browse Source

Use simpler, clearer error on invalid ids length

pull/273/head
Julian Simioni 9 years ago
parent
commit
16450087ee
  1. 8
      sanitiser/_ids.js
  2. 2
      test/ciao/place/missing_id.coffee
  3. 7
      test/unit/sanitiser/_ids.js
  4. 2
      test/unit/sanitiser/place.js

8
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

2
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

7
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();
});

2
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();
});

Loading…
Cancel
Save