mirror of https://github.com/pelias/api.git
Diana Shkolnikov
9 years ago
10 changed files with 128 additions and 87 deletions
@ -1,44 +0,0 @@
|
||||
var isObject = require('is-object'); |
||||
var sources_map = require( '../query/sources' ); |
||||
var all_sources = Object.keys(sources_map); |
||||
|
||||
function sanitize( req ) { |
||||
req.clean = req.clean || {}; |
||||
var params = req.query; |
||||
|
||||
req.clean.types = req.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.source || !params.source.length){ |
||||
return { error: false }; |
||||
} |
||||
|
||||
var sources = params.source.split(','); |
||||
|
||||
var invalid_sources = sources.filter(function(source) { |
||||
return all_sources.indexOf(source) === -1; |
||||
}); |
||||
|
||||
if (invalid_sources.length > 0) { |
||||
return { |
||||
error: true, |
||||
msg: '`' + invalid_sources[0] + '` is an invalid source parameter. Valid options: ' + all_sources.join(', ') |
||||
}; |
||||
} |
||||
|
||||
var types = sources.reduce(function(acc, source) { |
||||
return acc.concat(sources_map[source]); |
||||
}, []); |
||||
|
||||
req.clean.types.from_source = types; |
||||
|
||||
return { error: false }; |
||||
} |
||||
|
||||
module.exports = sanitize; |
@ -0,0 +1,44 @@
|
||||
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; |
Loading…
Reference in new issue