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.
39 lines
851 B
39 lines
851 B
10 years ago
|
|
||
|
var isObject = require('is-object');
|
||
|
|
||
|
// validate inputs, convert types and apply defaults
|
||
|
function sanitize( req ){
|
||
|
|
||
|
var clean = req.clean || {};
|
||
|
var params= req.query;
|
||
|
|
||
|
// ensure the input params are a valid object
|
||
|
if( !isObject( params ) ){
|
||
|
params = {};
|
||
|
}
|
||
|
|
||
|
// default case (no categories specified in GET params)
|
||
|
if('string' !== typeof params.categories || !params.categories.length){
|
||
|
clean.categories = [];
|
||
|
}
|
||
|
else {
|
||
|
// parse GET params
|
||
|
clean.categories = params.categories.split(',')
|
||
|
.map(function (cat) {
|
||
|
return cat.toLowerCase().trim(); // lowercase inputs
|
||
|
})
|
||
|
.filter( function( cat ) {
|
||
|
return ( cat.length > 0 );
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// pass validated params to next middleware
|
||
|
req.clean = clean;
|
||
|
|
||
|
return { 'error': false };
|
||
|
|
||
|
}
|
||
|
|
||
|
// export function
|
||
|
module.exports = sanitize;
|