Skip to content

Commit

Permalink
Merge pull request #235 from candleindark/contact-person-email
Browse files Browse the repository at this point in the history
Add validator to ensure a contact person has email provided
  • Loading branch information
yarikoptic authored Apr 17, 2024
2 parents 75ed4da + 146b6e2 commit dc15401
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dandischema/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from datetime import date, datetime
from enum import Enum
import os
Expand Down Expand Up @@ -845,6 +847,16 @@ class Contributor(DandiBaseModel):
"Contributor", validate_default=True, json_schema_extra={"readOnly": True}
)

@model_validator(mode="after")
def ensure_contact_person_has_email(self) -> Contributor:
role_names = self.roleName

if role_names is not None and RoleType.ContactPerson in role_names:
if self.email is None:
raise ValueError("Contact person must have an email address.")

return self


class Organization(Contributor):
identifier: Optional[RORID] = Field(
Expand Down
1 change: 1 addition & 0 deletions dandischema/tests/test_datacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def metadata_basic() -> Dict[str, Any]:
"contributor": [
{
"name": "A_last, A_first",
"email": "[email protected]",
"roleName": [RoleType("dcite:ContactPerson")],
}
],
Expand Down
1 change: 1 addition & 0 deletions dandischema/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_mismatch_key(schema_version: str, schema_key: str) -> None:
{
"schemaKey": "Person",
"name": "Last, first",
"email": "[email protected]",
"roleName": ["dcite:ContactPerson"],
}
],
Expand Down
43 changes: 43 additions & 0 deletions dandischema/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Asset,
BaseType,
CommonModel,
Contributor,
DandiBaseModel,
Dandiset,
DigestType,
Expand Down Expand Up @@ -378,6 +379,7 @@ def test_dantimeta_1() -> None:
"contributor": [
{
"name": "last name, first name",
"email": "[email protected]",
"roleName": [RoleType("dcite:ContactPerson")],
}
],
Expand Down Expand Up @@ -592,6 +594,7 @@ def test_schemakey_roundtrip() -> None:
},
{
"name": "last2, first2",
"email": "[email protected]",
"roleName": ["dcite:ContactPerson"],
"schemaKey": "Person",
"affiliation": [],
Expand Down Expand Up @@ -689,3 +692,43 @@ def test_embargoedaccess() -> None:
)
]
)


_NON_CONTACT_PERSON_ROLES_ARGS: List[List[RoleType]] = [
[],
[RoleType.Author, RoleType.DataCurator],
[RoleType.Funder],
]

_CONTACT_PERSON_ROLES_ARGS: List[List[RoleType]] = [
role_lst + [RoleType.ContactPerson] for role_lst in _NON_CONTACT_PERSON_ROLES_ARGS
]


class TestContributor:

@pytest.mark.parametrize("roles", _CONTACT_PERSON_ROLES_ARGS)
def test_contact_person_without_email(self, roles: List[RoleType]) -> None:
"""
Test creating a `Contributor` instance as a contact person without an email
"""
with pytest.raises(
pydantic.ValidationError, match="Contact person must have an email address"
):
Contributor(roleName=roles)

@pytest.mark.parametrize("roles", _NON_CONTACT_PERSON_ROLES_ARGS)
def test_non_contact_person_without_email(self, roles: List[RoleType]) -> None:
"""
Test creating a `Contributor` instance as a non-contact person without an email
"""
Contributor(roleName=roles)

@pytest.mark.parametrize(
"roles", _NON_CONTACT_PERSON_ROLES_ARGS + _CONTACT_PERSON_ROLES_ARGS
)
def test_with_email(self, roles: List[RoleType]) -> None:
"""
Test creating a `Contributor` instance with an email
"""
Contributor(email="[email protected]", roleName=roles)

0 comments on commit dc15401

Please sign in to comment.