diff --git a/invenio_vocabularies/contrib/names/schema.py b/invenio_vocabularies/contrib/names/schema.py index 380bd7fd..287d4f86 100644 --- a/invenio_vocabularies/contrib/names/schema.py +++ b/invenio_vocabularies/contrib/names/schema.py @@ -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. diff --git a/tests/contrib/names/test_names_schema.py b/tests/contrib/names/test_names_schema.py index fea19d62..dd1bb37c 100644 --- a/tests/contrib/names/test_names_schema.py +++ b/tests/contrib/names/test_names_schema.py @@ -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