Skip to content

Country code

Country codes.

validate_country_code(country_code)

Validate that a country code is valid.

Source code in gso/utils/types/country_code.py
def validate_country_code(country_code: str) -> str:
    """Validate that a country code is valid."""
    # Check for the UK code before attempting to look it up since it's known as "GB" in the pycountry database.
    if country_code != "UK":
        try:
            pycountry.countries.lookup(country_code)
        except LookupError as e:
            msg = "Invalid or non-existent country code, it must be in ISO 3166-1 alpha-2 format."
            raise ValueError(msg) from e
    return country_code