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.

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 issue

Assume 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:

  1. What use case is this behavior supposed to support? In what situation would I want an error to be raised if required=False but missing 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.

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

github_iconTop GitHub Comments

1reaction
lafrechcommented, Feb 26, 2022

missing (now deprecated, replaced with load_default) is a default value to use when the payload doesn’t contain the field.

If required is False (which is the case by default), the field may be missing. But None is not accepted. It is not the same as a missing value. To allow None, you may set allow_none=True.

Also, setting load_default=None implies allow_none=True.

https://marshmallow.readthedocs.io/en/stable/marshmallow.fields.html#marshmallow.fields.Field

0reactions
mkmoisencommented, Mar 1, 2022

@lafrech Thank you.

Read more comments on GitHub >

github_iconTop 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 >

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