Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _validate_country_code(self):
country_code = self.country_code
try:
iso3166.countries_by_alpha2[country_code]
except KeyError:
raise ValueError("Invalid country code '{}'".format(country_code))
from iso3166 import (
countries_by_alpha2,
countries_by_alpha3,
Country as CountryType,
)
value = self._filter(value, Type(string_types + (CountryType,)))
if self._has_errors:
return None
if isinstance(value, CountryType):
return value
search_functions = {
2: countries_by_alpha2,
3: countries_by_alpha3,
}
try:
return search_functions[len(value)][value.upper()]
except KeyError:
return self._invalid_value(value, self.CODE_INVALID, exc_info=True)