diff --git a/src/pysdmx/model/__base.py b/src/pysdmx/model/__base.py index e2323d4..9d28fea 100644 --- a/src/pysdmx/model/__base.py +++ b/src/pysdmx/model/__base.py @@ -3,6 +3,8 @@ from msgspec import Struct +from pysdmx.errors import ClientError + class Annotation(Struct, frozen=True, omit_defaults=True): """Annotation class. @@ -27,6 +29,24 @@ class Annotation(Struct, frozen=True, omit_defaults=True): url: Optional[str] = None type: Optional[str] = None + def __post_init__(self) -> None: + """Additional validation checks for Annotation.""" + if ( + not self.id + and not self.title + and not self.text + and not self.url + and not self.type + ): + raise ClientError( + 422, + "Empty annotation", + ( + "All fields of the annotation have been left empty." + "Please set at least one." + ), + ) + def __str__(self) -> str: """Returns a human-friendly description.""" out = [] @@ -196,6 +216,15 @@ class MaintainableArtefact( structure_url: Optional[str] = None agency: Union[str, Agency] = "" + def __post_init__(self) -> None: + """Additional validation checks for maintainable artefacts.""" + if not self.agency: + raise ClientError( + 422, + "Missing agency", + "Maintainable artefacts must reference an agency.", + ) + class ItemScheme(MaintainableArtefact, frozen=True, omit_defaults=True): """ItemScheme class. diff --git a/tests/model/test_annotation.py b/tests/model/test_annotation.py index 3fa8f1d..094c6e7 100644 --- a/tests/model/test_annotation.py +++ b/tests/model/test_annotation.py @@ -1,5 +1,6 @@ import pytest +from pysdmx.errors import Error from pysdmx.model.__base import Annotation @@ -60,14 +61,6 @@ def test_not_equal(id): assert a1 != a2 -def test_tostr_empty(): - a = Annotation() - - s = str(a) - - assert s == "" - - def test_tostr_id(id): a = Annotation(id) @@ -82,3 +75,8 @@ def test_tostr_all(id, title, text, url, type): s = str(a) assert s == f"id={id}, title={title}, text={text}, url={url}, type={type}" + + +def test_empty_annotation_not_allowed(): + with pytest.raises(Error, match="empty"): + Annotation() diff --git a/tests/model/test_maintainable.py b/tests/model/test_maintainable.py new file mode 100644 index 0000000..1c7ff82 --- /dev/null +++ b/tests/model/test_maintainable.py @@ -0,0 +1,9 @@ +import pytest + +from pysdmx.errors import Error +from pysdmx.model.__base import MaintainableArtefact + + +def test_empty_agency_not_allowed(): + with pytest.raises(Error, match="must reference an agency"): + MaintainableArtefact("ID")