Browse Source

feat: add debug:force-libpostal param to search sanitizers

force-libpostal-param
Diana Shkolnikov 8 years ago
parent
commit
b61c77f4a5
  1. 32
      sanitizer/_details.js
  2. 3
      sanitizer/search.js
  3. 1
      test/unit/run.js
  4. 31
      test/unit/sanitizer/_truthy.js
  5. 7
      test/unit/sanitizer/search.js

32
sanitizer/_details.js

@ -1,32 +0,0 @@
var _ = require('lodash'),
check = require('check-types');
var DEFAULT_DETAILS_BOOL = true;
// validate inputs, convert types and apply defaults
function sanitize( raw, clean ){
// error & warning messages
var messages = { errors: [], warnings: [] };
if( !check.undefined( raw.details ) ){
clean.details = isTruthy( raw.details );
} else {
clean.details = DEFAULT_DETAILS_BOOL;
}
return messages;
}
// be lenient with 'truthy' values
function isTruthy(val) {
if( check.string( val ) ){
return _.includes( ['true', '1'], val );
}
return val === 1 || val === true;
}
// export function
module.exports = sanitize;

3
sanitizer/search.js

@ -15,7 +15,8 @@ var sanitizeAll = require('../sanitizer/sanitizeAll'),
private: require('../sanitizer/_flag_bool')('private', false),
geo_search: require('../sanitizer/_geo_search'),
boundary_country: require('../sanitizer/_boundary_country'),
categories: require('../sanitizer/_categories')
categories: require('../sanitizer/_categories'),
debug: require('../sanitizer/_debug')
};
var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); };

1
test/unit/run.js

@ -44,6 +44,7 @@ var tests = [
require('./query/structured_geocoding'),
require('./query/text_parser'),
require('./sanitizer/_boundary_country'),
require('./sanitizer/_debug'),
require('./sanitizer/_flag_bool'),
require('./sanitizer/_geo_common'),
require('./sanitizer/_geo_reverse'),

31
test/unit/sanitizer/_truthy.js

@ -1,31 +0,0 @@
var isTruthy = require('../../../sanitizer/_truthy');
module.exports.tests = {};
module.exports.tests.sanitize_truthy = function(test, common) {
var valid_values = ['true', true, 1, '1', 'yes', 'y'];
valid_values.forEach(function(value) {
test('truthy value ' + value, function(t) {
t.equal(isTruthy(value), true, 'returns true');
t.end();
});
});
var valid_false_values = ['false', false, 0, '0', 'no', 'n', null, -1, 123, NaN, 'abc'];
valid_false_values.forEach(function(value) {
test('falsey value ' + value, function(t) {
t.equal(isTruthy(value), false, 'returns false');
t.end();
});
});
};
module.exports.all = function (tape, common) {
function test(name, testFunction) {
return tape('SANTIZE _truthy ' + name, testFunction);
}
for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};

7
test/unit/sanitizer/search.js

@ -83,6 +83,10 @@ module.exports.tests.sanitize = function(test, common) {
called_sanitizers.push('_categories');
return { errors: [], warnings: [] };
},
'../sanitizer/_debug': function() {
called_sanitizers.push('_debug');
return { errors: [], warnings: [] };
}
});
var expected_sanitizers = [
@ -98,7 +102,8 @@ module.exports.tests.sanitize = function(test, common) {
'_flag_bool',
'_geo_search',
'_boundary_country',
'_categories'
'_categories',
'_debug'
];
var req = {};

Loading…
Cancel
Save