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.

missing and default values should be specified in deserialized form

See original GitHub issue

Marshmallow 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:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lafrechcommented, Mar 21, 2018

I just sent a PR to achieve this.

@chadrik, @costrouc, @sloria, feedback welcome.

0reactions
Kareeeeemcommented, Jan 28, 2019

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.

Read more comments on GitHub >

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

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