mirror of https://github.com/pelias/api.git
Julian Simioni
9 years ago
15 changed files with 275 additions and 12 deletions
@ -0,0 +1,42 @@
|
||||
var check = require('check-types'); |
||||
var iso3166 = require('iso3166-1'); |
||||
|
||||
function sanitize(raw, clean) { |
||||
// error & warning messages
|
||||
var messages = { errors: [], warnings: [] }; |
||||
|
||||
// init clean.boundary (if not already init)
|
||||
clean.boundary = clean.boundary || {}; |
||||
|
||||
if (check.assigned(raw['boundary.country'])) { |
||||
var country = raw['boundary.country']; |
||||
|
||||
if (!check.string(country)) { |
||||
messages.errors.push('boundary.country is not a string'); |
||||
delete clean.boundary.country; |
||||
} |
||||
else if (!containsIsoCode(country.toUpperCase())) { |
||||
messages.errors.push(country + ' is not a valid ISO2/ISO3 country code'); |
||||
delete clean.boundary.country; |
||||
} |
||||
else { |
||||
// the only way for boundary.country to be assigned is if input is
|
||||
// a string and a known ISO2 or ISO3
|
||||
clean.boundary.country = iso3166.to3(country.toUpperCase()); |
||||
} |
||||
|
||||
} else { |
||||
delete clean.boundary.country; |
||||
} |
||||
|
||||
return messages; |
||||
|
||||
} |
||||
|
||||
function containsIsoCode(isoCode) { |
||||
return iso3166.list().some(function(row) { |
||||
return row.alpha2 === isoCode || row.alpha3 === isoCode; |
||||
}); |
||||
} |
||||
|
||||
module.exports = sanitize; |
@ -0,0 +1,54 @@
|
||||
|
||||
module.exports = { |
||||
'query': { |
||||
'filtered': { |
||||
'query': { |
||||
'bool': { |
||||
'must': [ |
||||
{ |
||||
'match': { |
||||
'alpha3': { |
||||
'analyzer': 'standard', |
||||
'query': 'ABC' |
||||
} |
||||
} |
||||
} |
||||
] |
||||
} |
||||
}, |
||||
'filter': { |
||||
'bool': { |
||||
'must': [ |
||||
{ |
||||
'geo_distance': { |
||||
'distance': '500km', |
||||
'distance_type': 'plane', |
||||
'optimize_bbox': 'indexed', |
||||
'_cache': true, |
||||
'center_point': { |
||||
'lat': 29.49136, |
||||
'lon': -82.50622 |
||||
} |
||||
} |
||||
} |
||||
] |
||||
} |
||||
} |
||||
} |
||||
}, |
||||
'sort': [ |
||||
'_score', |
||||
{ |
||||
'_geo_distance': { |
||||
'center_point': { |
||||
'lat': 29.49136, |
||||
'lon': -82.50622 |
||||
}, |
||||
'order': 'asc', |
||||
'distance_type': 'plane' |
||||
} |
||||
} |
||||
], |
||||
'size': 1, |
||||
'track_scores': true |
||||
}; |
@ -0,0 +1,44 @@
|
||||
|
||||
module.exports = { |
||||
'query': { |
||||
'filtered': { |
||||
'query': { |
||||
'bool': { |
||||
'must': [ |
||||
{ |
||||
'match': { |
||||
'alpha3': { |
||||
'analyzer': 'standard', |
||||
'query': 'ABC' |
||||
} |
||||
} |
||||
}, |
||||
{ |
||||
'match': { |
||||
'name.default': { |
||||
'query': 'test', |
||||
'boost': 1, |
||||
'analyzer': 'peliasOneEdgeGram' |
||||
} |
||||
} |
||||
} |
||||
], |
||||
'should': [{ |
||||
'match': { |
||||
'phrase.default': { |
||||
'query': 'test', |
||||
'analyzer': 'peliasPhrase', |
||||
'type': 'phrase', |
||||
'boost': 1, |
||||
'slop': 2 |
||||
} |
||||
} |
||||
}] |
||||
} |
||||
} |
||||
} |
||||
}, |
||||
'sort': [ '_score' ], |
||||
'size': 10, |
||||
'track_scores': true |
||||
}; |
@ -0,0 +1,79 @@
|
||||
var sanitize = require('../../../sanitiser/_boundary_country'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
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); |
||||
t.equals(clean.boundary.country, undefined, 'should be undefined'); |
||||
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); |
||||
t.end(); |
||||
}); |
||||
|
||||
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); |
||||
t.equals(clean.boundary.country, undefined, 'should be undefined'); |
||||
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); |
||||
t.end(); |
||||
}); |
||||
|
||||
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); |
||||
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(); |
||||
}); |
||||
|
||||
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); |
||||
t.equals(clean.boundary.country, 'ATA', 'should be uppercased ISO3'); |
||||
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); |
||||
t.end(); |
||||
}); |
||||
|
||||
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); |
||||
t.equals(clean.boundary.country, 'ATA', 'should be uppercased ISO3'); |
||||
t.deepEquals(errorsAndWarnings, { errors: [], warnings: [] }, 'no warnings or errors'); |
||||
t.end(); |
||||
}); |
||||
|
||||
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); |
||||
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(); |
||||
}); |
||||
|
||||
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); |
||||
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(); |
||||
}); |
||||
|
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _boundary_country ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue