mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.2 KiB
89 lines
2.2 KiB
9 years ago
|
var _ = require('lodash'),
|
||
|
check = require('check-types'),
|
||
9 years ago
|
type_mapping = require('../helper/type_mapping');
|
||
9 years ago
|
|
||
|
var ID_DELIM = ':';
|
||
10 years ago
|
|
||
9 years ago
|
// validate inputs, convert types and apply defaults id generally looks like
|
||
|
// 'geonames:venue:4163334' (source:layer:id) so, all three are required
|
||
10 years ago
|
|
||
9 years ago
|
var lengthError = 'invalid param \'ids\': length must be >0';
|
||
9 years ago
|
|
||
9 years ago
|
var formatError = function(input) {
|
||
9 years ago
|
return 'id `' + input + ' is invalid: must be of the format source:layer:id for ex: \'geonames:venue:4163334\'';
|
||
|
};
|
||
|
|
||
|
var targetError = function(target, target_list) {
|
||
|
return target + ' is invalid. It must be one of these values - [' + target_list.join(', ') + ']';
|
||
9 years ago
|
};
|
||
|
|
||
9 years ago
|
function sanitizeId(rawId, messages) {
|
||
9 years ago
|
var parts = rawId.split(ID_DELIM);
|
||
|
|
||
|
if ( parts.length < 3 ) {
|
||
|
messages.errors.push( formatError(rawId) );
|
||
|
return;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
var source = parts[0];
|
||
|
var layer = parts[1];
|
||
|
var id = parts.slice(2).join(ID_DELIM);
|
||
|
|
||
|
// check if any parts of the gid are empty
|
||
9 years ago
|
if (_.includes([source, layer, id], '')) {
|
||
9 years ago
|
messages.errors.push( formatError(rawId) );
|
||
9 years ago
|
return;
|
||
9 years ago
|
}
|
||
8 years ago
|
var valid_values = Object.keys(type_mapping.source_mapping);
|
||
|
if (!_.includes(valid_values, source)) {
|
||
|
messages.errors.push( targetError(source, valid_values) );
|
||
9 years ago
|
return;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if (!_.includes(type_mapping.layers, layer)) {
|
||
9 years ago
|
messages.errors.push( targetError(layer, type_mapping.layers) );
|
||
|
return;
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
return {
|
||
8 years ago
|
source: type_mapping.source_mapping[source][0],
|
||
9 years ago
|
layer: layer,
|
||
9 years ago
|
id: id,
|
||
|
};
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
function sanitize( raw, clean ){
|
||
9 years ago
|
// error & warning messages
|
||
|
var messages = { errors: [], warnings: [] };
|
||
|
|
||
9 years ago
|
if (!check.nonEmptyString( raw.ids )) {
|
||
9 years ago
|
messages.errors.push( lengthError);
|
||
9 years ago
|
return messages;
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// split string into array of values
|
||
9 years ago
|
var rawIds = raw.ids.split(',');
|
||
9 years ago
|
|
||
|
// deduplicate
|
||
9 years ago
|
rawIds = _.uniq(rawIds);
|
||
9 years ago
|
|
||
9 years ago
|
// ensure all elements are valid non-empty strings
|
||
9 years ago
|
if (!rawIds.every(check.nonEmptyString)) {
|
||
9 years ago
|
messages.errors.push( lengthError );
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
// cycle through raw ids and set those which are valid
|
||
9 years ago
|
var validIds = rawIds.map(function(rawId) {
|
||
|
return sanitizeId(rawId, messages);
|
||
9 years ago
|
});
|
||
10 years ago
|
|
||
9 years ago
|
if (validIds.every(check.object)) {
|
||
9 years ago
|
clean.ids = validIds;
|
||
|
}
|
||
|
|
||
9 years ago
|
return messages;
|
||
10 years ago
|
}
|
||
|
|
||
|
// export function
|
||
9 years ago
|
module.exports = sanitize;
|