Skip to content

Commit

Permalink
schemas: checks for duplicated schemes in identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Jun 3, 2022
1 parent d3b0953 commit 36f1d0a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions invenio_rdm_records/services/schemas/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# it under the terms of the MIT License; see LICENSE file for more details.

"""RDM record schemas."""

from functools import partial
from urllib import parse

Expand All @@ -21,6 +20,7 @@
from invenio_vocabularies.contrib.subjects.schema import SubjectRelationSchema
from marshmallow import Schema, ValidationError, fields, post_load, validate, \
validates, validates_schema
from marshmallow.fields import List
from marshmallow_utils.fields import EDTFDateString, IdentifierSet, \
SanitizedHTML, SanitizedUnicode
from marshmallow_utils.schemas import GeometryObjectSchema, IdentifierSchema
Expand Down Expand Up @@ -340,7 +340,7 @@ class MetadataSchema(Schema):
dates = fields.List(fields.Nested(DateSchema))
languages = fields.List(fields.Nested(VocabularySchema))
# alternate identifiers
identifiers = IdentifierSet(
identifiers = List(
fields.Nested(partial(
IdentifierSchema, allowed_schemes=record_identifiers_schemes))
)
Expand All @@ -356,3 +356,15 @@ class MetadataSchema(Schema):
locations = fields.Nested(FeatureSchema)
funding = fields.List(fields.Nested(FundingSchema))
references = fields.List(fields.Nested(ReferenceSchema))

@validates('identifiers')
def validate_non_duplicated_schemes(self, data, **kwargs):
"""Checks for duplicated schemes in identifiers."""
schemes = [identifier["scheme"] for identifier in data]
if not len(data) == len(set(schemes)):
raise ValidationError(
{
'identifiers':
_('Only one identifier per scheme is allowed.')
}
)

0 comments on commit 36f1d0a

Please sign in to comment.