Skip to content

Commit

Permalink
Add ErrorCollector to iso3_codes validator
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-almeida committed Sep 16, 2024
1 parent b73f29c commit e2705f5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions nomenclature/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
ValidationInfo,
)

from nomenclature.error import ErrorCollector

from pyam.utils import to_list

from .countries import countries
Expand Down Expand Up @@ -259,15 +261,20 @@ def check_countries(cls, v: List[str], info: ValidationInfo) -> List[str]:
@field_validator("iso3_codes")
def check_iso3_codes(cls, v: List[str], info: ValidationInfo) -> List[str]:
"""Verifies that each ISO3 code is valid according to pycountry library."""
errors = ErrorCollector()
if invalid_iso3_codes := [
iso3_code
for iso3_code in to_list(v)
if countries.get(alpha_3=iso3_code) is None
]:
raise ValueError(
f"Region '{info.data['name']}' has invalid ISO3 country code(s): "
+ ", ".join(invalid_iso3_codes)
errors.append(
ValueError(
f"Region '{info.data['name']}' has invalid ISO3 country code(s): "
+ ", ".join(invalid_iso3_codes)
)
)
if errors:
raise ValueError(errors)
return v


Expand Down

0 comments on commit e2705f5

Please sign in to comment.