Skip to content

Commit 34cad6d

Browse files
committed
services: references: add MultipleEntityReferenceBaseSchema
1 parent 347fd44 commit 34cad6d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

invenio_records_resources/services/references/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
"""Service-related things for entity references."""
1010

11-
from .schema import EntityReferenceBaseSchema
11+
from .schema import EntityReferenceBaseSchema, MultipleEntityReferenceBaseSchema
1212

13-
__all__ = ("EntityReferenceBaseSchema",)
13+
__all__ = (
14+
"EntityReferenceBaseSchema",
15+
"MultipleEntityReferenceBaseSchema",
16+
)

invenio_records_resources/services/references/schema.py

+27
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,30 @@ def create_from_dict(cls, allowed_types, special_fields=None):
4545
return cls.from_dict(
4646
{ref_type: field_types[ref_type] for ref_type in allowed_types}
4747
)
48+
49+
50+
class MultipleEntityReferenceBaseSchema(EntityReferenceBaseSchema):
51+
"""Base schema for entity references, allowing multiple keys.
52+
53+
Example of an allowed value: ``{"user": 1, "record": "abcd-1234"}``.
54+
Example of a disallowed value: ``{"user": 1}``.
55+
"""
56+
57+
@classmethod
58+
def create_from_dict(cls, allowed_types, special_fields=None):
59+
"""Create an entity reference schema based on the allowed ref types.
60+
61+
Per default, a ``fields.String()`` field is registered for each of
62+
the type names in the ``allowed_types`` list.
63+
The field type can be customized by providing an entry in the
64+
``special_fields`` dict, with the type name as key and the field type
65+
as value (e.g. ``{"user": fields.Integer()}``).
66+
"""
67+
field_types = special_fields or {}
68+
for ref_type in allowed_types:
69+
# each type would be a String field per default
70+
field_types.setdefault(ref_type, ma.fields.String())
71+
72+
return cls.from_dict(
73+
{ref_type: field_types[ref_type] for ref_type in allowed_types}
74+
)

0 commit comments

Comments
 (0)