From e5a389ecaf8fd428ce7c89a558178a170c7bb662 Mon Sep 17 00:00:00 2001 From: tekoiv Date: Wed, 28 Jun 2023 09:14:54 +0300 Subject: [PATCH] Allow filtering by country in api calls --- sanitizer/_boundary_country.js | 35 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/sanitizer/_boundary_country.js b/sanitizer/_boundary_country.js index 81a5e7d5..0a8b57c7 100644 --- a/sanitizer/_boundary_country.js +++ b/sanitizer/_boundary_country.js @@ -6,28 +6,29 @@ function _sanitize(raw, clean) { var messages = { errors: [], warnings: [] }; // target input param - var country = raw['boundary.country']; - - // param 'boundary.country' is optional and should not - // error when simply not set by the user - if (check.assigned(country)){ + var countries = raw['boundary.country']; + // If explicitly given, parse country codes. If not, set to FIN + if (check.assigned(countries)){ // must be valid string - if (!check.nonEmptyString(country)) { + if (!check.nonEmptyString(countries)) { messages.errors.push('boundary.country is not a string'); + return messages } - // must be a valid ISO 3166 code - else if (!containsIsoCode(country)) { - messages.errors.push(country + ' is not a valid ISO2/ISO3 country code'); - } - - // valid ISO 3166 country code, set alpha3 code on '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.iso3Code(country); - } + // every item must be a valid ISO 3166 code + var countriesArray = countries.split(",") + countriesArray.forEach(country => { + if (!containsIsoCode(country)) { + messages.errors.push(country + ' is not a valid ISO2/ISO3 country code'); + return messages; + } + }) + + // The codes are valid, set boundary.country to array of country codes + clean['boundary.country'] = countriesArray.map(country => iso3166.iso3Code(country)) + } else { // default behaviour (set FIN) + clean['boundary.country'] = ["FIN"] } return messages;