so1n/pait

Response model support generic pydantic model

so1n opened this issue · 0 comments

so1n commented

Normal use:

from pydantic import BaseModel, Field


class RespModel(BaseModel):
    code: int = Field(0, description="api code")
    msg: str = Field("success", description="api status msg")
    data: dict = Field(description="success result")


class DataModel(BaseModel):
    uid: int = Field(description="user id", gt=10, lt=1000)
    user_name: str = Field(description="user name", min_length=2, max_length=10)
    age: int = Field(description="age", gt=1, lt=100)

class MyRespModel(RespModel):
    data: DataModel = Field(description="success result")

from pait.app.any import pait

@pait(response_model_list=[MyRespModel])
def demo():
    pass

Generic model use:

from pydantic import BaseModel, Field
from typing import TypeVar, Generic

T = TypeVar("T")

class RespModel(BaseModel, Generic[T]):
    code: int = Field(0, description="api code")
    msg: str = Field("success", description="api status msg")
    data: T = Field(description="success result")


class DataModel(BaseModel):
    uid: int = Field(description="user id", gt=10, lt=1000)
    user_name: str = Field(description="user name", min_length=2, max_length=10)
    age: int = Field(description="age", gt=1, lt=100)
    

from pait.app.any import pait

@pait(response_model_list=[RespModel[DataModel]])
def demo():
    pass