Schema dump method does not validate?
See original GitHub issueAccording 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:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top 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 >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
For anyone finding this thread (like I have), note that @dnshio’s approach doesn’t always work.
validate
ignores fields withdump_only=True
(see here –load_fields
are fields that are notdump_only
, as opposed todump_fields
). As a result, those fields will be treated as unknown fields. For example, the following code:raises
marshmallow.exceptions.ValidationError: {'name': ['Unknown field.']}
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: