diff --git a/sanitizer/_boundary_country.js b/sanitizer/_boundary_country.js index b859013e..2b7f9bb3 100644 --- a/sanitizer/_boundary_country.js +++ b/sanitizer/_boundary_country.js @@ -1,7 +1,7 @@ -var check = require('check-types'); -var iso3166 = require('iso3166-1'); +const check = require('check-types'); +const iso3166 = require('iso3166-1'); -function sanitize(raw, clean) { +function _sanitize(raw, clean) { // error & warning messages var messages = { errors: [], warnings: [] }; @@ -37,4 +37,11 @@ function containsIsoCode(isoCode) { return iso3166.is2(isoCode) || iso3166.is3(isoCode); } -module.exports = sanitize; +function _expected(){ + return [{ name: 'boundary.country' }]; +} + +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_categories.js b/sanitizer/_categories.js index 06e65800..b964d1e7 100644 --- a/sanitizer/_categories.js +++ b/sanitizer/_categories.js @@ -1,4 +1,3 @@ - var check = require('check-types'); var categoryTaxonomy = require('pelias-categories'); @@ -8,7 +7,7 @@ var ERRORS = { }; // validate inputs, convert types and apply defaults -function sanitize( raw, clean, categories ) { +function _sanitize( raw, clean, categories ) { categories = categories || categoryTaxonomy; @@ -50,5 +49,11 @@ function sanitize( raw, clean, categories ) { return messages; } +function _expected() { + return [{ name: 'categories' }]; +} // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_city_name_standardizer.js b/sanitizer/_city_name_standardizer.js index 3bcf817d..6724eda1 100644 --- a/sanitizer/_city_name_standardizer.js +++ b/sanitizer/_city_name_standardizer.js @@ -15,7 +15,7 @@ function transliterate(match) { } // transliterate ft/mt/saint/sainte to fort/mount/st/ste, respectively -function sanitize(raw, clean) { +function _sanitize(raw, clean) { // error & warning messages // this function doesn't add any error or warning messages const messages = { errors: [], warnings: [] }; @@ -44,4 +44,6 @@ function sanitize(raw, clean) { } -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_deprecate_quattroshapes.js b/sanitizer/_deprecate_quattroshapes.js index bd45c025..50f3af8e 100644 --- a/sanitizer/_deprecate_quattroshapes.js +++ b/sanitizer/_deprecate_quattroshapes.js @@ -10,7 +10,7 @@ var _ = require('lodash'); @see: https://github.com/pelias/api/issues/442 **/ -function sanitize( raw, clean, opts ) { +function _sanitize( raw, clean, opts ) { // error & warning messages var messages = { errors: [], warnings: [] }; @@ -38,4 +38,6 @@ function sanitize( raw, clean, opts ) { return messages; } -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_flag_bool.js b/sanitizer/_flag_bool.js index 4ed5c339..aab2daac 100644 --- a/sanitizer/_flag_bool.js +++ b/sanitizer/_flag_bool.js @@ -5,38 +5,46 @@ var _ = require('lodash'); * * @param {string} paramName name of parameter being sanitized * @param {boolean} defaultValue value to set variable to if none specified - * @returns {Function} + * @returns {Object} object containing functions */ -function setup( paramName, defaultValue ) { - return function( raw, clean ){ - return sanitize( raw, clean, { - paramName: paramName, - defaultValue: defaultValue - }); +function _setup( paramName, defaultValue ) { + /** + * {object} opts + */ + + const opts = { + paramName: paramName, + defaultValue: defaultValue }; -} -/** - * Validate inputs, convert types and apply defaults - * - * @param {object} raw - * @param {object} clean - * @param {object} opts - * @returns {{errors: Array, warnings: Array}} - */ -function sanitize( raw, clean, opts ){ - - // error & warning messages`1 - var messages = { errors: [], warnings: [] }; - - if( !_.isUndefined( raw[opts.paramName] ) ){ - clean[opts.paramName] = isTruthy( raw[opts.paramName] ); - } - else { - clean[opts.paramName] = opts.defaultValue; - } - return messages; -} + return { + /** + * Validate inputs, convert types and apply defaults + * + * @param {object} raw + * @param {object} clean + * @returns {{errors: Array, warnings: Array}} + */ + + sanitize: function _sanitize( raw, clean){ + + // error & warning messages`1 + var messages = { errors: [], warnings: [] }; + + if( !_.isUndefined( raw[opts.paramName] ) ){ + clean[opts.paramName] = isTruthy( raw[opts.paramName] ); + } + else { + clean[opts.paramName] = opts.defaultValue; + } + return messages; + }, // end of _sanitize function + + expected: function _expected(){ + return [{ name: 'private'}]; + } // end of _expected function + }; // end of return object +} // end of _setup function /** * Determine if param value is "truthy" @@ -47,4 +55,4 @@ function isTruthy(val) { return _.includes( ['true', '1', 1, true], val ); } -module.exports = setup; +module.exports = _setup; diff --git a/sanitizer/_geo_autocomplete.js b/sanitizer/_geo_autocomplete.js index 45c2f921..e6ba948b 100644 --- a/sanitizer/_geo_autocomplete.js +++ b/sanitizer/_geo_autocomplete.js @@ -3,7 +3,7 @@ var LAT_LON_IS_REQUIRED = false; var RECT_IS_REQUIRED = false; // validate inputs, convert types and apply defaults -module.exports = function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -17,4 +17,19 @@ module.exports = function sanitize( raw, clean ){ } return messages; -}; +} + +function _expected(){ + return [ + { name: 'focus.point.lat' }, + { name: 'focus.point.lon' }, + { name: 'boundary.rect.min_lat' }, + { name: 'boundary.rect.max_lat' }, + { name: 'boundary.rect.min_lon' }, + { name: 'boundary.rect.max_lon' }]; +} + +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_geo_reverse.js b/sanitizer/_geo_reverse.js index 1bb85043..da811219 100644 --- a/sanitizer/_geo_reverse.js +++ b/sanitizer/_geo_reverse.js @@ -7,7 +7,7 @@ var LAT_LON_IS_REQUIRED = true, const non_coarse_layers = ['venue', 'address', 'street']; // validate inputs, convert types and apply defaults -module.exports = function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -38,4 +38,16 @@ module.exports = function sanitize( raw, clean ){ } return messages; -}; +} + +function _expected(){ + return [ + { name: 'point.lat' }, + { name: 'point.lon' }, + { name: 'boundary.circle.radius'}]; +} + +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_geo_search.js b/sanitizer/_geo_search.js index ae820fbf..b41184e7 100644 --- a/sanitizer/_geo_search.js +++ b/sanitizer/_geo_search.js @@ -6,7 +6,7 @@ var RECT_IS_REQUIRED = false; var CIRCLE_IS_REQUIRED = false; // validate inputs, convert types and apply defaults -module.exports = function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -21,4 +21,22 @@ module.exports = function sanitize( raw, clean ){ } return messages; -}; +} + +function _expected(){ + return [ + { name: 'focus.point.lat' }, + { name: 'focus.point.lon' }, + { name: 'boundary.circle.lon'}, + { name: 'boundary.circle.lat'}, + { name: 'boundary.circle.radius'}, + { name: 'boundary.rect.min_lat' }, + { name: 'boundary.rect.max_lat' }, + { name: 'boundary.rect.min_lon' }, + { name: 'boundary.rect.max_lon' }]; +} + +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_geonames_deprecation.js b/sanitizer/_geonames_deprecation.js index ceb4e591..b3cc8185 100644 --- a/sanitizer/_geonames_deprecation.js +++ b/sanitizer/_geonames_deprecation.js @@ -5,7 +5,7 @@ with the release of coarse reverse as a separate thing and ES reverse only handling venues, addresses, and streets, geonames make no sense in the reverse context **/ -function sanitize( raw, clean, opts ) { +function _sanitize( raw, clean, opts ) { // error & warning messages const messages = { errors: [], warnings: [] }; @@ -19,7 +19,9 @@ function sanitize( raw, clean, opts ) { } return messages; - + } -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_geonames_warnings.js b/sanitizer/_geonames_warnings.js index eda12131..3422c4f6 100644 --- a/sanitizer/_geonames_warnings.js +++ b/sanitizer/_geonames_warnings.js @@ -9,7 +9,7 @@ function hasAnyNonAdminFields(parsed_text) { non_admin_fields)); } -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages const messages = { errors: [], warnings: [] }; @@ -34,4 +34,6 @@ function sanitize( raw, clean ){ return messages; } -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_ids.js b/sanitizer/_ids.js index ba456766..89590ac3 100644 --- a/sanitizer/_ids.js +++ b/sanitizer/_ids.js @@ -52,7 +52,7 @@ function sanitizeId(rawId, messages) { }; } -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -84,5 +84,11 @@ function sanitize( raw, clean ){ return messages; } +function _expected(){ + return [{ name: 'ids' }]; +} // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_iso2_to_iso3.js b/sanitizer/_iso2_to_iso3.js index d8fc1837..6b37cec2 100644 --- a/sanitizer/_iso2_to_iso3.js +++ b/sanitizer/_iso2_to_iso3.js @@ -4,7 +4,7 @@ const iso3166 = require('iso3166-1'); // this sanitizer exists solely to convert an ISO2 country value to ISO3 // eg - 'TH' -> 'THA' // this can go away once altnames imports ISO2 country values from WOF -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages const messages = { errors: [], warnings: [] }; @@ -16,4 +16,6 @@ function sanitize( raw, clean ){ } // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_location_bias.js b/sanitizer/_location_bias.js index 4c9c7f8b..ee99e5e9 100644 --- a/sanitizer/_location_bias.js +++ b/sanitizer/_location_bias.js @@ -4,28 +4,29 @@ Set a focus.lat and focus.lon if specified in pelias config * @param {object} defaultParameters property of pelias config */ -function setup(defaultParameters){ +function _setup(defaultParameters){ + return { + sanitize: function sanitize(raw, clean){ + /* + check that: + 1. {object} raw exists + 2. pelias-config included the properties focus.point.lat and focus.point.lon + 3. raw.focus.point.lon and raw.focus.point.lat have not been set + */ + if (!_.isUndefined(raw) && + !_.isUndefined(defaultParameters['focus.point.lat']) && + !_.isUndefined(defaultParameters['focus.point.lon']) && + !_.has(raw, 'focus.point.lon') && + !_.has(raw, 'focus.point.lat') ) { - return function setLocationBias(raw, clean){ -/* -check that: -1. {object} raw exists -2. pelias-config included the properties focus.point.lat and focus.point.lon -3. raw.focus.point.lon and raw.focus.point.lat have not been set -*/ - if (!_.isUndefined(raw) && - !_.isUndefined(defaultParameters['focus.point.lat']) && - !_.isUndefined(defaultParameters['focus.point.lon']) && - !_.has(raw, 'focus.point.lon') && - !_.has(raw, 'focus.point.lat') ) { + raw['focus.point.lat'] = defaultParameters['focus.point.lat']; + raw['focus.point.lon'] = defaultParameters['focus.point.lon']; + } - raw['focus.point.lat'] = defaultParameters['focus.point.lat']; - raw['focus.point.lon'] = defaultParameters['focus.point.lon']; + return { errors: [], warnings: [] }; } - - return { errors: [], warnings: [] }; }; } // if focus.point.lat and focus.point.lon already exists, don't change -module.exports = setup; +module.exports = _setup; diff --git a/sanitizer/_single_scalar_parameters.js b/sanitizer/_single_scalar_parameters.js index 2b06cc65..4c49fc21 100644 --- a/sanitizer/_single_scalar_parameters.js +++ b/sanitizer/_single_scalar_parameters.js @@ -3,7 +3,7 @@ var _ = require('lodash'), check = require('check-types'); // validate inputs -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -22,4 +22,6 @@ function sanitize( raw, clean ){ } // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/sanitizer/_size.js b/sanitizer/_size.js index b2b5e62e..aaced3f9 100644 --- a/sanitizer/_size.js +++ b/sanitizer/_size.js @@ -5,40 +5,47 @@ var MIN_SIZE = 1, DEFAULT_SIZE = 10; // validate inputs, convert types and apply defaults -function setup( size_min, size_max, size_def ){ +function _setup( size_min, size_max, size_def ){ // allow caller to inject custom min/max/default values if( !check.number( size_min ) ){ size_min = MIN_SIZE; } if( !check.number( size_max ) ){ size_max = MAX_SIZE; } if( !check.number( size_def ) ){ size_def = DEFAULT_SIZE; } - return function sanitize( raw, clean ){ - - // error & warning messages - var messages = { errors: [], warnings: [] }; - - // coercions - clean.size = parseInt( raw.size, 10 ); - - // invalid numeric input - if( isNaN( clean.size ) ){ - clean.size = size_def; + return { + sanitize: function _sanitize( raw, clean ){ + + // error & warning messages + var messages = { errors: [], warnings: [] }; + + // coercions + clean.size = parseInt( raw.size, 10 ); + + // invalid numeric input + if( isNaN( clean.size ) ){ + clean.size = size_def; + } + // ensure size falls within defined range + else if( clean.size > size_max ){ + // set the max size + messages.warnings.push('out-of-range integer \'size\', using MAX_SIZE'); + clean.size = size_max; + } + else if( clean.size < size_min ){ + // set the min size + messages.warnings.push('out-of-range integer \'size\', using MIN_SIZE'); + clean.size = size_min; + } + + return messages; + }, + + expected: function _expected() { + // add size as a valid parameter for joi + return [{ name: 'size' }]; } - // ensure size falls within defined range - else if( clean.size > size_max ){ - // set the max size - messages.warnings.push('out-of-range integer \'size\', using MAX_SIZE'); - clean.size = size_max; - } - else if( clean.size < size_min ){ - // set the min size - messages.warnings.push('out-of-range integer \'size\', using MIN_SIZE'); - clean.size = size_min; - } - - return messages; }; } // export function -module.exports = setup; +module.exports = _setup; diff --git a/sanitizer/_sources_and_layers.js b/sanitizer/_sources_and_layers.js index fa0325b2..97b56049 100644 --- a/sanitizer/_sources_and_layers.js +++ b/sanitizer/_sources_and_layers.js @@ -5,7 +5,7 @@ var type_mapping = require( '../helper/type_mapping' ); * This sanitizer depends on clean.layers and clean.sources * so it has to be run after those sanitizers have been run */ -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ var messages = { errors: [], warnings: [] }; var possible_errors = []; @@ -34,4 +34,11 @@ function sanitize( raw, clean ){ return messages; } -module.exports = sanitize; +function _expected(){ + return [{ 'name': 'sources' }, { 'name': 'layers' }]; +} + +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_synthesize_analysis.js b/sanitizer/_synthesize_analysis.js index 6c702f9e..a4764549 100644 --- a/sanitizer/_synthesize_analysis.js +++ b/sanitizer/_synthesize_analysis.js @@ -35,7 +35,7 @@ function getHouseNumberField(analyzed_address) { } -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages const messages = { errors: [], warnings: [] }; @@ -86,5 +86,20 @@ function sanitize( raw, clean ){ return messages; } +function _expected() { + return [ + { 'name': 'venue' }, + { 'name': 'address' }, + { 'name': 'neighbourhood' }, + { 'name': 'borough' }, + { 'name': 'locality' }, + { 'name': 'county' }, + { 'name': 'region' }, + { 'name': 'postalcode' }, + { 'name': 'country' }]; +} // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_targets.js b/sanitizer/_targets.js index a8a50400..180f9442 100644 --- a/sanitizer/_targets.js +++ b/sanitizer/_targets.js @@ -5,70 +5,72 @@ function getValidKeys(mapping) { return _.uniq(Object.keys(mapping)).join(','); } -function setup( paramName, targetMap ) { - return function( raw, clean ){ - return sanitize( raw, clean, { - paramName: paramName, - targetMap: targetMap, - targetMapKeysString: getValidKeys(targetMap) - }); +function _setup( paramName, targetMap ) { + const opts = { + paramName: paramName, + targetMap: targetMap, + targetMapKeysString: getValidKeys(targetMap) }; -} -function sanitize( raw, clean, opts ) { - // error & warning messages - var messages = { errors: [], warnings: [] }; - - // the string of targets (comma delimeted) - var targetsString = raw[opts.paramName]; - - // trim whitespace - if( check.nonEmptyString( targetsString ) ){ - targetsString = targetsString.trim(); - - // param must be a valid non-empty string - if( !check.nonEmptyString( targetsString ) ){ - messages.errors.push( - opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString - ); - } - else { - - // split string in to array and lowercase each target string - var targets = targetsString.split(',').map( function( target ){ - return target.toLowerCase(); // lowercase inputs - }); - - // emit an error for each target *not* present in the targetMap - targets.filter( function( target ){ - return !opts.targetMap.hasOwnProperty(target); - }).forEach( function( target ){ - messages.errors.push( - '\'' + target + '\' is an invalid ' + opts.paramName + ' parameter. Valid options: ' + opts.targetMapKeysString - ); - }); + return { + sanitize: function _sanitize( raw, clean ) { + // error & warning messages + var messages = { errors: [], warnings: [] }; + + // the string of targets (comma delimeted) + var targetsString = raw[opts.paramName]; + + // trim whitespace + if( check.nonEmptyString( targetsString ) ){ + targetsString = targetsString.trim(); + + // param must be a valid non-empty string + if( !check.nonEmptyString( targetsString ) ){ + messages.errors.push( + opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString + ); + } + else { + + // split string in to array and lowercase each target string + var targets = targetsString.split(',').map( function( target ){ + return target.toLowerCase(); // lowercase inputs + }); - // only set types value when no error occured - if( !messages.errors.length ){ - clean[opts.paramName] = targets.reduce(function(acc, target) { - return acc.concat(opts.targetMap[target]); - }, []); + // emit an error for each target *not* present in the targetMap + targets.filter( function( target ){ + return !opts.targetMap.hasOwnProperty(target); + }).forEach( function( target ){ + messages.errors.push( + '\'' + target + '\' is an invalid ' + opts.paramName + ' parameter. Valid options: ' + opts.targetMapKeysString + ); + }); - // dedupe in case aliases expanded to common things or user typed in duplicates - clean[opts.paramName] = _.uniq(clean[opts.paramName]); + // only set types value when no error occured + if( !messages.errors.length ){ + clean[opts.paramName] = targets.reduce(function(acc, target) { + return acc.concat(opts.targetMap[target]); + }, []); + + // dedupe in case aliases expanded to common things or user typed in duplicates + clean[opts.paramName] = _.uniq(clean[opts.paramName]); + } + } } - } - } - // string is empty - else if( check.string( targetsString ) ){ - messages.errors.push( - opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString - ); - } + // string is empty + else if( check.string( targetsString ) ){ + messages.errors.push( + opts.paramName + ' parameter cannot be an empty string. Valid options: ' + opts.targetMapKeysString + ); + } - return messages; -} + return messages; + } // end of _sanitize function + + }; // end of object to be returned +} // end of _setup function + -module.exports = setup; +module.exports = _setup; diff --git a/sanitizer/_text.js b/sanitizer/_text.js index 82662473..19e5f5b5 100644 --- a/sanitizer/_text.js +++ b/sanitizer/_text.js @@ -2,7 +2,8 @@ const check = require('check-types'); const _ = require('lodash'); // validate texts, convert types and apply defaults -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ + // error & warning messages const messages = { errors: [], warnings: [] }; @@ -20,5 +21,11 @@ function sanitize( raw, clean ){ return messages; } +function _expected(){ + return [{ name: 'text' }, { name: 'parsed_text' }]; +} // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize, + expected: _expected +}); diff --git a/sanitizer/_text_addressit.js b/sanitizer/_text_addressit.js index 68a293bf..6c34699a 100644 --- a/sanitizer/_text_addressit.js +++ b/sanitizer/_text_addressit.js @@ -5,7 +5,7 @@ var _ = require('lodash'); var logger = require('pelias-logger').get('api'); // validate texts, convert types and apply defaults -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -36,9 +36,9 @@ function sanitize( raw, clean ){ } // export function -module.exports = sanitize; - - +module.exports = () => ({ + sanitize: _sanitize +}); // this is the addressit functionality from https://github.com/pelias/text-analyzer/blob/master/src/addressItParser.js var DELIM = ','; diff --git a/sanitizer/_tokenizer.js b/sanitizer/_tokenizer.js index 328cd1aa..18b5d3e0 100644 --- a/sanitizer/_tokenizer.js +++ b/sanitizer/_tokenizer.js @@ -14,7 +14,7 @@ var check = require('check-types'); note: this sanitizer should run *after* the '_text' sanitizer so it can use the output of clean.parsed_text where available. **/ -function sanitize( raw, clean ){ +function _sanitize( raw, clean ){ // error & warning messages var messages = { errors: [], warnings: [] }; @@ -103,4 +103,6 @@ function sanitize( raw, clean ){ } // export function -module.exports = sanitize; +module.exports = () => ({ + sanitize: _sanitize +}); diff --git a/test/unit/sanitizer/_boundary_country.js b/test/unit/sanitizer/_boundary_country.js index 9411acdf..7649da66 100644 --- a/test/unit/sanitizer/_boundary_country.js +++ b/test/unit/sanitizer/_boundary_country.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_boundary_country'); +var sanitizer = require('../../../sanitizer/_boundary_country')(); module.exports.tests = {}; @@ -6,7 +6,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('raw w/o boundary should set boundary.country undefined', function(t) { var raw = { }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], undefined, 'should be undefined'); t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); t.end(); @@ -15,7 +15,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('boundary.country explicitly undefined in raw should leave boundary.country undefined', function(t) { var raw = { 'boundary.country': undefined }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], undefined, 'should be undefined'); t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); t.end(); @@ -24,7 +24,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('non-string boundary.country should set boundary.country to undefined and return warning', function(t) { var raw = { 'boundary.country': ['this isn\'t a string primitive'] }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], undefined, 'should be undefined'); t.deepEquals(errorsAndWarnings, { errors: ['boundary.country is not a string'], warnings: [] }, 'non-string country warning'); t.end(); @@ -33,7 +33,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('iso2 boundary.country in raw should set boundary.country to ISO3 uppercased', function(t) { var raw = { 'boundary.country': 'aq' }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], 'ATA', 'should be uppercased ISO3'); t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); t.end(); @@ -42,7 +42,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('iso3 boundary.country in raw should set boundary.country to matching ISO3 uppercased', function(t) { var raw = { 'boundary.country': 'aTa' }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], 'ATA', 'should be uppercased ISO3'); t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); t.end(); @@ -51,7 +51,7 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('unknown 2-character boundary.country should set boundary.country to undefined', function(t) { var raw = { 'boundary.country': 'zq' }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], undefined, 'should be undefined'); t.deepEquals(errorsAndWarnings, { errors: ['zq is not a valid ISO2/ISO3 country code'], warnings: [] }, 'country not found warning`'); t.end(); @@ -60,17 +60,24 @@ module.exports.tests.sanitize_boundary_country = function(test, common) { test('unknown 3-character boundary.country should set boundary.country to undefined', function(t) { var raw = { 'boundary.country': 'zqx' }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); + var errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.country'], undefined, 'should be undefined'); t.deepEquals(errorsAndWarnings, { errors: ['zqx is not a valid ISO2/ISO3 country code'], warnings: [] }, 'country not found warning`'); t.end(); }); + test('return an array of valid parameters in object form for Joi schema validation', (t) => { + const expected = [{ name: 'boundary.country' }]; + const validParameters = sanitizer.expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); + }; module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _boundary_country ' + name, testFunction); + return tape('SANITIZE _boundary_country ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_categories.js b/test/unit/sanitizer/_categories.js index 64e9555f..84589098 100644 --- a/test/unit/sanitizer/_categories.js +++ b/test/unit/sanitizer/_categories.js @@ -1,4 +1,4 @@ -var sanitize = require( '../../../sanitizer/_categories'); +var sanitizer = require( '../../../sanitizer/_categories')(); module.exports.tests = {}; @@ -9,7 +9,7 @@ module.exports.tests.no_categories = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.equal(req.clean.categories, undefined, 'no categories should be defined'); t.deepEqual(messages.errors, [], 'no error returned'); @@ -27,7 +27,7 @@ module.exports.tests.no_categories = function(test, common) { var expected_error = 'Categories parameter cannot be left blank. See documentation of service for valid options.'; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.equal(req.clean.categories, undefined, 'no categories should be defined'); t.deepEqual(messages.errors.length, 1, 'error returned'); @@ -46,7 +46,7 @@ module.exports.tests.no_categories = function(test, common) { var expected_error = 'Invalid categories parameter value(s). See documentation of service for valid options.'; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.equal(req.clean.categories, undefined, 'no categories should be defined'); t.deepEqual(messages.errors.length, 1, 'error returned'); @@ -74,7 +74,7 @@ module.exports.tests.valid_categories = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean, validCategories); + var messages = sanitizer.sanitize(req.query, req.clean, validCategories); t.deepEqual(req.clean.categories, ['food'], 'categories should contain food'); t.deepEqual(messages.errors, [], 'no error returned'); @@ -95,7 +95,7 @@ module.exports.tests.valid_categories = function(test, common) { }; var expectedCategories = ['food', 'health']; - var messages = sanitize(req.query, req.clean, validCategories); + var messages = sanitizer.sanitize(req.query, req.clean, validCategories); t.deepEqual(req.clean.categories, expectedCategories, 'clean.categories should be an array with proper values'); @@ -130,7 +130,7 @@ module.exports.tests.invalid_categories = function(test, common) { warnings: [] }; - var messages = sanitize(req.query, req.clean, validCategories); + var messages = sanitizer.sanitize(req.query, req.clean, validCategories); t.deepEqual(messages, expected_messages, 'error with message returned'); t.equal(req.clean.categories, undefined, 'clean.categories should remain empty'); @@ -151,17 +151,26 @@ module.exports.tests.invalid_categories = function(test, common) { warnings: [] }; - var messages = sanitize(req.query, req.clean, validCategories); + var messages = sanitizer.sanitize(req.query, req.clean, validCategories); t.deepEqual(messages, expected_messages, 'error with message returned'); t.equal(req.clean.categories, undefined, 'clean.categories should remain empty'); t.end(); }); + + test('return an array of valid parameters in object form for Joi schema validation', (t) => { + const expected = [{ name: 'categories' }]; + + const validParameters = sanitizer.expected(); + + t.deepEquals(validParameters, expected); + t.end(); + }); }; module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _categories ' + name, testFunction); + return tape('SANITIZE _categories ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_city_name_standardizer.js b/test/unit/sanitizer/_city_name_standardizer.js index da362868..5a01e58b 100644 --- a/test/unit/sanitizer/_city_name_standardizer.js +++ b/test/unit/sanitizer/_city_name_standardizer.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const sanitizer = require('../../../sanitizer/_city_name_standardizer'); +const sanitizer = require('../../../sanitizer/_city_name_standardizer')(); module.exports.tests = {}; @@ -13,7 +13,7 @@ module.exports.tests.text_parser = function(test, common) { const expected_clean = { }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -39,7 +39,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -77,7 +77,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -115,7 +115,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -153,7 +153,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -191,7 +191,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -215,7 +215,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -239,7 +239,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -263,7 +263,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); diff --git a/test/unit/sanitizer/_deprecate_quattroshapes.js b/test/unit/sanitizer/_deprecate_quattroshapes.js index 61b581aa..d468b97f 100644 --- a/test/unit/sanitizer/_deprecate_quattroshapes.js +++ b/test/unit/sanitizer/_deprecate_quattroshapes.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_deprecate_quattroshapes'); +var sanitizer = require('../../../sanitizer/_deprecate_quattroshapes')(); module.exports.tests = {}; @@ -7,7 +7,7 @@ module.exports.tests.warning_message_1 = function(test, common) { var raw = { sources: 'qs' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEquals(messages, { errors: [], warnings: ['You are using Quattroshapes as a data source in this query. ' + @@ -27,7 +27,7 @@ module.exports.tests.warning_message_2 = function(test, common) { var raw = { sources: 'quattroshapes' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEquals(messages, { errors: [], warnings: ['You are using Quattroshapes as a data source in this query. ' + @@ -47,7 +47,7 @@ module.exports.tests.rewrite = function(test, common) { var raw = { sources: 'qs,quattroshapes,qs,quattroshapes,osm' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); t.equals(raw.sources,'osm,whosonfirst','use wof instead of qs'); t.end(); @@ -56,7 +56,7 @@ module.exports.tests.rewrite = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _deprecate_quattroshapes ' + name, testFunction); + return tape('SANITIZE _deprecate_quattroshapes ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_flag_bool.js b/test/unit/sanitizer/_flag_bool.js index 55ecdc94..04e31f49 100644 --- a/test/unit/sanitizer/_flag_bool.js +++ b/test/unit/sanitizer/_flag_bool.js @@ -1,5 +1,4 @@ var sanitizer = require('../../../sanitizer/_flag_bool'); -var sanitize = sanitizer('dirty_param', true); module.exports.tests = {}; @@ -9,7 +8,7 @@ module.exports.tests.sanitize_private = function(test, common) { test('invalid dirty_param ' + value, function (t) { var raw = {dirty_param: value}; var clean = {}; - sanitize(raw, clean); + sanitizer('dirty_param', true).sanitize(raw, clean); t.equal(clean.dirty_param, false, 'default clean value set (to false)'); t.end(); }); @@ -20,7 +19,7 @@ module.exports.tests.sanitize_private = function(test, common) { test('valid dirty_param ' + value, function (t) { var raw = {dirty_param: value}; var clean = {}; - sanitize(raw, clean); + sanitizer('dirty_param', true).sanitize(raw, clean); t.equal(clean.dirty_param, true, 'clean value set to true'); t.end(); }); @@ -31,7 +30,7 @@ module.exports.tests.sanitize_private = function(test, common) { test('test setting false explicitly ' + value, function (t) { var raw = {dirty_param: value}; var clean = {}; - sanitize(raw, clean); + sanitizer('dirty_param', true).sanitize(raw, clean); t.equal(clean.dirty_param, false, 'clean value set to false'); t.end(); }); @@ -45,17 +44,26 @@ module.exports.tests.validate_default_behavior = function(test, common) { var sanitize_true = sanitizer('foo_bar', defaultValue); var raw = {}; var clean = {}; - sanitize_true(raw, clean); + sanitize_true.sanitize(raw, clean); t.equal(clean.foo_bar, defaultValue, 'foo_bar set to ' + defaultValue); t.end(); }); }); }; +module.exports.tests.check_valid_parameters = function(test, common) { + test('return an array of valid parameters in object form for Joi schema validation', (t) => { + const expected = [{ name: 'private' }]; + const validParameters = sanitizer('dirty_param', true).expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _flag_bool: ' + name, testFunction); + return tape('SANITIZE _flag_bool: ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_geo_reverse.js b/test/unit/sanitizer/_geo_reverse.js index 223cf9fa..c2fa6bd9 100644 --- a/test/unit/sanitizer/_geo_reverse.js +++ b/test/unit/sanitizer/_geo_reverse.js @@ -1,6 +1,6 @@ 'use strict'; -const sanitize = require('../../../sanitizer/_geo_reverse'); +const sanitizer = require('../../../sanitizer/_geo_reverse')(); const defaults = require('../../../query/reverse_defaults'); module.exports.tests = {}; @@ -13,7 +13,7 @@ module.exports.tests.warning_situations = (test, common) => { 'boundary.circle.lat': '13.131313' }; const clean = {}; - const errorsAndWarnings = sanitize(raw, clean); + const errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.circle.lat'], 12.121212, 'should be set to point.lat'); t.deepEquals(errorsAndWarnings, { @@ -31,7 +31,7 @@ module.exports.tests.warning_situations = (test, common) => { 'boundary.circle.lon': '31.313131' }; const clean = {}; - const errorsAndWarnings = sanitize(raw, clean); + const errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(clean['boundary.circle.lon'], 21.212121, 'should be set to point.lon'); t.deepEquals(errorsAndWarnings, { @@ -49,7 +49,7 @@ module.exports.tests.warning_situations = (test, common) => { 'boundary.circle.radius': '17' }; const clean = {}; - const errorsAndWarnings = sanitize(raw, clean); + const errorsAndWarnings = sanitizer.sanitize(raw, clean); // t.equals(clean['boundary.circle.radius'], 12.121212, 'should be set to point.lat') t.deepEquals(errorsAndWarnings, { @@ -70,7 +70,7 @@ module.exports.tests.success_conditions = (test, common) => { 'boundary.circle.radius': '3248732857km' // this will never be the default }; const clean = {}; - const errorsAndWarnings = sanitize(raw, clean); + const errorsAndWarnings = sanitizer.sanitize(raw, clean); t.equals(raw['boundary.circle.lat'], 12.121212); t.equals(raw['boundary.circle.lon'], 21.212121); diff --git a/test/unit/sanitizer/_geonames_deprecation.js b/test/unit/sanitizer/_geonames_deprecation.js index f7676db3..41bd86c6 100644 --- a/test/unit/sanitizer/_geonames_deprecation.js +++ b/test/unit/sanitizer/_geonames_deprecation.js @@ -1,4 +1,4 @@ -const geonames_deprecation = require('../../../sanitizer/_geonames_deprecation'); +const sanitizer = require('../../../sanitizer/_geonames_deprecation')(); module.exports.tests = {}; @@ -6,7 +6,7 @@ module.exports.tests.no_warnings_or_errors_conditions = (test, common) => { test('undefined sources should add neither warnings nor errors', (t) => { const clean = {}; - const messages = geonames_deprecation(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean, {}); t.deepEquals(messages, { errors: [], warnings: [] }); @@ -19,7 +19,7 @@ module.exports.tests.no_warnings_or_errors_conditions = (test, common) => { sources: ['source 1', 'source 2'], }; - const messages = geonames_deprecation(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean.sources, ['source 1', 'source 2']); t.deepEquals(messages, { errors: [], warnings: [] }); @@ -36,7 +36,7 @@ module.exports.tests.error_conditions = (test, common) => { sources: [sources] }; - const messages = geonames_deprecation(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean.sources, [sources]); t.deepEquals(messages.errors, ['/reverse does not support geonames']); @@ -57,7 +57,7 @@ module.exports.tests.warning_conditions = (test, common) => { sources: ['another source', sources, 'yet another source'] }; - const messages = geonames_deprecation(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(clean.sources, ['another source', 'yet another source']); t.deepEquals(messages.errors, []); @@ -73,7 +73,7 @@ module.exports.tests.warning_conditions = (test, common) => { module.exports.all = (tape, common) => { function test(name, testFunction) { - return tape(`SANTIZE _geonames_deprecation ${name}`, testFunction); + return tape(`SANITIZE _geonames_deprecation ${name}`, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_geonames_warnings.js b/test/unit/sanitizer/_geonames_warnings.js index 47881781..d4e20e6c 100644 --- a/test/unit/sanitizer/_geonames_warnings.js +++ b/test/unit/sanitizer/_geonames_warnings.js @@ -1,6 +1,6 @@ const _ = require('lodash'); -const geonames_warnings = require('../../../sanitizer/_geonames_warnings'); +const sanitizer = require('../../../sanitizer/_geonames_warnings')(); const nonAdminProperties = ['number', 'street', 'query', 'category']; const adminProperties = ['neighbourhood', 'borough', 'city', 'county', 'state', 'postalcode', 'country']; @@ -13,7 +13,7 @@ module.exports.tests.no_errors = (test, common) => { sources: ['geonames'], }; - const messages = geonames_warnings(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(messages, { errors: [], warnings: [] }); t.end(); @@ -30,7 +30,7 @@ module.exports.tests.no_errors = (test, common) => { clean.parsed_text[nonAdminProperty] = `${nonAdminProperty} value`; clean.parsed_text[adminProperty] = `${adminProperty} value`; - const messages = geonames_warnings(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(messages, { errors: [], warnings: [] }); @@ -50,7 +50,7 @@ module.exports.tests.no_errors = (test, common) => { clean.parsed_text[nonAdminProperty] = `${nonAdminProperty} value`; clean.parsed_text[adminProperty] = `${adminProperty} value`; - const messages = geonames_warnings(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(messages, { errors: [], warnings: [] }); @@ -68,7 +68,7 @@ module.exports.tests.error_conditions = (test, common) => { const clean = _.set({ sources: ['geonames'] }, ['parsed_text', property], `${property} value`); - const messages = geonames_warnings(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(messages.errors, ['input contains only administrative area data, ' + 'no results will be returned when sources=geonames']); @@ -87,7 +87,7 @@ module.exports.tests.warning_conditions = (test, common) => { const clean = _.set({ sources: ['source 1', 'geonames', 'source 2'] }, ['parsed_text', property], `${property} value`); - const messages = geonames_warnings(undefined, clean); + const messages = sanitizer.sanitize(undefined, clean); t.deepEquals(messages.errors, []); t.deepEquals(messages.warnings, ['input contains only administrative area data, ' + diff --git a/test/unit/sanitizer/_ids.js b/test/unit/sanitizer/_ids.js index e1dfb680..f376c2ca 100644 --- a/test/unit/sanitizer/_ids.js +++ b/test/unit/sanitizer/_ids.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_ids'); +var sanitizer = require('../../../sanitizer/_ids')(); var delimiter = ':'; var type_mapping = require('../../../helper/type_mapping'); @@ -15,7 +15,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: '' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], lengthError, 'ids length error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -26,7 +26,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: ':' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], formatError(':'), 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -37,7 +37,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: '::' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], formatError('::'), 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -48,7 +48,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: 'geoname:' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], formatError('geoname:'), 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -59,7 +59,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: ':234' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], formatError(':234'), 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -72,7 +72,7 @@ module.exports.tests.invalid_ids = function(test, common) { var expected_error = 'invalidsource is invalid. It must be one of these values - [' + Object.keys(type_mapping.source_mapping).join(', ') + ']'; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], expected_error, 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -83,7 +83,7 @@ module.exports.tests.invalid_ids = function(test, common) { var raw = { ids: 'geonames:23' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors[0], formatError('geonames:23'), 'format error returned'); t.equal(clean.ids, undefined, 'ids unset in clean object'); @@ -96,7 +96,7 @@ module.exports.tests.valid_ids = function(test, common) { var raw = { ids: 'openaddresses:address:20' }; var clean = {}; - var messages = sanitize( raw, clean ); + var messages = sanitizer.sanitize( raw, clean ); var expected_ids = [{ source: 'openaddresses', @@ -112,7 +112,7 @@ test('ids: valid short input (openaddresses)', function(t) { var raw = { ids: 'oa:address:20' }; var clean = {}; - var messages = sanitize( raw, clean ); + var messages = sanitizer.sanitize( raw, clean ); var expected_ids = [{ source: 'openaddresses', @@ -133,7 +133,7 @@ test('ids: valid short input (openaddresses)', function(t) { id: 'node:500', }]; - var messages = sanitize( raw, clean ); + var messages = sanitizer.sanitize( raw, clean ); t.deepEqual( messages.errors, [], ' no errors'); t.deepEqual( clean.ids, expected_ids, 'osm has node: or way: in id field'); @@ -149,7 +149,7 @@ test('ids: valid short input (openaddresses)', function(t) { id: 'node:500', }]; - var messages = sanitize( raw, clean ); + var messages = sanitizer.sanitize( raw, clean ); t.deepEqual( messages.errors, [], ' no errors'); t.deepEqual( clean.ids, expected_ids, 'osm has node: or way: in id field'); @@ -162,7 +162,7 @@ module.exports.tests.multiple_ids = function(test, common) { var raw = { ids: 'geonames:venue:1,openstreetmap:address:way:2' }; var clean = {}; - var messages = sanitize( raw, clean); + var messages = sanitizer.sanitize( raw, clean); var expected_ids = [ { source: 'geonames', @@ -186,7 +186,7 @@ module.exports.tests.de_dupe = function(test, common) { var raw = { ids: 'geonames:venue:1,openstreetmap:venue:node:2,geonames:venue:1' }; var clean = {}; - var messages = sanitize( raw, clean ); + var messages = sanitizer.sanitize( raw, clean ); var expected_ids = [ { source: 'geonames', @@ -204,9 +204,18 @@ module.exports.tests.de_dupe = function(test, common) { }); }; +module.exports.tests.valid_Parameters = function(test, common) { + test('return an array of valid parameters in object form for Joi schema validation', (t) => { + const expected = [{ name: 'ids' }]; + const validParameters = sanitizer.expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); +}; + module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _ids ' + name, testFunction); + return tape('SANITIZE _ids ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_iso2_to_iso3.js b/test/unit/sanitizer/_iso2_to_iso3.js index 7d27499b..27fe7eb6 100644 --- a/test/unit/sanitizer/_iso2_to_iso3.js +++ b/test/unit/sanitizer/_iso2_to_iso3.js @@ -1,4 +1,4 @@ -const sanitizer = require('../../../sanitizer/_iso2_to_iso3'); +const sanitizer = require('../../../sanitizer/_iso2_to_iso3')(); module.exports.tests = {}; @@ -12,7 +12,7 @@ module.exports.tests.text_parser = function(test, common) { const expected_clean = { }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -38,7 +38,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -64,7 +64,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -90,7 +90,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -103,7 +103,7 @@ module.exports.tests.text_parser = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('sanitizer _iso2_to_iso3: ' + name, testFunction); + return tape('SANITIZE _iso2_to_iso3: ' + name, testFunction); } for( const testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_layers.js b/test/unit/sanitizer/_layers.js index 5a0c0693..11c618c8 100644 --- a/test/unit/sanitizer/_layers.js +++ b/test/unit/sanitizer/_layers.js @@ -1,12 +1,12 @@ var type_mapping = require('../../../helper/type_mapping'); -var sanitize = require('../../../sanitizer/_targets')('layers', type_mapping.layer_mapping); +var sanitizer = require('../../../sanitizer/_targets')('layers', type_mapping.layer_mapping); module.exports.tests = {}; module.exports.tests.sanitize_layers = function(test, common) { test('unspecified', function(t) { - var messages = sanitize({ layers: undefined }, {}); + var messages = sanitizer.sanitize({ layers: undefined }, {}); t.equal(messages.errors.length, 0, 'no errors'); t.end(); }); @@ -15,7 +15,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'test_layer' }; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); var msg = ' is an invalid layers parameter. Valid options: '; t.equal(messages.errors.length, 1, 'errors set'); @@ -28,7 +28,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'venue' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var venue_layers = ['venue']; t.deepEqual(clean.layers, venue_layers, 'venue layers set'); @@ -39,7 +39,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'coarse' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var admin_layers = [ 'continent', 'country', 'dependency', 'macroregion', 'region', 'locality', 'localadmin', 'macrocounty', 'county', @@ -53,7 +53,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'address' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); t.deepEqual(clean.layers, ['address'], 'address layer set'); t.end(); @@ -63,7 +63,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'venue,country,region' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var expected_layers = ['venue', 'country', 'region']; t.deepEqual(clean.layers, expected_layers, 'venue + regular layers'); @@ -74,7 +74,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'coarse,country' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var expected_layers = [ 'continent', 'country', 'dependency', 'macroregion', 'region', 'locality', 'localadmin', 'macrocounty', 'county', @@ -88,7 +88,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'address,country,locality' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var expected_layers = ['address', 'country', 'locality' ]; t.deepEqual(clean.layers, expected_layers, 'address + regular layers set'); @@ -99,7 +99,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'venue,country' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var expected_layers = ['venue', 'country']; t.deepEqual(clean.layers, expected_layers, 'venue layers found (no duplicates)'); @@ -110,7 +110,7 @@ module.exports.tests.sanitize_layers = function(test, common) { var raw = { layers: 'venue,coarse' }; var clean = {}; - sanitize(raw, clean); + sanitizer.sanitize(raw, clean); var coarse_layers = [ 'continent', 'country', 'dependency', 'macroregion', 'region', 'locality', 'localadmin', @@ -126,7 +126,7 @@ module.exports.tests.sanitize_layers = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _layers ' + name, testFunction); + return tape('SANITIZE _layers ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_location_bias.js b/test/unit/sanitizer/_location_bias.js index 6778f045..35afca38 100644 --- a/test/unit/sanitizer/_location_bias.js +++ b/test/unit/sanitizer/_location_bias.js @@ -1,4 +1,4 @@ -const setup = require('../../../sanitizer/_location_bias'); +const sanitizer = require('../../../sanitizer/_location_bias'); module.exports.tests = {}; @@ -8,14 +8,14 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lat': 12.12121212, 'focus.point.lon': 21.21212121 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); const raw = {}; const expected = { 'focus.point.lat': 12.12121212, 'focus.point.lon': 21.21212121 }; - locationBias(raw, undefined); + locationBias.sanitize(raw, undefined); t.deepEqual(raw, expected, 'focus point should be set'); t.end(); @@ -26,9 +26,9 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lat': 12.12121212, 'focus.point.lon': 21.21212121 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); - locationBias(undefined, undefined); + locationBias.sanitize(undefined, undefined); t.deepEqual(undefined, undefined, 'should be unmodified' ); t.end(); }); @@ -37,11 +37,11 @@ module.exports.tests.setLocationBias = function(test, common) { const defaultParameters = { 'focus.point.lon': 12.2121212 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); const raw = {}; const expected = {}; - locationBias(raw, undefined); + locationBias.sanitize(raw, undefined); t.deepEqual(raw, expected, 'should be unmodified' ); t.end(); }); @@ -50,11 +50,11 @@ module.exports.tests.setLocationBias = function(test, common) { const defaultParameters = { 'focus.point.lat': 12.2121212 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); const raw = {}; const expected = {}; - locationBias(raw, undefined); + locationBias.sanitize(raw, undefined); t.deepEqual(raw, expected, 'should be unmodified' ); t.end(); }); @@ -64,7 +64,7 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lon': 12.2121212, 'focus.point.lat': 12.2121212 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); const raw = { 'focus.point.lon': 43.4343434 }; @@ -72,7 +72,7 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lon': 43.4343434 }; - locationBias(raw, undefined); + locationBias.sanitize(raw, undefined); t.deepEqual(raw, expected, 'should be unmodified' ); t.end(); }); @@ -82,7 +82,7 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lon': 12.2121212, 'focus.point.lat': 12.2121212 }; - const locationBias = setup(defaultParameters); + const locationBias = sanitizer(defaultParameters); const raw = { 'focus.point.lat': 34.3434343 }; @@ -90,7 +90,7 @@ module.exports.tests.setLocationBias = function(test, common) { 'focus.point.lat': 34.3434343 }; - locationBias(raw, undefined); + locationBias.sanitize(raw, undefined); t.deepEqual(raw, expected, 'should be unmodified' ); t.end(); }); diff --git a/test/unit/sanitizer/_single_scalar_parameters.js b/test/unit/sanitizer/_single_scalar_parameters.js index c3322703..1710caa8 100644 --- a/test/unit/sanitizer/_single_scalar_parameters.js +++ b/test/unit/sanitizer/_single_scalar_parameters.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_single_scalar_parameters'); +var sanitizer = require('../../../sanitizer/_single_scalar_parameters')(); module.exports.tests = {}; @@ -10,8 +10,8 @@ module.exports.tests.single_scalar_parameters = function(test, common) { arrayParameter2: ['value3'] }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); - t.deepEquals(errorsAndWarnings, { + var messages = sanitizer.sanitize(raw, clean); + t.deepEquals(messages, { errors: [ '\'arrayParameter1\' parameter can only have one value', '\'arrayParameter2\' parameter can only have one value', @@ -33,8 +33,8 @@ module.exports.tests.single_scalar_parameters = function(test, common) { objectParameter2: { } }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); - t.deepEquals(errorsAndWarnings, { + var messages = sanitizer.sanitize(raw, clean); + t.deepEquals(messages, { errors: [ '\'objectParameter1\' parameter must be a scalar', '\'objectParameter2\' parameter must be a scalar' @@ -51,8 +51,8 @@ module.exports.tests.single_scalar_parameters = function(test, common) { test('request with all scalar parameters should return empty errors', function(t) { var raw = { scalarParameter1: 'value1', scalarParameter2: 2, scalarParameter3: true }; var clean = {}; - var errorsAndWarnings = sanitize(raw, clean); - t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }); + var messages = sanitizer.sanitize(raw, clean); + t.deepEquals(messages, { errors: [], warnings: [] }); t.end(); }); diff --git a/test/unit/sanitizer/_size.js b/test/unit/sanitizer/_size.js index 6553685d..0bd1e415 100644 --- a/test/unit/sanitizer/_size.js +++ b/test/unit/sanitizer/_size.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_size'); +var sanitizer = require('../../../sanitizer/_size'); module.exports.tests = {}; @@ -6,7 +6,7 @@ module.exports.tests.sanitize_size = function(test, common) { test('size=0', function(t) { var raw = { size: 0 }; var clean = {}; - var res = sanitize(/*defaults*/)(raw, clean); + var res = sanitizer(/*defaults*/).sanitize(raw, clean); t.equal(res.errors.length, 0, 'should return no errors'); t.equal(res.warnings.length, 1, 'should return warning'); t.equal(res.warnings[0], 'out-of-range integer \'size\', using MIN_SIZE', 'check warning text'); @@ -17,7 +17,7 @@ module.exports.tests.sanitize_size = function(test, common) { test('size=10000', function(t) { var raw = { size: 10000 }; var clean = {}; - var res = sanitize(/*defaults*/)(raw, clean); + var res = sanitizer(/*defaults*/).sanitize(raw, clean); t.equal(res.errors.length, 0, 'should return no errors'); t.equal(res.warnings.length, 1, 'should return warning'); t.equal(res.warnings[0], 'out-of-range integer \'size\', using MAX_SIZE', 'check warning text'); @@ -28,20 +28,26 @@ module.exports.tests.sanitize_size = function(test, common) { test('size not set', function(t) { var raw = {}; var clean = {}; - var res = sanitize(/*defaults*/)(raw, clean); + var res = sanitizer(/*defaults*/).sanitize(raw, clean); t.equal(res.errors.length, 0, 'should return no errors'); t.equal(res.warnings.length, 0, 'should return no warning'); t.equal(clean.size, 10, 'default to 10'); t.end(); }); + test('return an array of valid parameters in object form for Joi schema validation', function(t) { + const expected = [{ name: 'size' }]; + const validParameters = sanitizer(/*defaults*/).expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); var valid_sizes = [5, '5', 5.5, '5.5']; valid_sizes.forEach(function (size) { test('size=' + size, function (t) { var raw = {size: size}; var clean = {}; - var res = sanitize(/*defaults*/)(raw, clean); + var res = sanitizer(/*defaults*/).sanitize(raw, clean); t.equal(res.errors.length, 0, 'should return no errors'); t.equal(res.warnings.length, 0, 'should return warning'); t.equal(clean.size, 5, 'set to correct integer'); @@ -52,7 +58,7 @@ module.exports.tests.sanitize_size = function(test, common) { module.exports.all = function (tape, common) { function test(name, testFunction) { - return tape('SANTIZE _size ' + name, testFunction); + return tape('SANITIZE _size ' + name, testFunction); } for( var testCase in module.exports.tests ){ diff --git a/test/unit/sanitizer/_sources.js b/test/unit/sanitizer/_sources.js index e8eb8ef5..e14f88dc 100644 --- a/test/unit/sanitizer/_sources.js +++ b/test/unit/sanitizer/_sources.js @@ -1,5 +1,5 @@ var type_mapping = require('../../../helper/type_mapping'); -var sanitize = require( '../../../sanitizer/_targets' )('sources', type_mapping.source_mapping); +var sanitizer = require( '../../../sanitizer/_targets' )('sources', type_mapping.source_mapping); var success_messages = { error: false }; @@ -12,7 +12,7 @@ module.exports.tests.no_sources = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.equal(req.clean.sources, undefined, 'no sources should be defined'); t.deepEqual(messages.errors, [], 'no error returned'); @@ -31,7 +31,7 @@ module.exports.tests.no_sources = function(test, common) { var expected_error = 'sources parameter cannot be an empty string. ' + 'Valid options: osm,oa,gn,wof,openstreetmap,openaddresses,geonames,whosonfirst'; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.equal(req.clean.sources, undefined, 'no sources should be defined'); t.deepEqual(messages.errors.length, 1, 'error returned'); @@ -50,7 +50,7 @@ module.exports.tests.valid_sources = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.deepEqual(req.clean.sources, ['geonames'], 'sources should contain geonames'); t.deepEqual(messages.errors, [], 'no error returned'); @@ -67,7 +67,7 @@ module.exports.tests.valid_sources = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.deepEqual(req.clean.sources, ['openstreetmap'], 'abbreviation is expanded to full version'); t.deepEqual(messages.errors, [], 'no error returned'); @@ -83,7 +83,7 @@ module.exports.tests.valid_sources = function(test, common) { clean: { } }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.deepEqual(req.clean.sources, ['openstreetmap', 'openaddresses'], 'clean.sources should contain openstreetmap and openadresses'); @@ -109,7 +109,7 @@ module.exports.tests.invalid_sources = function(test, common) { warnings: [] }; - var messages = sanitize(req.query, req.clean); + var messages = sanitizer.sanitize(req.query, req.clean); t.deepEqual(messages, expected_messages, 'error with message returned'); t.equal(req.clean.sources, undefined, 'clean.sources should remain empty'); diff --git a/test/unit/sanitizer/_sources_and_layers.js b/test/unit/sanitizer/_sources_and_layers.js index f506c1f9..b0266095 100644 --- a/test/unit/sanitizer/_sources_and_layers.js +++ b/test/unit/sanitizer/_sources_and_layers.js @@ -1,4 +1,4 @@ -var sanitize = require('../../../sanitizer/_sources_and_layers'); +var sanitizer = require('../../../sanitizer/_sources_and_layers')(); var type_mapping = require('../../../helper/type_mapping'); @@ -9,7 +9,7 @@ module.exports.tests.inactive = function(test, common) { var raw = {}; var clean = {}; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -20,7 +20,7 @@ module.exports.tests.inactive = function(test, common) { var raw = {}; var clean = { layers: ['venue'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -31,7 +31,7 @@ module.exports.tests.inactive = function(test, common) { var raw = {}; var clean = { sources: ['openstreetmap'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -44,7 +44,7 @@ module.exports.tests.no_errors = function(test, common) { var raw = {}; var clean = { sources: ['openstreetmap'], layers: ['venue'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -55,7 +55,7 @@ test('valid combination', function(t) { var raw = {}; var clean = { sources: ['geonames'], layers: ['borough'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -66,7 +66,7 @@ test('valid combination', function(t) { var raw = {}; var clean = { sources: ['geonames'], layers: ['macroregion'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -77,7 +77,7 @@ test('valid combination', function(t) { var raw = {}; var clean = { sources: ['whosonfirst'], layers: ['venue'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -88,7 +88,7 @@ test('valid combination', function(t) { var raw = {}; var clean = { sources: ['openstreetmap', 'openaddresses'], layers: ['venue'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); @@ -99,12 +99,19 @@ test('valid combination', function(t) { var raw = {}; var clean = { sources: ['openaddresses'], layers: ['address', 'country'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 0, 'should return no errors'); t.equal(messages.warnings.length, 0, 'should return no warnings'); t.end(); }); + + test('return an array of valid parameters in object form for Joi schema validation', function (t) { + const expected = [{ 'name': 'sources' }, { 'name': 'layers' }]; + const validParameters = sanitizer.expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); }; module.exports.tests.invalid_combination = function(test, common) { @@ -112,7 +119,7 @@ module.exports.tests.invalid_combination = function(test, common) { var raw = {}; var clean = { sources: ['whosonfirst'], layers: ['address'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 1, 'should return an error'); t.equal(messages.errors[0], 'You have specified both the `sources` and `layers` ' + @@ -125,7 +132,7 @@ module.exports.tests.invalid_combination = function(test, common) { var raw = {}; var clean = { sources: ['openstreetmap'], layers: ['country', 'locality'] }; - var messages = sanitize(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.equal(messages.errors.length, 2, 'should return an error'); t.equal(messages.errors[0], 'You have specified both the `sources` and `layers` ' + diff --git a/test/unit/sanitizer/_synthesize_analysis.js b/test/unit/sanitizer/_synthesize_analysis.js index 82352c8d..fd13b947 100644 --- a/test/unit/sanitizer/_synthesize_analysis.js +++ b/test/unit/sanitizer/_synthesize_analysis.js @@ -37,7 +37,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -76,7 +76,7 @@ module.exports.tests.text_parser = function(test, common) { parsed_text: {} }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + @@ -99,7 +99,7 @@ module.exports.tests.text_parser = function(test, common) { const expected_clean = { parsed_text: {} }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, ['at least one of the following fields is required: ' + @@ -128,7 +128,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -161,7 +161,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -194,7 +194,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -224,7 +224,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - const messages = sanitizer(raw, clean); + const messages = sanitizer().sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, [], 'no errors'); @@ -233,6 +233,23 @@ module.exports.tests.text_parser = function(test, common) { }); + test('return an array of valid parameters in object form for Joi schema validation', function (t) { + const sanitizer = require('../../../sanitizer/_synthesize_analysis'); + const expected = [ + { 'name': 'venue' }, + { 'name': 'address' }, + { 'name': 'neighbourhood' }, + { 'name': 'borough' }, + { 'name': 'locality' }, + { 'name': 'county' }, + { 'name': 'region' }, + { 'name': 'postalcode' }, + { 'name': 'country' }]; + + const validParameters = sanitizer().expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); }; module.exports.all = function (tape, common) { diff --git a/test/unit/sanitizer/_text.js b/test/unit/sanitizer/_text.js index 78b9a676..2692967d 100644 --- a/test/unit/sanitizer/_text.js +++ b/test/unit/sanitizer/_text.js @@ -1,4 +1,4 @@ -const sanitizer = require('../../../sanitizer/_text'); +const sanitizer = require('../../../sanitizer/_text')(); module.exports.tests = {}; @@ -15,7 +15,7 @@ module.exports.tests.text_parser = function(test, common) { text: raw.text }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages, { warnings: [], errors: [] }, 'no errors/warnings'); @@ -34,7 +34,7 @@ module.exports.tests.text_parser = function(test, common) { const expected_clean = { }; - const messages = sanitizer(raw, clean); + const messages = sanitizer.sanitize(raw, clean); t.deepEquals(clean, expected_clean); t.deepEquals(messages.errors, ['invalid param \'text\': text length, must be >0'], 'no errors'); @@ -46,6 +46,13 @@ module.exports.tests.text_parser = function(test, common) { }); + test('return an array of expected parameters in object form for validation', (t) => { + const expected = [{ name: 'text' }]; + const validParameters = sanitizer.expected(); + t.deepEquals(validParameters, expected); + t.end(); + }); + }; module.exports.all = (tape, common) => { diff --git a/test/unit/sanitizer/_text_addressit.js b/test/unit/sanitizer/_text_addressit.js index 26989c18..5d8f0d47 100644 --- a/test/unit/sanitizer/_text_addressit.js +++ b/test/unit/sanitizer/_text_addressit.js @@ -1,4 +1,4 @@ -var sanitizer = require('../../../sanitizer/_text_addressit'); +var sanitizer = require('../../../sanitizer/_text_addressit')(); var type_mapping = require('../../../helper/type_mapping'); module.exports.tests = {}; @@ -11,7 +11,7 @@ module.exports.tests.text_parser = function(test, common) { var clean = { }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEquals(messages.errors, [], 'no errors'); t.deepEquals(messages.warnings, [], 'no warnings'); @@ -42,7 +42,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -67,7 +67,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -98,7 +98,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -122,7 +122,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -144,7 +144,7 @@ module.exports.tests.text_parser = function(test, common) { text: 'yugolsavia' }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -164,7 +164,7 @@ module.exports.tests.text_parser = function(test, common) { text: 'small town' }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -184,7 +184,7 @@ module.exports.tests.text_parser = function(test, common) { text: '123 main' }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -204,7 +204,7 @@ module.exports.tests.text_parser = function(test, common) { text: 'main 123' }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -228,7 +228,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -253,7 +253,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -279,7 +279,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -305,7 +305,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); @@ -331,7 +331,7 @@ module.exports.tests.text_parser = function(test, common) { } }; - var messages = sanitizer(raw, clean); + var messages = sanitizer.sanitize(raw, clean); t.deepEqual(messages, { errors: [], warnings: [] } ); t.deepEqual(clean, expected_clean); diff --git a/test/unit/sanitizer/_tokenizer.js b/test/unit/sanitizer/_tokenizer.js index c5208ffe..e5becebb 100644 --- a/test/unit/sanitizer/_tokenizer.js +++ b/test/unit/sanitizer/_tokenizer.js @@ -1,4 +1,4 @@ -var sanitizer = require('../../../sanitizer/_tokenizer'); +var sanitizer = require('../../../sanitizer/_tokenizer')(); module.exports.tests = {}; @@ -6,7 +6,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('clean.text not set', function(t) { var clean = {}; // clean.text not set - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // no tokens produced t.deepEquals(clean.tokens, [], 'no tokens'); @@ -22,7 +22,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('clean.text not a string', function(t) { var clean = { text: {} }; // clean.text not a string - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // no tokens produced t.deepEquals(clean.tokens, [], 'no tokens'); @@ -38,7 +38,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('empty string', function(t) { var clean = { text: '' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // no tokens produced t.deepEquals(clean.tokens, [], 'no tokens'); @@ -54,7 +54,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('clean.parsed_text set but clean.parsed_text.name invalid', function(t) { var clean = { parsed_text: { text: {} } }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // no tokens produced t.deepEquals(clean.tokens, [], 'no tokens'); @@ -70,7 +70,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('favor clean.parsed_text.name over clean.text', function(t) { var clean = { parsed_text: { name: 'foo' }, text: 'bar' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // favor clean.parsed_text.name over clean.text t.deepEquals(clean.tokens, [ 'foo' ], 'use clean.parsed_text.name'); @@ -86,7 +86,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('favor clean.parsed_text street data over clean.text', function(t) { var clean = { parsed_text: { number: '190', street: 'foo st' }, text: 'bar' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // favor clean.parsed_text.name over clean.text t.deepEquals(clean.tokens, [ '190', 'foo', 'st' ], 'use street name + number'); @@ -102,7 +102,7 @@ module.exports.tests.sanity_checks = function(test, common) { test('favor clean.parsed_text.name over clean.parsed_text street data', function(t) { var clean = { parsed_text: { number: '190', street: 'foo st', name: 'foo' }, text: 'bar' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // favor clean.parsed_text.name over all other variables t.deepEquals(clean.tokens, [ 'foo' ], 'use clean.parsed_text.name'); @@ -121,7 +121,7 @@ module.exports.tests.space_delimiter = function(test, common) { test('space delimiter - simple', function(t) { var clean = { text: '30 west 26th street new york' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -156,7 +156,7 @@ module.exports.tests.space_delimiter = function(test, common) { test('space delimiter - multiple spaces / other whitespace', function(t) { var clean = { text: ' 30 west \t26th \nstreet new york ' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -194,7 +194,7 @@ module.exports.tests.comma_delimiter = function(test, common) { test('comma delimiter - simple', function(t) { var clean = { text: '30 west 26th street, new york' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -229,7 +229,7 @@ module.exports.tests.comma_delimiter = function(test, common) { test('comma delimiter - multiple commas', function(t) { var clean = { text: ',30 west 26th street,,, new york,' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -267,7 +267,7 @@ module.exports.tests.forward_slash_delimiter = function(test, common) { test('forward slash delimiter - simple', function(t) { var clean = { text: 'Bedell Street/133rd Avenue' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -298,7 +298,7 @@ module.exports.tests.forward_slash_delimiter = function(test, common) { test('forward slash - multiple slashes', function(t) { var clean = { text: '/Bedell Street//133rd Avenue/' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -332,7 +332,7 @@ module.exports.tests.final_token_single_gram = function(test, common) { test('final token single gram - numeric', function(t) { var clean = { text: 'grolmanstrasse 1' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -359,7 +359,7 @@ module.exports.tests.final_token_single_gram = function(test, common) { test('final token single gram - non-numeric', function(t) { var clean = { text: 'grolmanstrasse a' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -389,7 +389,7 @@ module.exports.tests.back_slash_delimiter = function(test, common) { test('back slash delimiter - simple', function(t) { var clean = { text: 'Bedell Street\\133rd Avenue' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -408,7 +408,7 @@ module.exports.tests.back_slash_delimiter = function(test, common) { test('back slash - multiple slashes', function(t) { var clean = { text: '\\Bedell Street\\\\133rd Avenue\\' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [ @@ -430,7 +430,7 @@ module.exports.tests.mixed_delimiter = function(test, common) { test('mixed delimiters', function(t) { var clean = { text: ',/Bedell Street\\, \n\t ,\\//133rd Avenue, /\n/' }; - var messages = sanitizer({}, clean); + var messages = sanitizer.sanitize({}, clean); // tokens produced t.deepEquals(clean.tokens, [