question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot add marshmallow metadata to list items

See original GitHub issue

Issue

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rusnydercommented, Oct 22, 2019

Sorry for the delay in getting back, here, but this works perfectly. Thanks for the update!

0reactions
lovasoacommented, Oct 19, 2019

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/

Read more comments on GitHub >

github_iconTop Results From Across the Web

proposal: changing fields.Field's `**metadata` to ... - GitHub
Sometimes, people that unfamiliar with marshmallow, want to add validation with typo. e.g. class S(Schema): # this is typo.
Read more >
Fields — marshmallow 3.19.0 documentation
A list field, composed with another Field class or instance. Mapping ([keys, values]). An abstract class for objects with key-value pairs.
Read more >
marshmallow NewType can not be turned into a dataclass ...
I've had a similar issue with a similar bit of code that is raising an error since trying to upgrade marshmallow_dataclasses.
Read more >
marshmallow - Read the Docs
Create a schema by defining a class with variables mapping attribute names to Field objects. from marshmallow import Schema, fields class UserSchema(Schema):.
Read more >
great_expectations.marshmallow__shade.fields
Allows you to replace nested data with one of the data's fields. List (cls_or_instance: typing.Union[Field, type], **kwargs).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found