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.

Schema dump method does not validate?

See original GitHub issue

According to the docs:

Serialize objects by passing them to your schema’s dump method, which returns the formatted result (as well as a dictionary of validation errors…)

But as far as I can tell, the Schema.dump method does not perform validation. I have:

>>> from marshmallow import Schema
>>> from marshmallow.fields import String
>>> from marshmallow.validate import Regexp
>>> class TestSchema(Schema):
...     testfield1 = String(validate=Regexp('^foo'))
...     testfield2 = String(default='teststring')

>>> TestSchema().dump({'testfield1': 'bar'})
MarshalResult(data={'testfield2': 'teststring', 'testfield1': 'bar'}, errors={})

Based on the docs, I thought I would see validation errors in the errors key. The load method validates as expected:

>>> TestSchema().load({'testfield1': 'bar'})
UnmarshalResult(data={}, errors={'testfield1': ['String does not match expected pattern.']})

But the load message does not apply default values:

>>> TestSchema().load({})
UnmarshalResult(data={}, errors={})

So the only way to get a validated data structure with default values applied appears to be:

>>> result1 = TestSchema().load({'testfield1': 'foo'})
>>> result2 = TestSchema().dump(result1.data)
>>> result2.data
{'testfield2': 'teststring', 'testfield1': 'foo'}

The above is with:

>>> marshmallow.__version__
'2.13.6'
>>> sys.version
'3.5.4 (default, Aug 23 2017, 18:32:05) \n[GCC 6.4.1 20170727 (Red Hat 6.4.1-1)]'

Am I simply misreading the documentation?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
afalloucommented, Jan 30, 2020

For anyone finding this thread (like I have), note that @dnshio’s approach doesn’t always work.

validate ignores fields with dump_only=True (see hereload_fields are fields that are not dump_only, as opposed to dump_fields). As a result, those fields will be treated as unknown fields. For example, the following code:

class PersonSchema(Schema):
    name = fields.String(
        dump_only=True,
    )

schema = PersonSchema()
errors = schema.validate(dict(name="John"))
if errors:
    raise ValidationError(message=errors)

raises marshmallow.exceptions.ValidationError: {'name': ['Unknown field.']}

9reactions
dnshiocommented, Apr 10, 2019

I wanted to validate during serialisation time and was disappointed to find this thread.

However, it’s not that difficult to do what I wanted by validating the output from .dump(). e.g:

schema = SomethingSchema()
data, errors = schema.dump(some_object)

# call schema.validate to raise ValidationErrors 
schema.validate(data)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Quickstart — marshmallow 3.19.0 documentation
Validation occurs on deserialization but not on serialization. To improve serialization performance, data passed to Schema.dump() are considered valid.
Read more >
Marshmallow How to Enforce a Required Field when Dumping?
Marshmallow only validates on load. It is a design choice. You can use a pre_load method for this. I just realized this could...
Read more >
schema - PyPI
validate (data). This method may raise SchemaError exception, which will tell Schema that that piece of data is invalid, otherwise—it will continue validating....
Read more >
Source code for marshmallow.schema - MindMeld
Also sets the ``opts`` class attribute, which is the Schema class's ``class Meta`` ... Whether to ignore missing fields and not require any...
Read more >
Data Schema - APIFlask
Deserialization (load) and serialization (dump)¶. In APIFlask (marshmallow), the process of parsing and validating the input request data is called ...
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