Skip to content

Commit

Permalink
Merge pull request #101 from HSLdevcom/DT-5961
Browse files Browse the repository at this point in the history
Allow filtering by country in api calls
  • Loading branch information
vesameskanen authored Jun 28, 2023
2 parents b7b1f51 + e5a389e commit 2801306
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions sanitizer/_boundary_country.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2801306

Please sign in to comment.