so1n/pait

A simpler way to use Json responses

so1n opened this issue · 0 comments

so1n commented

The declaration of the JsonResponse in the example is too complicated:

class DemoResponseModel(JsonResponseModel):
    class ResponseModel(BaseModel):
        uid: int = Field()
        user_name: str = Field()

    description: str = "demo response"
    response_data: Type[BaseModel] = ResponseModel


@pait(response_model_list=[DemoResponseModel])
async def demo_post(
    uid: int = Json.t(description="user id", gt=10, lt=1000),
    username: str = Json.t(description="user name", min_length=2, max_length=4),
) -> JSONResponse:
    return JSONResponse({"uid": uid, "user_name": username})

Json is a common API response format, can define some rules to make the definition of JsonResponse more simple, such as :

class DemoResponseModel(BaseModel):
    """demo response"""
    uid: int = Field()
    user_name: str = Field()

@pait(response_model_list=[DemoResponseModel])
async def demo_post(
    uid: int = Json.t(description="user id", gt=10, lt=1000),
    username: str = Json.t(description="user name", min_length=2, max_length=4),
) -> JSONResponse:
    return JSONResponse({"uid": uid, "user_name": username})