Skip to content

Allow users to raise RequestValidationError #471

@sscherfke

Description

@sscherfke

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions