Browse Source

Simplify ids sanitizing logic

pull/273/head
Julian Simioni 10 years ago
parent
commit
3101aad399
  1. 32
      sanitiser/_ids.js

32
sanitiser/_ids.js

@ -23,34 +23,24 @@ function sanitize( raw, clean ){
return messages; return messages;
} }
var rawIdsString; if (!check.unemptyString( raw.ids )) {
if (check.unemptyString( raw.ids )) { messages.errors.push( errorMessage( 'ids' ));
rawIdsString = raw.ids; return messages;
} else {
rawIdsString = '';
} }
// split string into array of values // split string into array of values
var rawIds = rawIdsString.split(','); var rawIds = raw.ids.split(',');
// deduplicate // deduplicate
rawIds = _.unique(rawIds); rawIds = _.unique(rawIds);
// ensure all elements are valid non-empty strings // ensure all elements are valid non-empty strings
rawIds = rawIds.filter( function( uc ){ if (!rawIds.every(check.unemptyString)) {
if( !check.unemptyString( uc ) ){
messages.errors.push( errorMessage('ids') ); messages.errors.push( errorMessage('ids') );
return false; }
}
return true;
});
// init 'clean.ids'
var validIds = [];
// cycle through raw ids and set those which are valid // cycle through raw ids and set those which are valid
rawIds.forEach( function( rawId ){ var validIds = rawIds.map( function( rawId ){
var param_index = rawId.indexOf(ID_DELIM); var param_index = rawId.indexOf(ID_DELIM);
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);
@ -59,7 +49,6 @@ function sanitize( raw, clean ){
if(param_index === -1) { if(param_index === -1) {
messages.errors.push( 'invalid: must be of the format type:id for ex: \'geoname:4163334\'' ); messages.errors.push( 'invalid: must be of the format type:id for ex: \'geoname:4163334\'' );
} }
// id text // id text
else if( !check.unemptyString( id ) ){ else if( !check.unemptyString( id ) ){
messages.errors.push( errorMessage( rawId ) ); messages.errors.push( errorMessage( rawId ) );
@ -74,15 +63,14 @@ function sanitize( raw, clean ){
} }
// add valid id to 'clean.ids' array // add valid id to 'clean.ids' array
else { else {
validIds.push({ return {
id: id, id: id,
type: type type: type
}); };
} }
}); });
if (validIds.length > 0) { if (validIds.every(check.object)) {
clean.ids = validIds; clean.ids = validIds;
} }

Loading…
Cancel
Save