Browse Source

remove callback in sanitizeAll.runAllChecks for sync processing

pull/942/head
Lily He 8 years ago
parent
commit
80a3a259ef
  1. 9
      sanitizer/autocomplete.js
  2. 9
      sanitizer/nearby.js
  3. 9
      sanitizer/place.js
  4. 9
      sanitizer/reverse.js
  5. 33
      sanitizer/sanitizeAll.js
  6. 9
      sanitizer/search.js
  7. 9
      sanitizer/search_fallback.js
  8. 10
      sanitizer/structured_geocoding.js
  9. 68
      test/unit/sanitizer/sanitizeAll.js

9
sanitizer/autocomplete.js

@ -20,12 +20,7 @@ module.exports.middleware = (_api_pelias_config) => {
};
return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};
};

9
sanitizer/nearby.js

@ -15,11 +15,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware
module.exports.middleware = function( req, res, next ){
sanitize(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};

9
sanitizer/place.js

@ -14,11 +14,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware
module.exports.middleware = function(req, res, next){
sanitize(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};

9
sanitizer/reverse.js

@ -23,11 +23,6 @@ module.exports.sanitizer_list = sanitizers;
// middleware
module.exports.middleware = function( req, res, next ){
sanitize(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};

33
sanitizer/sanitizeAll.js

@ -1,8 +1,5 @@
'use strict';
const async = require('async');
function sanitize( req, sanitizers, cb ){
function sanitize( req, sanitizers ){
// init an object to store clean (sanitized) input parameters if not initialized
req.clean = req.clean || {};
@ -29,11 +26,11 @@ function sanitize( req, sanitizers, cb ){
req.warnings = req.warnings.concat( sanity.warnings );
}
}
return cb( undefined, req.clean );
}
// Adds to goodParameters every acceptable parameter passed through API call
function checkParameters(req, sanitizers, cb) {
function checkParameters( req, sanitizers ) {
req.warnings = req.warnings || [];
// source of input parameters
// (in this case from the GET querystring params)
const params = req.query || {};
@ -41,9 +38,9 @@ function checkParameters(req, sanitizers, cb) {
for (let s in sanitizers) {
// checks if there is a function that returns valid params
// checks if function exists
if (typeof sanitizers[s].expected === 'function'){
/** func returns {array} ex: [{ name: 'text' }] */
/** expected() returns {array} ex: [{ name: 'text' }] */
for (let t in sanitizers[s].expected()) {
/** {object} prop */
const prop = sanitizers[s].expected()[t];
@ -54,21 +51,21 @@ function checkParameters(req, sanitizers, cb) {
}
}
}
// If there are any unexpected parameters, add a warning to messages
for (let p in params) {
if (!goodParameters.hasOwnProperty(p)){
req.warnings = req.warnings.concat('Invalid Parameter: ' + p);
// If there are any unexpected parameters & goodParameters isn't empty,
// add a warning message
if (Object.keys(goodParameters).length !== 0) {
for (let p in params) {
if (!goodParameters.hasOwnProperty(p)){
req.warnings = req.warnings.concat('Invalid Parameter: ' + p);
}
}
}
return cb( undefined, req.clean );
}
// runs both sanitize and checkParameters functions in async parallel
function runAllChecks (req, sanitizers, cb) {
async.parallel([
sanitize.bind(null, req, sanitizers),
checkParameters.bind(null, req, sanitizers)
], cb);
function runAllChecks (req, sanitizers) {
sanitize(req, sanitizers);
checkParameters(req, sanitizers);
}
// export function

9
sanitizer/search.js

@ -21,13 +21,8 @@ module.exports.middleware = (_api_pelias_config) => {
};
return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};
};

9
sanitizer/search_fallback.js

@ -23,12 +23,7 @@ module.exports.middleware = function( req, res, next ){
}
// calls to sanitize the input
// omits check if parameters are valid since it only calls _text_addressit
sanitizeAll.sanitize(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.sanitize(req, sanitizers);
next();
};

10
sanitizer/structured_geocoding.js

@ -22,13 +22,9 @@ module.exports.middleware = (_api_pelias_config) => {
};
return ( req, res, next ) => {
sanitizeAll.runAllChecks(req, sanitizers, ( err, clean ) => {
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
next();
});
sanitizeAll.runAllChecks(req, sanitizers);
next();
};
};

68
test/unit/sanitizer/sanitizeAll.js

@ -35,10 +35,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['warning 1', 'warning 2', 'warning 3']
};
sanitizeAll.sanitize(req, sanitizers, function (){
t.deepEquals(req, expected_req);
t.end();
});
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
@ -82,11 +81,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['pre-existing warning', 'warning 1', 'warning 2', 'warning 3']
};
sanitizeAll.sanitize(req, sanitizers, function () {
t.deepEquals(req, expected_req);
t.end();
});
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
test('req.query should be passed to individual sanitizers when available', function(t) {
@ -97,7 +94,7 @@ module.exports.tests.all = function(test, common) {
};
var sanitizers = {
'first': {
sanitize: function(params) {
sanitize: function (params) {
req.clean.query = params;
return {
errors: [],
@ -120,11 +117,9 @@ module.exports.tests.all = function(test, common) {
warnings: []
};
sanitizeAll.sanitize(req, sanitizers, function () {
t.deepEquals(req, expected_req);
t.end();
});
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
test('an empty object should be passed to individual sanitizers when req.query is unavailable', function(t) {
@ -152,11 +147,9 @@ module.exports.tests.all = function(test, common) {
warnings: []
};
sanitizeAll.sanitize(req, sanitizers, function () {
t.deepEquals(req, expected_req);
t.end();
});
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
test('unexpected parameters should throw warning', function(t) {
@ -178,12 +171,10 @@ module.exports.tests.all = function(test, common) {
}
};
sanitizeAll.checkParameters(req, sanitizers, function () {
t.equals(req.errors.length, 0);
t.deepEquals(req.warnings[0], 'Invalid Parameter: unknown_value');
t.end();
});
sanitizeAll.checkParameters(req, sanitizers);
t.equals(req.errors.length, 0);
t.deepEquals(req.warnings[0], 'Invalid Parameter: unknown_value');
t.end();
});
test('expected parameters should not throw warning', function(t) {
@ -205,14 +196,14 @@ module.exports.tests.all = function(test, common) {
}
};
sanitizeAll.checkParameters(req, sanitizers, function () {
t.equals(req.errors.length, 0);
t.equals(req.warnings.length, 0);
t.end();
});
sanitizeAll.checkParameters(req, sanitizers);
t.equals(req.errors.length, 0);
t.equals(req.warnings.length, 0);
t.end();
});
test('runAllChecks calls both sanitize and expectedParameters function', function(t) {
test('sanitizer without expected() should not validate parameters', function(t) {
var req = {
query: {
value: 'query'
@ -227,12 +218,6 @@ module.exports.tests.all = function(test, common) {
errors: [],
warnings: ['warning 1']
};
},
expected: function _expected () {
// add value as a valid parameter
return [{
name: 'value'
}];
}
}
};
@ -250,10 +235,9 @@ module.exports.tests.all = function(test, common) {
warnings: ['warning 1']
};
sanitizeAll.runAllChecks(req, sanitizers, function () {
t.deepEquals(req, expected_req);
t.end();
});
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
};

Loading…
Cancel
Save