marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

`Field may not be null` when data_key parameter provided in Schema

Open rttomlinson opened this issue 7 years ago • 2 comments

It seems that if you provider the data_key parameter in your Schema, the field become required as a side-effect. Even if required=False and allow_none=True are added you will still get the Field may not be null error. Ideally I would like this field to still be optional even if the key is not available in the python dict that is passed to the schema.

Example: (Parts omitted in interest of brevity)

class AuthRepsSchema(Schema):
...
    general_release_signer_info = fields.Mapping(data_key="generalReleaseSignerInfo")
...

auth_reps_V1 = self.auth_reps_v1_schema.load({"hello": "goodbye")

Will result in the marshmallow.exceptions.ValidationError: {'generalReleaseSignerInfo': ['Field may not be null.']} error.

rttomlinson avatar Apr 06 '19 22:04 rttomlinson

I can't reproduce.

>>> import marshmallow as ma
>>> class AuthRepsSchema(ma.Schema):
...   general_release_signer_info = ma.fields.Mapping(data_key="generalReleaseSignerInfo")
... 
>>> s = AuthRepsSchema()
>>> s.load({"hello": "goodbye"})
marshmallow.exceptions.ValidationError: {'hello': ['Unknown field.']}
>>> s.load({})
{}

Can you please provide a minimal, complete and verifiable example?

lafrech avatar Apr 07 '19 13:04 lafrech

I'm unable to reproduce as well now. I'll close this issue for now and reopen if I run into it again. Thanks for looking at it. I think I must be providing None somewhere in my code as the value for generalReleaseSignerInfo even though that is not my intention.

rttomlinson avatar Apr 07 '19 18:04 rttomlinson