Repository: IAMconsortium/nomenclature

Issue:
ErrorCollection for countries-validation
The validation of countries-elements in the RegionCode class raises a ValueError directly. It would be better to use the error-collection utility (rather than only raising the first problem in a region-yaml-file.

To be specific: this error https://github.com/IAMconsortium/nomenclature/blob/65f005c40787548f59a4e0d068c38fda424cad0c/nomenclature/code.py#L267

should be raised as part of an error-collection like

https://github.com/IAMconsortium/nomenclature/blob/65f005c40787548f59a4e0d068c38fda424cad0c/nomenclature/codelist.py#L219

Discussion:

Patch:
diff --git a/nomenclature/code.py b/nomenclature/code.py
index 097b644..0d7e536 100644
--- a/nomenclature/code.py
+++ b/nomenclature/code.py
@@ -12,6 +12,8 @@ from pydantic import (
     ValidationInfo,
 )
 
+from nomenclature.error import ErrorCollector
+
 from pyam.utils import to_list
 
 from .countries import countries
@@ -259,15 +261,20 @@ class RegionCode(Code):
     @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
 
 


Test patch:
diff --git a/tests/test_code.py b/tests/test_code.py
index c228c5e..25380b7 100644
--- a/tests/test_code.py
+++ b/tests/test_code.py
@@ -157,7 +157,8 @@ def test_RegionCode_iso3_code_list_fail():
     error_pattern = (
         "1 validation error for RegionCode\n"
         "iso3_codes\n"
-        "  Value error, Region 'Western Europe' has "
+        "  Value error, Collected 1 error:\n"
+        "  1. Region 'Western Europe' has "
         "invalid ISO3 country code\(s\): DMK, IPL, ATZ, FNL, FRE, DEX, GRE, "  # noqa
         "IBL, ITL, LIC, MLA, BEG, FRT, ANB, GDR, LXB, MNO, NTD, NRW, PRE, EPA, "  # noqa
         "SWD, CEW, GTR, SOR"  # noqa
@@ -170,7 +171,8 @@ def test_RegionCode_iso3_code_str_fail():
     error_pattern = (
         "1 validation error for RegionCode\n"
         "iso3_codes\n"
-        "  Value error, Region 'Austria' has invalid ISO3 country code\(s\): AUTT"
+        "  Value error, Collected 1 error:\n"
+        "  1. Region 'Austria' has invalid ISO3 country code\(s\): AUTT"
     )
     with raises(ValueError, match=error_pattern):
         RegionCode(name="Austria", hierarchy="country", iso3_codes="AUTT")