Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validator to ensure a contact person has email provided #235

Merged
merged 7 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "nemo@example.com",
"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": "nobody@example.com",
"roleName": ["dcite:ContactPerson"],
}
],
Expand Down
53 changes: 53 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": "someone@dandiarchive.org",
"roleName": [RoleType("dcite:ContactPerson")],
}
],
Expand Down Expand Up @@ -592,6 +594,7 @@ def test_schemakey_roundtrip() -> None:
},
{
"name": "last2, first2",
"email": "nospam@dandiarchive.org",
"roleName": ["dcite:ContactPerson"],
"schemaKey": "Person",
"affiliation": [],
Expand Down Expand Up @@ -689,3 +692,53 @@ def test_embargoedaccess() -> None:
)
]
)


def _non_contact_person_roles_args() -> List[List[RoleType]]:
"""
Return a list of role lists that each do not contain the role of a contact person
"""
return [[], [RoleType.Author, RoleType.DataCurator], [RoleType.Funder]]


def _contact_person_roles_args() -> List[List[RoleType]]:
"""
Return a list of role lists that each contains the role of a contact person
"""
return [
role_lst + [RoleType.ContactPerson]
for role_lst in _non_contact_person_roles_args()
]
candleindark marked this conversation as resolved.
Show resolved Hide resolved


class TestContributor:
satra marked this conversation as resolved.
Show resolved Hide resolved
@pytest.mark.parametrize("roles", _contact_person_roles_args())
def test_contact_person_with_email(self, roles: List[RoleType]) -> None:
"""
Test creating a `Contributor` instance as a contact person with an email
"""
Contributor(email="nemo@dandiarchive.org", roleName=roles)

@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_with_email(self, roles: List[RoleType]) -> None:
"""
Test creating a `Contributor` instance as a non-contact person with an email
"""
Contributor(email="nemo@dandiarchive.org", roleName=roles)
candleindark marked this conversation as resolved.
Show resolved Hide resolved

@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)
Loading