spectree icon indicating copy to clipboard operation
spectree copied to clipboard

[Feature request] Global validation error model override

Open dfkki123508 opened this issue 2 years ago • 6 comments

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

dfkki123508 avatar Sep 13 '22 09:09 dfkki123508

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

kemingy avatar Sep 13 '22 09:09 kemingy

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.

dfkki123508 avatar Sep 18 '22 13:09 dfkki123508

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.

kemingy avatar Sep 18 '22 13:09 kemingy

Sure, but I'm referring to the generated docs. There is no possibility to globally override the standard validation error.

dfkki123508 avatar Sep 18 '22 14:09 dfkki123508

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)

kemingy avatar Sep 18 '22 14:09 kemingy

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.

dfkki123508 avatar Sep 18 '22 14:09 dfkki123508

For apps in my project this would also be useful!

alexted avatar Nov 07 '22 06:11 alexted

For apps in my project this would also be useful!

@alexted May I know your use case?

kemingy avatar Nov 09 '22 13:11 kemingy

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.

alexted avatar Nov 17 '22 07:11 alexted

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.

kemingy avatar Nov 19 '22 02:11 kemingy

Awesome thanks!

dfkki123508 avatar Nov 22 '22 19:11 dfkki123508