RFC: Don't pass a status_code as kwargs in a Field validator
See original GitHub issuewebargs’s ValidationError
adds status_code
and headers
to marshmallow’s ValidationError
. This allows to specify a default code for input validation error. The webserver uses this attribute to return the proper error code.
The tests show another use of this that I find a bit problematic.
It is possible to define a validator on a Field
of the input validation Schema
that raises a ValidationError
with a status_code
. The deserialization logic in marshmallow adds this attribute to the ValidationError
that is raised at the end of the validation process.
The issue with this is the case of several fields raising with a different status code. The last one to deserialize has priority. This is probably a corner case, but it shows the feature is not 100% reliable. It looks underterministic.
In practice I don’t see the use case for this in the first place. I generally use the default 422 for all validation errors and return specific codes from flask abort in view function or in specific decorator (authentication decorator,…).
Related marshmallow issue: https://github.com/marshmallow-code/marshmallow/issues/996.
My suggestion is to remove the logic that passes the kwargs in marshmallow and delete the test case here that exhibit the case of specifying a status code in a field validator.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
We’re using webargs’
abort
. It accepts kwargs it stores in exceptiondata
attribute, so I can pass a message inabort
.That’s two very different ways to address the problem, so it is hard to compare.
Yep.
I think you’re right about this, @lafrech . In retrospect, the
status_code
kwarg was the wrong solution to https://github.com/sloria/webargs/issues/85 . The use case is met by usingflask.abort
, as you said.Modified from the OP in #85:
So let’s move forward with this. It’d be good to have a minor release deprecating
status_code
andDEFAULT_VALIDATION_STATUS
before we remove it in 5.0.0. I might have some time to work on this over the next few days.