Skip to content

Commit

Permalink
schema: add validation for affiliations
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Dec 6, 2023
1 parent 41b435d commit 1352bc1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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

0 comments on commit 1352bc1

Please sign in to comment.