Browse Source

remove callback in sanitizeAll.runAllChecks for sync processing

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

7
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};
};

7
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};

7
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};

7
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};

27
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
// 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

7
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};
};

7
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);
}
sanitizeAll.sanitize(req, sanitizers);
next();
});
};

8
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);
}
sanitizeAll.runAllChecks(req, sanitizers);
next();
});
};
};

34
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 (){
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
});
@ -82,13 +81,11 @@ module.exports.tests.all = function(test, common) {
warnings: ['pre-existing warning', 'warning 1', 'warning 2', 'warning 3']
};
sanitizeAll.sanitize(req, sanitizers, function () {
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
});
test('req.query should be passed to individual sanitizers when available', function(t) {
var req = {
query: {
@ -120,13 +117,11 @@ module.exports.tests.all = function(test, common) {
warnings: []
};
sanitizeAll.sanitize(req, sanitizers, function () {
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) {
var req = {};
var sanitizers = {
@ -152,13 +147,11 @@ module.exports.tests.all = function(test, common) {
warnings: []
};
sanitizeAll.sanitize(req, sanitizers, function () {
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
});
test('unexpected parameters should throw warning', function(t) {
var req = {
query: {
@ -178,14 +171,12 @@ module.exports.tests.all = function(test, common) {
}
};
sanitizeAll.checkParameters(req, sanitizers, function () {
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) {
var req = {
query: {
@ -205,14 +196,14 @@ module.exports.tests.all = function(test, common) {
}
};
sanitizeAll.checkParameters(req, sanitizers, function () {
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,11 +235,10 @@ module.exports.tests.all = function(test, common) {
warnings: ['warning 1']
};
sanitizeAll.runAllChecks(req, sanitizers, function () {
sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});
});
};

Loading…
Cancel
Save