mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
16 changed files with 217 additions and 307 deletions
@ -1,25 +0,0 @@
|
||||
module.exports = function(alias_layers) { |
||||
// make a copy of the array so, you are not modifying original ref
|
||||
var layers = alias_layers.slice(0); |
||||
|
||||
// expand aliases
|
||||
var expand_aliases = function(alias, layers, layer_indeces) { |
||||
var alias_index = layers.indexOf(alias); |
||||
if (alias_index !== -1 ) { |
||||
layers.splice(alias_index, 1); |
||||
layers = layers.concat(layer_indeces); |
||||
} |
||||
return layers; |
||||
}; |
||||
|
||||
layers = expand_aliases('poi', layers, ['geoname','osmnode','osmway']); |
||||
layers = expand_aliases('admin', layers, ['admin0','admin1','admin2','neighborhood','locality','local_admin']); |
||||
layers = expand_aliases('address', layers, ['osmaddress','openaddresses']); |
||||
|
||||
// de-dupe
|
||||
layers = layers.filter(function(item, pos) { |
||||
return layers.indexOf(item) === pos; |
||||
}); |
||||
|
||||
return layers; |
||||
}; |
@ -0,0 +1,15 @@
|
||||
/* |
||||
* Mapping from data layers to type values |
||||
*/ |
||||
|
||||
module.exports = { |
||||
'venue': ['geoname','osmnode','osmway'], |
||||
'address': ['osmaddress','openaddresses'], |
||||
'country': ['admin0'], |
||||
'region': ['admin1'], |
||||
'county': ['admin2'], |
||||
'locality': ['locality'], |
||||
'localadmin': ['local_admin'], |
||||
'neighbourhood': ['neighborhood'], |
||||
'coarse': ['admin0','admin1','admin2','neighborhood','locality','local_admin'], |
||||
}; |
@ -1,52 +0,0 @@
|
||||
|
||||
var isObject = require('is-object'), |
||||
types = require('../query/types'), |
||||
get_layers = require('../helper/layers'); |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
function sanitize( req ){ |
||||
var clean = req.clean || {}; |
||||
var params= req.query; |
||||
|
||||
clean.types = clean.types || {}; |
||||
|
||||
// ensure the input params are a valid object
|
||||
if( !isObject( params ) ){ |
||||
params = {}; |
||||
} |
||||
|
||||
// default case (no layers specified in GET params)
|
||||
// don't even set the from_layers key in this case
|
||||
if('string' !== typeof params.layers || !params.layers.length){ |
||||
return { 'error': false }; |
||||
} |
||||
|
||||
// decide which layers can be queried
|
||||
var alias_layers = ['poi', 'admin', 'address']; |
||||
var alias_types = types.concat(alias_layers); |
||||
|
||||
// parse GET params
|
||||
var layers = params.layers.split(',').map( function( layer ){ |
||||
return layer.toLowerCase(); // lowercase inputs
|
||||
}); |
||||
|
||||
// validate layer names
|
||||
for( var x=0; x<layers.length; x++ ){ |
||||
if( -1 === alias_types.indexOf( layers[x] ) ){ |
||||
return { |
||||
'error': true, |
||||
'message': 'invalid param \'layers\': must be one or more of ' + alias_types.join(',') |
||||
}; |
||||
} |
||||
} |
||||
|
||||
// pass validated params to next middleware
|
||||
clean.types.from_layers = get_layers(layers); |
||||
req.clean = clean; |
||||
|
||||
return { 'error': false }; |
||||
|
||||
} |
||||
|
||||
// export function
|
||||
module.exports = sanitize; |
@ -1,44 +0,0 @@
|
||||
var isObject = require('is-object'); |
||||
var sources_map = require( '../query/sources' ); |
||||
|
||||
function sanitize( req ) { |
||||
var params = req.query || {}; |
||||
|
||||
req.clean = req.clean || {}; |
||||
req.clean.types = req.clean.types || {}; |
||||
|
||||
// default case (no sources specified in GET params)
|
||||
if (params.hasOwnProperty('sources') === false) { |
||||
return { error: false }; |
||||
} |
||||
|
||||
params.sources = params.sources.trim(); |
||||
|
||||
if (params.sources.trim().length === 0) { |
||||
return { |
||||
error: true, |
||||
message: '`sources` parameter cannot be an empty string. Valid options: ' + Object.keys(sources_map).join(', ') |
||||
}; |
||||
} |
||||
|
||||
var sources = params.sources.split(','); |
||||
|
||||
var invalid_sources = sources.filter(function(source) { |
||||
return sources_map.hasOwnProperty(source) === false; |
||||
}); |
||||
|
||||
if (invalid_sources.length > 0) { |
||||
return { |
||||
error: true, |
||||
message: '`' + invalid_sources[0] + '` is an invalid source parameter. Valid options: ' + Object.keys(sources_map).join(', ') |
||||
}; |
||||
} |
||||
|
||||
req.clean.types.from_sources = sources.reduce(function(acc, source) { |
||||
return acc.concat(sources_map[source]); |
||||
}, []); |
||||
|
||||
return { error: false }; |
||||
} |
||||
|
||||
module.exports = sanitize; |
@ -0,0 +1,57 @@
|
||||
var _ = require('lodash'); |
||||
|
||||
function setup(paramName, targetMap) { |
||||
return function (req ) { |
||||
return sanitize(paramName, targetMap, req); |
||||
}; |
||||
} |
||||
|
||||
function sanitize( paramName, targetMap, req ) { |
||||
var params = req.query || {}; |
||||
|
||||
req.clean = req.clean || {}; |
||||
req.clean.types = req.clean.types || {}; |
||||
|
||||
// default case (no sources specified in GET params)
|
||||
// there is a case where the property is present, but set to null or undefined
|
||||
if (params.hasOwnProperty(paramName) === false || typeof params[paramName] === 'undefined') { |
||||
return { error: false }; |
||||
} |
||||
|
||||
params[paramName] = params[paramName].trim(); |
||||
|
||||
if (params[paramName].length === 0) { |
||||
return { |
||||
error: true, |
||||
message: paramName + ' parameter cannot be an empty string. Valid options: ' + Object.keys(targetMap).join(', ') |
||||
}; |
||||
} |
||||
|
||||
var targets = params[paramName].split(',').map( function( target ){ |
||||
return target.toLowerCase(); // lowercase inputs
|
||||
}); |
||||
|
||||
var invalidTargets = targets.filter(function(target) { |
||||
return targetMap.hasOwnProperty(target) === false; |
||||
}); |
||||
|
||||
if (invalidTargets.length > 0) { |
||||
return { |
||||
error: true, |
||||
message: invalidTargets[0] + ' is an invalid ' + paramName + ' parameter. Valid options: ' + Object.keys(targetMap).join(', ') |
||||
}; |
||||
} |
||||
|
||||
var cleanPropName = 'from_' + paramName; |
||||
|
||||
req.clean.types[cleanPropName] = targets.reduce(function(acc, target) { |
||||
return acc.concat(targetMap[target]); |
||||
}, []); |
||||
|
||||
// dedupe in case aliases expanded to common things or user typed in duplicates
|
||||
req.clean.types[cleanPropName] = _.unique(req.clean.types[cleanPropName]); |
||||
|
||||
return { error: false }; |
||||
} |
||||
|
||||
module.exports = setup; |
@ -0,0 +1,93 @@
|
||||
|
||||
var sanitize = require('../../../sanitiser/_targets')('layers', require('../../../query/layers')); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitize_layers = function(test, common) { |
||||
test('unspecified', function(t) { |
||||
var res = sanitize({ query: { layers: undefined } }); |
||||
t.equal(res.error, false); |
||||
t.end(); |
||||
}); |
||||
test('invalid layer', function(t) { |
||||
var req = { query: { layers: 'test_layer' } }; |
||||
var res = sanitize(req); |
||||
var msg = ' is an invalid layers parameter. Valid options: '; |
||||
t.true(res.error, 'error flag set'); |
||||
t.true(res.message.match(msg), 'invalid layer requested'); |
||||
t.true(res.message.length > msg.length, 'invalid error message'); |
||||
t.end(); |
||||
}); |
||||
test('venue (alias) layer', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway']; |
||||
var req = { query: { layers: 'venue' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, venue_layers, 'venue layers set'); |
||||
t.end(); |
||||
}); |
||||
test('coarse (alias) layer', function(t) { |
||||
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
||||
var req = { query: { layers: 'coarse' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, admin_layers, 'coarse layers set'); |
||||
t.end(); |
||||
}); |
||||
test('address (alias) layer', function(t) { |
||||
var address_layers = ['osmaddress','openaddresses']; |
||||
var req = { query: { layers: 'address' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, address_layers, 'address layers set'); |
||||
t.end(); |
||||
}); |
||||
test('venue alias layer plus regular layers', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway']; |
||||
var reg_layers = ['admin0', 'admin1']; |
||||
var req = { query: { layers: 'venue,country,region' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, venue_layers.concat(reg_layers), 'venue + regular layers'); |
||||
t.end(); |
||||
}); |
||||
test('coarse alias layer plus regular layers', function(t) { |
||||
var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; |
||||
var reg_layers = ['geoname', 'osmnode', 'osmway']; |
||||
|
||||
var req = { query: { layers: 'coarse,venue,country' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, admin_layers.concat(reg_layers), 'coarse + regular layers set'); |
||||
t.end(); |
||||
}); |
||||
test('address alias layer plus regular layers', function(t) { |
||||
var address_layers = ['osmaddress','openaddresses']; |
||||
var reg_layers = ['admin0', 'locality']; |
||||
|
||||
var req = { query: { layers: 'address,country,locality' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, address_layers.concat(reg_layers), 'address + regular layers set'); |
||||
t.end(); |
||||
}); |
||||
test('alias layer plus regular layers (no duplicates)', function(t) { |
||||
var venue_layers = ['geoname','osmnode','osmway','admin0']; |
||||
var req = { query: { layers: 'venue,country' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, venue_layers, 'venue layers found (no duplicates)'); |
||||
t.end(); |
||||
}); |
||||
test('multiple alias layers (no duplicates)', function(t) { |
||||
var alias_layers = ['geoname','osmnode','osmway','admin0','admin1','admin2','neighborhood','locality','local_admin']; |
||||
var req = { query: { layers: 'venue,coarse' } }; |
||||
sanitize(req); |
||||
t.deepEqual(req.clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
|
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _layers ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue