Pass schema context to `missing` callback.
See original GitHub issueI sometimes need to fetch a default deserialized value from the outside of the schema. Method
and Function
callbacks are not called if the field is missing in JSON, would be nice to have something like:
class TestSchema(Schema):
value = fields.Function(missing=itemgetter('default_value'))
So that TestSchema(context={'default_value': 123}).load({}) would return
value = 123` using the context.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Subscriptions in Apollo Server
We can pass this schema object to both the subscription server and ApolloServer . This way, we make sure that the same schema...
Read more >Set up Lambda proxy integrations in API Gateway
Learn how to configure a Lambda proxy integration request and integration response in API Gateway.
Read more >Schema
In both cases, the callback function is called with the current value as the first argument and a context object as the second....
Read more >Schema | REST API Handbook
A schema is metadata that tells us how our data is structured. Most databases implement some form of schema which enables us to...
Read more >Package API — xmlschema 2.1.1 documentation
defuse – optional argument to pass for construct schema and XMLResource instances ... use_defaults – whether to use default values for filling missing...
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
I was hoping to use the
context
dictionary to drive field logic, but I was disappointed to find that when using themissing
parameter as a callable function, there is no avenue for accessing thecontext
data.I’d like to propose feeding the
context
dictionary as a parameter to themissing
callable (unfortunately this would be a breaking change).The rationale:
context
(if I am not mistaken) is to provide information to drive logic within a Field/Schema’s methods.missing
callable is to support an asynchronous/callback mechanism to allow the missing value to be determined/defined after the field has been instantiated (i.e. during load/parse time).context
when calling themissing
callable.Below is a somewhat simplified example of what I would like to achieve.
In this specific case I have a custom field where I can define the
missing
callback on the Field itself, and have thecontext
be provided to it.Add it to a simple UserSchema… (note that the
missing
parameter is not specified in theUserId
instantiation)And then parse…
Obviously the
missing
callable does not need to be defined as method on the field, but I figured this implementation might turn some gears for other potential solutions.I’m a noob to marshmallow, so perhaps there are better ways to skin/think about this. Much appreciated!
And thank your for all of your work!
Done, thanks!