Dynamic Pydantic schemas based on user #5252
-
Is there a way to have Then, on every request, we get the user's folder name (ex. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
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 @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())
|
Beta Was this translation helpful? Give feedback.
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