From 4037c49c4b15439a59143ceaf4c196eb97ab0ace Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Thu, 3 Sep 2015 14:58:58 -0400 Subject: [PATCH] Store sanitised types from layers parameter This moves the list of types created by sanitising the layer API parameter from clean.layers to clean.types.from_layers. In subsequent commits, types created from address parsing, and the yet-to-be-implemented source parameter will also live in the clean.types object. This will allow moving logic to set cmd.type out of controllers, and into separate logic that can be a littler smarter. Also, it will no longer require the clean.default_layers_set flag to be passed all around like a nasty global variable. --- controller/search.js | 4 ++-- sanitiser/_layers.js | 4 +++- test/unit/sanitiser/reverse.js | 24 +++++++++++++----------- test/unit/sanitiser/search.js | 24 +++++++++++++----------- test/unit/sanitiser/suggest.js | 2 +- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/controller/search.js b/controller/search.js index 2923f57d..386af22f 100644 --- a/controller/search.js +++ b/controller/search.js @@ -15,8 +15,8 @@ function setup( backend, query ){ body: query( req.clean ) }; - if (req.clean.layers) { - cmd.type = req.clean.layers; + if (req.clean.types && req.clean.types.from_layers) { + cmd.type = req.clean.types.from_layers; } // set type if input suggests targeting a layer(s) diff --git a/sanitiser/_layers.js b/sanitiser/_layers.js index 2ad5c42f..25e8ba26 100644 --- a/sanitiser/_layers.js +++ b/sanitiser/_layers.js @@ -8,6 +8,8 @@ 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 = {}; @@ -39,7 +41,7 @@ function sanitize( req ){ } // pass validated params to next middleware - clean.layers = get_layers(layers); + clean.types.from_layers = get_layers(layers); req.clean = clean; return { 'error': false }; diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index d2ff8293..d8d666c1 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -5,8 +5,10 @@ var suggest = require('../../../sanitiser/reverse'), delim = ',', defaultError = 'missing param \'lat\'', defaultClean = { lat:0, - layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', - 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + types: { + from_layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + }, lon: 0, size: 10, details: true, @@ -158,7 +160,7 @@ module.exports.tests.sanitize_details = function(test, common) { module.exports.tests.sanitize_layers = function(test, common) { test('unspecified', function(t) { sanitize({ layers: undefined, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, defaultClean.layers, 'default layers set'); + t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); t.end(); }); }); @@ -173,21 +175,21 @@ module.exports.tests.sanitize_layers = function(test, common) { test('poi (alias) layer', function(t) { var poi_layers = ['geoname','osmnode','osmway']; sanitize({ layers: 'poi', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, poi_layers, 'poi layers set'); + t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set'); t.end(); }); }); test('admin (alias) layer', function(t) { var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; sanitize({ layers: 'admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, admin_layers, 'admin layers set'); + t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set'); t.end(); }); }); test('address (alias) layer', function(t) { var address_layers = ['osmaddress','openaddresses']; sanitize({ layers: 'address', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, address_layers, 'address layers set'); + t.deepEqual(clean.types.from_layers, address_layers, 'address layers set'); t.end(); }); }); @@ -195,7 +197,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var poi_layers = ['geoname','osmnode','osmway']; var reg_layers = ['admin0', 'admin1']; sanitize({ layers: 'poi,admin0,admin1', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers'); t.end(); }); }); @@ -203,7 +205,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; var reg_layers = ['geoname', 'osmway']; sanitize({ layers: 'admin,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); t.end(); }); }); @@ -211,21 +213,21 @@ module.exports.tests.sanitize_layers = function(test, common) { var address_layers = ['osmaddress','openaddresses']; var reg_layers = ['geoname', 'osmway']; sanitize({ layers: 'address,geoname,osmway', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set'); t.end(); }); }); test('alias layer plus regular layers (no duplicates)', function(t) { var poi_layers = ['geoname','osmnode','osmway']; sanitize({ layers: 'poi,geoname,osmnode', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)'); + t.deepEqual(clean.types.from_layers, poi_layers, 'poi 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']; sanitize({ layers: 'poi,admin', input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)'); + t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); t.end(); }); }); diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 702bbcb5..1f37fa13 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -8,8 +8,10 @@ var search = require('../../../sanitiser/search'), delim = ',', defaultError = 'invalid param \'input\': text length, must be >0', defaultClean = { input: 'test', - layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', - 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + types: { + from_layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood', + 'locality', 'local_admin', 'osmaddress', 'openaddresses' ], + }, size: 10, details: true, parsed_input: defaultParsed, @@ -320,7 +322,7 @@ module.exports.tests.sanitize_details = function(test, common) { module.exports.tests.sanitize_layers = function(test, common) { test('unspecified', function(t) { sanitize({ layers: undefined, input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, defaultClean.layers, 'default layers set'); + t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); t.end(); }); }); @@ -335,21 +337,21 @@ module.exports.tests.sanitize_layers = function(test, common) { test('poi (alias) layer', function(t) { var poi_layers = ['geoname','osmnode','osmway']; sanitize({ layers: 'poi', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, poi_layers, 'poi layers set'); + t.deepEqual(clean.types.from_layers, poi_layers, 'poi layers set'); t.end(); }); }); test('admin (alias) layer', function(t) { var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; sanitize({ layers: 'admin', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, admin_layers, 'admin layers set'); + t.deepEqual(clean.types.from_layers, admin_layers, 'admin layers set'); t.end(); }); }); test('address (alias) layer', function(t) { var address_layers = ['osmaddress','openaddresses']; sanitize({ layers: 'address', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, address_layers, 'address layers set'); + t.deepEqual(clean.types.from_layers, address_layers, 'address layers set'); t.end(); }); }); @@ -357,7 +359,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var poi_layers = ['geoname','osmnode','osmway']; var reg_layers = ['admin0', 'admin1']; sanitize({ layers: 'poi,admin0,admin1', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(poi_layers), 'poi + regular layers'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(poi_layers), 'poi + regular layers'); t.end(); }); }); @@ -365,7 +367,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var admin_layers = ['admin0','admin1','admin2','neighborhood','locality','local_admin']; var reg_layers = ['geoname', 'osmway']; sanitize({ layers: 'admin,geoname,osmway', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(admin_layers), 'admin + regular layers set'); t.end(); }); }); @@ -373,21 +375,21 @@ module.exports.tests.sanitize_layers = function(test, common) { var address_layers = ['osmaddress','openaddresses']; var reg_layers = ['geoname', 'osmway']; sanitize({ layers: 'address,geoname,osmway', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, reg_layers.concat(address_layers), 'address + regular layers set'); + t.deepEqual(clean.types.from_layers, reg_layers.concat(address_layers), 'address + regular layers set'); t.end(); }); }); test('alias layer plus regular layers (no duplicates)', function(t) { var poi_layers = ['geoname','osmnode','osmway']; sanitize({ layers: 'poi,geoname,osmnode', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, poi_layers, 'poi layers found (no duplicates)'); + t.deepEqual(clean.types.from_layers, poi_layers, 'poi 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']; sanitize({ layers: 'poi,admin', input: 'test' }, function( err, clean ){ - t.deepEqual(clean.layers, alias_layers, 'all layers found (no duplicates)'); + t.deepEqual(clean.types.from_layers, alias_layers, 'all layers found (no duplicates)'); t.end(); }); }); diff --git a/test/unit/sanitiser/suggest.js b/test/unit/sanitiser/suggest.js index b1ad1be6..003b44ae 100644 --- a/test/unit/sanitiser/suggest.js +++ b/test/unit/sanitiser/suggest.js @@ -272,7 +272,7 @@ module.exports.tests.sanitize_details = function(test, common) { module.exports.tests.sanitize_layers = function(test, common) { test('unspecified', function(t) { sanitize({ layers: undefined, input: 'test', lat: 0, lon: 0 }, function( err, clean ){ - t.deepEqual(clean.layers, defaultClean.layers, 'default layers set'); + t.deepEqual(clean.types.from_layers, defaultClean.types.from_layers, 'default layers set'); t.end(); }); });