Browse Source

Provide better, more consistent, errors on invalid ids

All error messages for invalid id formats now use a common format that
explains the nature of the error a bit better.
pull/273/head
Julian Simioni 9 years ago
parent
commit
edaf175aa7
  1. 19
      sanitiser/_ids.js
  2. 17
      test/unit/sanitiser/_ids.js

19
sanitiser/_ids.js

@ -12,6 +12,10 @@ function errorMessage(fieldname, message) {
return message || 'invalid param \''+ fieldname + '\': text length, must be >0'; return message || 'invalid param \''+ fieldname + '\': text length, must be >0';
} }
var formatError = function(input) {
return 'id `' + input + 'is invalid: must be of the format type:id for ex: \'geoname:4163334\'';
};
function sanitize( raw, clean ){ function sanitize( raw, clean ){
// error & warning messages // error & warning messages
var messages = { errors: [], warnings: [] }; var messages = { errors: [], warnings: [] };
@ -45,23 +49,14 @@ function sanitize( raw, clean ){
var type = rawId.substring(0, param_index ); var type = rawId.substring(0, param_index );
var id = rawId.substring(param_index + 1); var id = rawId.substring(param_index + 1);
// basic format/ presence of ':' // check id format
if(param_index === -1) { if(param_index === -1 || !check.unemptyString( id ) || !check.unemptyString( type )) {
messages.errors.push( 'invalid: must be of the format type:id for ex: \'geoname:4163334\'' ); messages.errors.push( formatError(rawId) );
}
// id text
else if( !check.unemptyString( id ) ){
messages.errors.push( errorMessage( rawId ) );
}
// type text
else if( !check.unemptyString( type ) ){
messages.errors.push( errorMessage( rawId ) );
} }
// type text must be one of the types // type text must be one of the types
else if( !_.contains( types, type ) ){ else if( !_.contains( types, type ) ){
messages.errors.push( type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']' ); messages.errors.push( type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']' );
} }
// add valid id to 'clean.ids' array
else { else {
return { return {
id: id, id: id,

17
test/unit/sanitiser/_ids.js

@ -6,9 +6,12 @@ var inputs = {
valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ], valid: [ 'geoname:1', 'osmnode:2', 'admin0:53', 'osmway:44', 'geoname:5' ],
invalid: [ ':', '', '::', 'geoname:', ':234', 'gibberish:23' ] invalid: [ ':', '', '::', 'geoname:', ':234', 'gibberish:23' ]
}; };
var defaultLengthError = function(input) { return 'invalid param \''+ input + '\': text length, must be >0'; },
defaultFormatError = 'invalid: must be of the format type:id for ex: \'geoname:4163334\'', var formatError = function(input) {
defaultError = 'invalid param \'ids\': text length, must be >0', 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) { defaultMissingTypeError = function(input) {
var type = input.split(delimiter)[0]; var type = input.split(delimiter)[0];
return type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']'; return type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']';
@ -34,7 +37,7 @@ module.exports.tests.invalid_ids = function(test, common) {
var messages = sanitize(raw, clean); var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError(':'), 'format error returned'); t.equal(messages.errors[0], formatError(':'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object'); t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end(); t.end();
}); });
@ -45,7 +48,7 @@ module.exports.tests.invalid_ids = function(test, common) {
var messages = sanitize(raw, clean); var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError('::'), 'format error returned'); t.equal(messages.errors[0], formatError('::'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object'); t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end(); t.end();
}); });
@ -56,7 +59,7 @@ module.exports.tests.invalid_ids = function(test, common) {
var messages = sanitize(raw, clean); var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError('geoname:'), 'format error returned'); t.equal(messages.errors[0], formatError('geoname:'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object'); t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end(); t.end();
}); });
@ -67,7 +70,7 @@ module.exports.tests.invalid_ids = function(test, common) {
var messages = sanitize(raw, clean); var messages = sanitize(raw, clean);
t.equal(messages.errors[0], defaultLengthError(':234'), 'format error returned'); t.equal(messages.errors[0], formatError(':234'), 'format error returned');
t.equal(clean.ids, undefined, 'ids unset in clean object'); t.equal(clean.ids, undefined, 'ids unset in clean object');
t.end(); t.end();
}); });

Loading…
Cancel
Save