Skip to content
New issue

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 does't take effect when nested #46

Open
qin-nz opened this issue Oct 21, 2019 · 2 comments
Open

unknown does't take effect when nested #46

qin-nz opened this issue Oct 21, 2019 · 2 comments

Comments

@qin-nz
Copy link

qin-nz commented Oct 21, 2019

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.

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.']}}}
@lovasoa
Copy link
Owner

lovasoa commented Oct 29, 2019

@sloria Do you know why that it ?

@SlonSky
Copy link

SlonSky commented Jul 23, 2020

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants