Skip to content

Commit

Permalink
Add ErrorCollector and pydantic model_dump
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-almeida committed Nov 14, 2024
1 parent 61a71d9 commit 2ea110e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
17 changes: 10 additions & 7 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,16 @@ def check_illegal_characters(self, config: NomenclatureConfig) -> Dict[str, Code
if config.check_illegal_characters
else ["{", "}"]
)
errors = ErrorCollector()

def _check_string(value):
if isinstance(value, str):
if any(char in value for char in illegal):
raise ValueError(
f"Unexpected character in {self.name}: '{code.name}'."
" Check for illegal characters and/or if tags were spelled correctly."
errors.append(
ValueError(
f"Unexpected character in {self.name}: '{code.name}'."
" Check for illegal characters and/or if tags were spelled correctly."
)
)
elif isinstance(value, dict):
for k in value.keys():
Expand All @@ -361,10 +364,10 @@ def _check_string(value):

for code in self.mapping.values():
if not code.repository:
for attr in code.model_fields:
if attr != "file":
value = getattr(code, attr)
_check_string(value)
for value in code.model_dump(exclude="file").values():
_check_string(value)
if errors:
raise ValueError(errors)

def to_yaml(self, path=None):
"""Write mapping to yaml file or return as stream
Expand Down
Submodule common-definitions added at 3b75bb

0 comments on commit 2ea110e

Please sign in to comment.