We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
unknown
main code:
from dataclasses import dataclass, field from typing import List, Optional import marshmallow import marshmallow_dataclass @dataclass class Building: # field metadata is used to instantiate the marshmallow field height: float = field(metadata={"validate": marshmallow.validate.Range(min=0)}) name: str = field(default="anonymous") @dataclass class City: name: Optional[str] buildings: List[Building] = field(default_factory=list) CitySchema = marshmallow_dataclass.class_schema(City) city = CitySchema().load( {"name": "Paris", "aaa": "bbb", "buildings": [{"name": "Eiffel Tower", "height": 324, "ccc": "ddd"}]}, unknown=marshmallow.EXCLUDE ) print(city)
If I use {"name": "Paris", "aaa": "bbb", "buildings": [{"name": "Eiffel Tower", "height": 324}]}, unknown works well.
{"name": "Paris", "aaa": "bbb", "buildings": [{"name": "Eiffel Tower", "height": 324}]}
errer:
Traceback (most recent call last): File "clos/Untitled-2.py", line 25, in <module> unknown=marshmallow.EXCLUDE File "/home/hudingyuan/.local/lib/python3.6/site-packages/marshmallow/schema.py", line 714, in load data, many=many, partial=partial, unknown=unknown, postprocess=True File "/home/hudingyuan/.local/lib/python3.6/site-packages/marshmallow/schema.py", line 892, in _do_load raise exc marshmallow.exceptions.ValidationError: {'buildings': {0: {'ccc': ['Unknown field.']}}}
The text was updated successfully, but these errors were encountered:
@sloria Do you know why that it ?
Sorry, something went wrong.
It seems there is a WIP feature to overcome this issue: marshmallow-code/marshmallow#1634.
Currently i found solution overriding base schema during loading:
from dataclasses import dataclass import marshmallow from marshmallow_dataclass import class_schema @dataclass class A: name: str @dataclass class B: nested: A surname: str class BaseSchema(marshmallow.Schema): class Meta: unknown = marshmallow.EXCLUDE B_schema = class_schema(B, base_schema=BaseSchema) loaded = B_schema().load( { "surname": "Smith", "top_level_unknown": "value", "nested": { "name": "John", "low_level_unknown": "value" } } ) print(loaded) # B(nested=A(name='John'), surname='Smith')
No branches or pull requests
main code:
If I use
{"name": "Paris", "aaa": "bbb", "buildings": [{"name": "Eiffel Tower", "height": 324}]}
, unknown works well.errer:
The text was updated successfully, but these errors were encountered: