pgjones / quart-schema

Quart-Schema is a Quart extension that provides schema validation and auto-generated API documentation.
MIT License
76 stars 24 forks source link

added ability to use raw pydantic models without datasclasses #59

Closed kodecreer closed 7 months ago

kodecreer commented 11 months ago

I added this functionality for a personal project of mine. Don't know if you want it or not, but you can define normal BaseModels from Pydantic V2 and able to use it as a decorator to your route. This uses the naming of validate_pydantic_model to prevent issues with backward compatibility. It will work as long as the Json sent in will be able to be converted into a Pydantic object. This doesn't have OpenAPI or Redoc support. This only has the functionality for Pydantic V2 model validation.

pgjones commented 10 months ago

You should be able to use Pydantic models with the existing decorators - is there a bug you experience?

kodecreer commented 10 months ago

I haven't seen any examples in the documentation where I can just throw in a normal pydantic base model without needing converting things into dataclasses and relies on the pydantic validation.

Do you have an example for using like pydantic model? The code I submitted allows this kind of usage.

For example

class Foo(BaseModel)
    ...

@validate_pydantic_model(Foo)
@app.route('/')
def index(foo: Foo):
    ...

Forgive any syntax errors I am typing this on my phone but it communicates the whole point.

Right now it's only able to handle just one model, but it's easy enough to scale it with some json parsing. Thank you for taking the time to respond and for your work in this repository.

Let me know about the examples or any questions you have when you got the chance.

pgjones commented 7 months ago

I've improved the documentation for this.