Why is it necessary to add missing=True when Required=False? Why is the error message "Field may not be null" instead of "Field may not be missing" ?
See original GitHub issueAssume the following schema:
from marshmallow import Schema, fields
class FooSchema(Schema):
a = fields.Int(required=True)
b = fields.Int(required=False) # Forgot to include missing=None
This will not raise an error when the payload is missing the b
field:
# No error, makes sense:
FooSchema().load({'a': 1})
However, it will raise an error when the payload contains a b
field set to None
:
# Raises ValidationError {'b': ['Field may not be null.']}
FooSchema().load({'a': 1, 'b': None})
# Why? What use case is this supposed to handle?
# Regardless, why is the error "Field may not be null" instead of "Field may not be missing"?
The solution to this problem if to add missing=None
:
class FooSchema(Schema):
a = fields.Int(required=True)
b = fields.Int(required=False, missing=None)
# No error
FooSchema().load({'a': 1})
# No error
FooSchema().load({'a': 1, 'b': None})
–
I have two questions about this behavior:
- What use case is this behavior supposed to support? In what situation would I want an error to be raised if
required=False
butmissing
is not set to None?
I agree that if someone has this use case, the reasoning for this implementation is sensible in that this is the only non ambiguous way to support it.
That is understandable. But what I’m a bit confused about is the error message: Field may not be null.
- Wouldn’t
Field may not be missing
make more sense? The field absolutely may be null- so long as the field is included in the payload.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to have an “optional” field but if present required ... - GitHub
Meaning a field may be missing but if it is present it should not be None. from pydantic import BaseModel class Foo(BaseModel): count: ......
Read more >Required fields cannot be set to null - BMC Community
I have created a regular form with some required fields. Whenever user is leaving these fields blank while submitting records, an error message...
Read more >{'required': 'This field is required.', 'null': 'This field may not be ...
This is the error which is thrown by the invoice.is_valid() . I don't know why invoice is not valid. If anyone can help...
Read more >Using nullability in GraphQL
A field can either be nullable or non-null, and this tells you whether or not you could receive a null value when you...
Read more >Missing Values - Julia Documentation
Passing a missing value to a function which does not have a method accepting arguments of type Missing throws a MethodError , just...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
missing
(now deprecated, replaced withload_default
) is a default value to use when the payload doesn’t contain the field.If
required
isFalse
(which is the case by default), the field may be missing. ButNone
is not accepted. It is not the same as a missing value. To allowNone
, you may setallow_none=True
.Also, setting
load_default=None
impliesallow_none=True
.https://marshmallow.readthedocs.io/en/stable/marshmallow.fields.html#marshmallow.fields.Field
@lafrech Thank you.