Effortless Query Parameter Validation in FastAPI

The validation are performed to ensure the Data Integrity, with FastAPI, it is even simple to put validation in the decorator method. We will see this in follow up example.

The parameter validation are of two type:

  1. Query Parameter Validation
  2. Path Parameter Validation

In this post we will cover different kind of validation we can perform with Query Parameter:

  1. Required Parameter Validation:
  2. Default Value with Query Parameter
  3. Min and Max validation on Query Parameter
  4. Less than and greater than Query Validation

Required Parameter Validation:

@app.get("/jupiter/")
async def read_jupiter(jup_value: str = Query(..., description="Query Para is required field")):
    return {"jup": jup_value}

Here we have used “…” which refers that this field is mandatory and the value need to be passed in this parameter mandatorily.

Default Value with Query Parameter

@app.get("/count/")
async def read_count(count: int = Query(5,title="Less Than Validation", description="less then 10", le=10)):
    return {"jup": count}

Here in the Query construct, if you see the first parameter is passed as 5, that is nothing but the default parameter, if you don’t pass anything while calling this api, by default it will take 5 as parameter.

Min and Max validation on Query Parameter

@app.get("/min_max_input/")
async def read_min_max_input(count: int = Query(5,title="Less Than Validation", description="less than 100", ge=0, le=100)):
    return {"jup": count}

Here in the Query construct, we have used ge and le, which corresponds to “greater than equal to” and “less than equal it” as 0 and 100. So it will accept the value which is ranging from 1 to 100.

Min Length and Max Length Query Parameter

@app.get("/min_max_str_input/")
async def read_min_max_str_input(name: str = Query(...,title="min and max length validation",
                                                   description="min and max validation", min_length=3, max_length=10)):
    return {"name": name}

Here the validation is performed on string datatype where it checks the length of the string and find if the length of the string is less than 3 or more than 10 through validation parameter min_length and max_length.

Checking the validation on swagger UI:

Required Parameter Validation on SwiggerUI:

Here if you see the jup value parameter comes with red *, which signifies this is compulsory field and need a value:

Default Value with Query Parameter with SwaggerUI:

Here if you see the default value is set to 5, if we don’t pass anything, by default it will take 5 as parameter.

Min and Max validation on Query Parameter with SwaggerUI:

Here it shows the default value is 5, min value can be entered is 0 and max is 100, lets tryout

Since we passed parameter value out of range i.e. more than 100, we got the validation error.

Min Length and Max Length Query Parameter

In this post we saw some validation on query parameter, in the next post we will see more validation with Path Parameter.

Happy Coding!

23 Comments

  1. It’s perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or tips. Maybe you could write next articles referring to this article. I want to read even more things about it!

  2. Have you ever thought about publishing an ebook or guest authoring on other websites? I have a blog based upon on the same information you discuss and would really like to have you share some stories/information. I know my audience would value your work. If you are even remotely interested, feel free to send me an email.

  3. You lost me, friend. I mean, I imagine I recieve what youre saying. I recognize what you’re saying, but the truth is just appear to have forgotten that might be some other folks in the world who view this concern for which it happens to be and may perhaps not believe you. You might be turning away alot of folks that might have been lovers of one’s website.

  4. Hey there! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no data backup. Do you have any methods to stop hackers?

  5. This important put up appears to be redeem a majority of customers. How would you market it? That it provides a fantastic one of a kind style directly on factors. My partner and i getting a specific product realistic or just sizeable furnish information on is a vital process.

  6. Amazingly, your piece goes to the heart of the topic. Your clarity leaves me wanting to know more. Just so you know, i’ll immediately grab your feed to keep up currently with your online blog. Sounding Out thanks is merely my little way of claiming what a masterpiece for a fantastic resource. Take On my best wishes for your subsequent publish.

  7. It is actually a nice and helpful piece of information. I’m satisfied that you shared this helpful info with us. Please stay us informed like this. Thank you for sharing.

  8. Oh my goodness! an excellent write-up dude. Thanks a ton Even so We are experiencing problem with ur rss . Do not know why Unable to register for it. Could there be everyone getting identical rss problem? Anybody who knows kindly respond. Thnkx

  9. I discovered your site website on bing and check some of your early posts. Always maintain inside the great operate. I recently additional encourage RSS feed to my MSN News Reader. Looking for forward to reading more within you at a later time!…

  10. Hey there! I’m at work browsing your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the great work!

  11. I am often to blogging and i really appreciate your content. Your content has truly peaks my interest. My goal is to bookmark your web blog and keep checking for brand new information.

  12. I was suggested this website by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *