mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
9 years ago
|
var check = require('check-types');
|
||
9 years ago
|
var iso3166 = require('iso3166-1');
|
||
|
|
||
|
function sanitize(raw, clean) {
|
||
|
// error & warning messages
|
||
|
var messages = { errors: [], warnings: [] };
|
||
|
|
||
9 years ago
|
// target input param
|
||
|
var country = raw['boundary.country'];
|
||
9 years ago
|
|
||
9 years ago
|
// param 'boundary.country' is optional and should not
|
||
|
// error when simply not set by the user
|
||
|
if (check.assigned(country)){
|
||
9 years ago
|
|
||
9 years ago
|
// must be valid string
|
||
9 years ago
|
if (!check.nonEmptyString(country)) {
|
||
9 years ago
|
messages.errors.push('boundary.country is not a string');
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// must be a valid ISO 3166 code
|
||
9 years ago
|
else if (!containsIsoCode(country.toUpperCase())) {
|
||
9 years ago
|
messages.errors.push(country + ' is not a valid ISO2/ISO3 country code');
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
|
// valid ISO 3166 country code, set alpha3 code on 'clean.boundary.country'
|
||
9 years ago
|
else {
|
||
9 years ago
|
// the only way for boundary.country to be assigned is if input is
|
||
|
// a string and a known ISO2 or ISO3
|
||
9 years ago
|
clean['boundary.country'] = iso3166.to3(country.toUpperCase());
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return messages;
|
||
|
}
|
||
|
|
||
|
function containsIsoCode(isoCode) {
|
||
9 years ago
|
return iso3166.list().some(function(row) {
|
||
9 years ago
|
return row.alpha2 === isoCode || row.alpha3 === isoCode;
|
||
9 years ago
|
});
|
||
9 years ago
|
}
|
||
|
|
||
|
module.exports = sanitize;
|