Add a hook to intercept ValidationError

This commit is contained in:
Vincent Maillol
2021-07-26 08:12:38 +02:00
parent 81138cc1c6
commit 7ab2d84263
4 changed files with 100 additions and 8 deletions

View File

@@ -310,6 +310,26 @@ Open Api Specification.
self.request.app["model"].remove_pet(id)
return web.Response(status=204)
Custom Validation error
-----------------------
You can redefine the on_validation_error hook in your PydanticView
.. code-block:: python3
class PetView(PydanticView):
async def on_validation_error(self,
exception: ValidationError,
context: str):
errors = exception.errors()
for error in errors:
error["in"] = context # context is "body", "headers", "path" or "query string"
error["custom"] = "your custom field ..."
return json_response(data=errors, status=400)
Demo
----