Freature request: allow handle_error to suppress an error.
See original GitHub issueIn do_load
you have the following code block:
if errors:
exc = ValidationError(errors, data=data, valid_data=result)
self.handle_error(exc, data, many=many, partial=partial)
raise exc
So the exception is raised unconditionally. This means that there is no way for a custom schema to correct or suppress an error raised by a sub-schema.
Our use case is wanting to have a schema in which some fields are validated, but the remainder are passed “as is” (they are massaged and validated later on in our data pipeline). In Marshmallow 2 we set our schema to strict=False and had our handle_error raise on the fields we wanted validated. As far as I can see there is no equivalent functionality in Marshmallow 3. This could be solved by having the code that calls handle_error allow it to signal that the exception be ignored. For example, it could treat a return value of True
from handle_error as signalling that the error has been successfully handled and should not be raised.
(By the way, this use case is similar to that mentioned in #1198, but we aren’t serializing an object, just a json data structure. We are currently using a @post_dump method to include the unknown fields in the dump, but I haven’t actually gotten far enough yet to know if that will actually work in Marshmallow 3 😃
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
The validation error that is raised has a
valid_data
property you can use to recover the valid data.https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation
I think this is what @bitdancer ended up doing:
Ah, nevermind. What I requested wouldn’t do what we wanted because _do_load is called from validate as well.
What I ended up with was adding a ‘load’ method to our schema that calls super inside a try/except and does what we want. Sorry for wasting your time 😦