spectree
spectree copied to clipboard
[Feature request] Global validation error model override
It would be really cool to not only be able to override the validation_error_status
globally but also the validation error model. e.g.
spectree.spec.SpecTree(..., validation_error_model=MyPydanticErrorModel)
What do you think? I would be happy to contribute.
BR
It has a global before
function which will be called in validate > before() > endpoint func > after() > response
.
spec = SpecTree(before=your_func_to_modify_the_error_model)
Check the example: https://github.com/0b01001001/spectree/blob/bb96e101b10bc02daffa75a1b910a104811476e0/tests/test_plugin_flask.py#L26-L28
And the doc: https://0b01001001.github.io/spectree/utils.html#spectree.utils.default_before_handler
Yes, I saw that you can intercept the error handling with before and after. However, in the docs there will always display the standard error model, but I want to override it with my own error model. Technically it is possible if I add the validation error response to every endpoint, but this is a lot of redundant work, so overriding the standard error model globally would be nice.
Technically it is possible if I add the validation error response to every endpoint, but this is a lot of redundant work, so overriding the standard error model globally would be nice.
You don't have to override one by one. You can define the global before
handle and it will work on all the endpoints.
Sure, but I'm referring to the generated docs. There is no possibility to globally override the standard validation error.
This is global:
spec = SpecTree(before=your_func_to_modify_the_error_model)
This is a specific function:
spec.validate(before=your_func_to_modify_the_error_model)
No this is not what I mean :smile: Sorry, for the confusion. I try to explain in a different way: This line adds the validation error to the responses which is later used in spec._generate_spec
. So the api docs for every endpoint (not overriding 422 response) will look like this:
responses:
'422':
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError.6a07bef'
So I want to tell spectree to use a custom model by default instead of the standard validation error.
Does this make it clearer? Thanks for your answers.
For apps in my project this would also be useful!
For apps in my project this would also be useful!
@alexted May I know your use case?
On our project, all applications have a middleware that intercepts all errors that occur and converts them into a standardized response with a certain set of fields.
On our project, all applications have a middleware that intercepts all errors that occur and converts them into a standardized response with a certain set of fields.
I see. The PR has been merged. Close this.
Awesome thanks!