mirror of https://github.com/pelias/api.git
Julian Simioni
10 years ago
5 changed files with 67 additions and 1 deletions
@ -0,0 +1,19 @@
|
||||
var geo_common = require ('./_geo_common'); |
||||
var LAT_LON_IS_REQUIRED = false; |
||||
|
||||
// validate inputs, convert types and apply defaults
|
||||
module.exports = function sanitize( raw, clean ){ |
||||
|
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
try { |
||||
geo_common.sanitize_coord( 'lat', clean, raw['focus.point.lat'], LAT_LON_IS_REQUIRED ); |
||||
geo_common.sanitize_coord( 'lon', clean, raw['focus.point.lon'], LAT_LON_IS_REQUIRED ); |
||||
} |
||||
catch (err) { |
||||
messages.errors.push( err.message ); |
||||
} |
||||
|
||||
return messages; |
||||
}; |
@ -0,0 +1,24 @@
|
||||
var sanitizeAll = require('../sanitiser/sanitizeAll'), |
||||
sanitizers = { |
||||
text: require('../sanitiser/_text'), |
||||
size: require('../sanitiser/_size'), |
||||
private: require('../sanitiser/_flag_bool')('private', false), |
||||
geo_autocomplete: require('../sanitiser/_geo_autocomplete'), |
||||
}; |
||||
|
||||
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; |
||||
|
||||
// export sanitize for testing
|
||||
module.exports.sanitize = sanitize; |
||||
module.exports.sanitiser_list = sanitizers; |
||||
|
||||
// middleware
|
||||
module.exports.middleware = function( req, res, next ){ |
||||
sanitize( req, function( err, clean ){ |
||||
if( err ){ |
||||
res.status(400); // 400 Bad Request
|
||||
return next(err); |
||||
} |
||||
next(); |
||||
}); |
||||
}; |
@ -0,0 +1,21 @@
|
||||
var autocomplete = require('../../../sanitiser/autocomplete'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.sanitisers = function(test, common) { |
||||
test('check sanitiser list', function (t) { |
||||
var expected = ['text', 'size', 'private', 'geo_autocomplete' ]; |
||||
t.deepEqual(Object.keys(autocomplete.sanitiser_list), expected); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE /autocomplete ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue