0

I'm building a FastAPI project, and in it, I'd like all my errors to be structured as such:

errors: {
    #... whatever  
}

I know I can create a pydantic schema as such:

class Error(BaseModel):
    error: Dict

However, for documentation sake, I'd like to avoid having to create two schemas every time I want to set an output response:

class AuthError(BaseModel):
    invalid_user = True

class Error(BaseModel):
    error: AuthError

Is there a better way to do this?

Rohit
  • 3,018
  • 2
  • 29
  • 58
  • If what you need is a json containing anything there is no point to using pydantic since it's purpose is to map json structurs – Bastien B Jan 03 '22 at 08:40
  • @BastienB It's not arbitrary json objects. rather that each error object has the same wrapping structure. – Rohit Jan 04 '22 at 00:40
  • 1
    check if this helps https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers creating a custom exception handler and json response..this is related to raising error, however it is not related to extending base schemas – N.Moudgil Jan 04 '22 at 13:04

0 Answers0