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

identifiers: relax deduplication rules on Identifiers #92

Merged
merged 2 commits into from
Nov 11, 2024
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
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
Changes
=======

Version 0.10.0 (released 2024-11-11)

- add IdentifierValueSet to require scheme and value to be unique

Version v0.9.3 (released 2024-11-05)

- setup: upgrade babel-edtf to 1.2.0 and edtf to 5.0.0 and fix bug with dates and times with hour 23
Expand Down
2 changes: 1 addition & 1 deletion marshmallow_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
`arrow <https://pypi.org/project/arrow/>`_ for date parsing.
"""

__version__ = "0.9.3"
__version__ = "0.10.0"

__all__ = ("__version__",)
3 changes: 2 additions & 1 deletion marshmallow_utils/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .contrib import Function, Method
from .edtfdatestring import EDTFDateString, EDTFDateTimeString
from .generated import GenFunction, GenMethod
from .identifier import IdentifierSet
from .identifier import IdentifierSet, IdentifierValueSet
from .isodate import ISODateString
from .isolanguage import ISOLangString
from .links import Link, Links
Expand All @@ -43,6 +43,7 @@
"GenFunction",
"GenMethod",
"IdentifierSet",
"IdentifierValueSet",
"ISODateString",
"ISOLangString",
"Link",
Expand Down
21 changes: 20 additions & 1 deletion marshmallow_utils/fields/identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,24 @@ class IdentifierSet(List):
def _validate(self, value):
"""Validates the list of identifiers."""
schemes = [identifier["scheme"] for identifier in value]
if not len(value) == len(set(schemes)):
if len(value) != len(set(schemes)):
raise self.make_error(key="multiple_values")


class IdentifierValueSet(List):
"""Identifier list with deduplication.

It assumes the items of the list contain a *scheme* property.
"""

default_error_messages = {
"multiple_values": "Duplicated identifier entry is not allowed.",
}

def _validate(self, value):
"""Validates the list of identifiers."""
identifiers = [
(identifier["scheme"], identifier["identifier"]) for identifier in value
]
if len(value) != len(set(identifiers)):
raise self.make_error(key="multiple_values")