Skip to content

Commit 9ca23cd

Browse files
committed
services: references: add MultipleEntityReferenceBaseSchema
1 parent 347fd44 commit 9ca23cd

File tree

1 file changed

+27
-0
lines changed
  • invenio_records_resources/services/references

1 file changed

+27
-0
lines changed

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)