Skip to content
Discussion options

You must be logged in to vote

The only way is to define a universal model that will fit for all users. Maybe using discriminated unions.

Or, if you don't care about openapi schema, you can exclude body parameters from endpoint declaration and validate body data manually from request.json

@app.post("/submit")
async def submit(request: Request):
    user_id = request.headers.get("X-User-ID")
    if not user_id:
        raise HTTPException(status_code=400, detail="Missing user ID")

    schema_cls = get_user_schema(user_id)

    try:
        body = await request.json()
        validated_data = schema_cls(**body)
    except ValidationError as e:
        raise HTTPException(status_code=422, detail=e.errors())
   

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants