-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Closed
Labels
Description
Sometimes, you have to perform additional validation of parameters (e.g., Path parameters) inside your API function. Currently, it is not very convenient to produce an error structured like the ones that the internal validation produces (e.g. when an int-typed param cannot be parsed).
Describe the solution you'd like
from fastapi import [Request]ValidationError
@app.post("/upload/{channel}")
async dev conda_upload(channel: str, file ...):
if not meets_some_condition(channel):
raise RequestValidationError(
["path", "channel"],
f"Channel '{channel}' does not match some contion",
"value_error.str.mycondition", # maybe this can be optional
# **extra, # Additional information
)Describe alternatives you've considered
You can explicitly construct the JSON structure. But you need to remember the correct responsec code and the correct JSON structure:
from fastapi import HTTPException
@app.post("/upload/{channel}")
async dev conda_upload(channel: str, file ...):
if not meets_some_condition(channel)
raise HTTPException(
422,
[{
'loc': ["path", 'channel'],
'msg': f"Channel '{channel}' does not match some condition",
'type': 'value_error.str.condition',
}]
)Additional context
Add any other context or screenshots about the feature request here.
ChristianHopper, sakti, tuukkamustonen, kissgyorgy, Pei116 and 6 more