missing and default values should be specified in deserialized form
See original GitHub issueMarshmallow expects missing
and default
values to be specified in a pre-serialized form, which is inconvenient and unintuitive.
Here is a schema which I would expect to work:
import datetime
import uuid
class TestSchema(Schema):
id = fields.UUID(
missing=uuid.uuid1)
ts = fields.DateTime(
format='rfc',
missing=datetime.datetime.now)
However, it does not:
schema = TestSchema()
pprint.pprint(tuple(schema.load({})))
({}, {'id': [u'Not a valid UUID.'], 'ts': [u'Not a valid datetime.']})
Instead, I have to convert my default
and missing
values to primitive types, which undermines the purpose of a library like marshmallow that already knows how to do this. For data types like datetimes, which have many possible serialized formats, I have to keep the formatting of my default
and missing
values in sync with my field properties, which is fragile and convoluted:
from email.Utils import formatdate
class TestSchema(Schema):
id = fields.UUID(
missing=lambda: str(uuid.uuid1()))
ts = fields.DateTime(
format='rfc',
missing=formatdate)
In the case of default
values, marshmallow does not attempt to validate them, so no error is raised, but the resulting document is not deserializable.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
c# - Default value for missing properties with JSON.net
I tried DefaultValue attribute but it's doesn't work. I'm using private setters and constructor to deserialize the json, so setting the value of ......
Read more >Data Member Default Values - WCF - Microsoft Learn
To omit a member from serialized data, set the EmitDefaultValue property of the DataMemberAttribute attribute to false (the default is true ).
Read more >Default value for a field - Serde
Default value for a field. use serde::Deserialize; #[derive(Deserialize, Debug)] struct Request { // Use the result of a function as the default if ......
Read more >Colander Basics - The Pylons Project
The default of a schema node indicates the value to be serialized if a value for the schema node is not found in...
Read more >API Reference — marshmallow 2.21.0 documentation
Deserialize a data structure to an object defined by this Schema's fields and ... default – If set, this value will be used...
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 just sent a PR to achieve this.
@chadrik, @costrouc, @sloria, feedback welcome.
That workaround works but then context is not there. I get the idea for the change but the defaulting nested schemas just became a hell of a lot more convoluted.