question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to do GET request query parameter database validation which depends on multiple other query params.

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI
import datetime

app = FastAPI()

@app.get("/clinic/available/doctors/list/")
def get_supplier_available_experts_list_on_clinic(clinic_id: str, supplier_id: str, date: datetime.date):
    """Return the list of the experts for a given supplier to a given clinic on a specific date."""
    return {"experts": []}

Description

On my above code you can see the function get_supplier_available_experts_list_on_clinic() takes three URL parameters. One of them is clinic_id, one is supplier_id and other is a valid date. The function does some internal calculations to generated the list of available experts for a specific supplier to a specific clinic on a given date.

The problem is I want to validate the three parameters as follows:

  • There must be a valid clinic on the Database for the inputted Clinic ID
  • There must be a valid supplier on the Database for the inputted Supplier ID
  • There must be a relation between the Clinic and the Supplier on the Database
  • There must be a valid schedule for the Clinic and for the Supplier on the Database for the given Date

Currently, I can do these validations checkups on my internal scripts but I want to do it on the parameter level like using some Dependencies or other mechanisms. Is it currently possible with the FastAPI?

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.77.1

Python Version

3.10.2

Additional Context

Current Database: PostgreSQL 14

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
aboubacscommented, May 20, 2022

Validators don’t seem like a valid place for your use case. If you don’t want to implement your parameter validation logic in your endpoint (I’m not exactly sure why in the first place ?), you could do it with the dependency system. For example, instead of depending on “clinic_id”, your endpoint can depend on “clinic”, which depends on the database and a clinic_id (and raises an error if the clinic doesn’t exist).

Some sample code would be:


def get_db():
    try:
        db = your_function_to_get_a_database_session()
        yield db
    finally:
        db.close()


def get_clinic(db = Depends(get_db), clinic_id: str):
    # Reads the clinic from clinic_id
    # Raises an exception if the clinic doesn't exist

@app.get("/clinic/available/doctors/list/")
def get_supplier_available_experts_list_on_clinic(clinic: Depends(get_clinic), .......)
    .......
1reaction
acidjunkcommented, May 19, 2022

You could probably use a pydantic validator: to do some heavy lifting. https://pydantic-docs.helpmanual.io/usage/validators/

But considering that 2 of them need a DB (and a valid record) -> it would probably be good to raise 404 error’s when the ID don’t exist. And 422 when the relation doesn’t exist?

So IMO: it also depends on how you want to design your API.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query Parameters and String Validations - FastAPI
When you define a query parameter explicitly with Query you can also declare it to receive a list of values, or said in...
Read more >
java - How to accept multiple query parameters with different ...
To have these parameters as query parameters is just fine according to REST. Your controller method to handle the request will be good ......
Read more >
hapi — Query Parameter Validation With Joi - Future Studio
Joi will validate every query parameter if you're activating one or many for a given route. If you're going to use the route...
Read more >
Caching content based on query string parameters
For example, if requests for an object include two parameters that each have three different values, CloudFront caches six versions of that object,...
Read more >
REST API query parameters - Oracle Help Center
You can use query parameters to control what data is returned in endpoint responses.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found