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

schema: add validation for affiliations #286

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions invenio_vocabularies/contrib/names/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def validate_names(self, data, **kwargs):
]
raise ValidationError({"family_name": messages})

@validates_schema
def validate_affiliatons(self, data, **kwargs):
"""Validate names."""
affiliations = data.get("affiliations", [])
seen_names = set()
for affiliation in affiliations:
name = affiliation.get("name")
if not affiliation.get("id") and name:
if name in seen_names:
messages = [_("Duplicated affiliations.")]
raise ValidationError({"affiliations": messages})
else:
seen_names.add(name)

@post_load
def update_name(self, data, **kwargs):
"""Update names for person.
Expand Down
16 changes: 16 additions & 0 deletions tests/contrib/names/test_names_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ def test_invalid_no_names(app):

with pytest.raises(ValidationError):
data = NameSchema().load(invalid)


def test_duplicated_affiliations(app):
invalid = {
"family_name": "Doe",
"given_name": "John",
"name": "Doe, John",
"id": "0000-0001-8135-3489",
"identifiers": [{"identifier": "0000-0001-8135-3489", "scheme": "orcid"}],
"affiliations": [{"name": "CustomORG"}, {"name": "CustomORG"}],
}
with pytest.raises(ValidationError) as e:
NameSchema().load(invalid)

messages = e.value.normalized_messages()
assert {"affiliations": ["Duplicated affiliations."]} == messages