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.
40 lines
1.0 KiB
40 lines
1.0 KiB
var isObject = require('is-object'); |
|
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 (raw.boundary && raw.boundary.country) { |
|
var country = raw.boundary.country |
|
|
|
if (typeof country !== 'string') { |
|
messages.warnings.push('boundary.country is not a string'); |
|
clean.boundary.country = undefined; |
|
} |
|
else if (!containsIsoCode(country.toUpperCase())) { |
|
messages.warnings.push(country + ' is not a valid ISO2/ISO3 country code'); |
|
clean.boundary.country = undefined; |
|
} |
|
else { |
|
clean.boundary.country = iso3166.to2(country.toUpperCase()); |
|
} |
|
|
|
} else { |
|
clean.boundary.country = undefined; |
|
} |
|
|
|
return messages; |
|
|
|
} |
|
|
|
function containsIsoCode(isoCode) { |
|
return iso3166.list().filter(function(row) { |
|
return row.alpha2 === isoCode || row.alpha3 === isoCode; |
|
}).length > 0; |
|
} |
|
|
|
module.exports = sanitize;
|
|
|