Cannot add marshmallow metadata to list items
See original GitHub issueIssue
I can’t seem to find any way to apply validation to items of a list type object, and upon inspection of the field_for_schema
implementation I don’t think this is possible, as no metadata is forwarded to the schema creation for the list item type:
marshmallow_dataclass: init.py
239: def field_for_schema(
240: typ: type,
241: default=marshmallow.missing,
242: metadata: Mapping[str, Any] = None
243: ) -> marshmallow.fields.Field:
...
303: if origin in (list, List):
304: list_elements_type = typing_inspect.get_args(typ, True)[0]
305: return marshmallow.fields.List(
306: field_for_schema(list_elements_type), # <<< No metadata passed
307: **metadata
308: )
Current State
Let’s say I have a User
class and and want to maintain a list of email addresses for each user. Currently, the only way I’d be able to validate the IP addresses is with a callable that iterated over the list and manually invoked Marshmallow’s validators:
from typing import List
from marshmallow.validate import Email
from marshmallow_dataclass import dataclass
@dataclass
class User:
name: str
emails: List[str] = field(metadata={
'validate': lambda l: all(Email()(i) for i in l)
})
…which, admittedly, is pretty simple, but it’s more weakly representative of the validator’s intent, particularly for tools that post-process the schema (i.e. - the fact that it’s a list validator means that tools aren’t able to recognize that the validation actually applies to the list item).
Proposal
I’d like to add a mechanism for explicitly passing metadata to the list item schema. I figure using a key in the metadata
dict that doesn’t conflict with Marshmallow schema kwargs would work just fine for this purpose:
from typing import List
from marshmallow.validate import Email
from marshmallow_dataclass import dataclass
@dataclass
class User:
name: str
emails: List[str] = field(metadata={
'item_metadata': {
'validate': Email()
}
})
If this sounds like something of interest, I’ll gladly whip it up and put out a PR.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Sorry for the delay in getting back, here, but this works perfectly. Thanks for the update!
Version 6.1.0 was just released with support for NewType, so I am closing this. https://pypi.org/project/marshmallow-dataclass/6.1.0/