username query param validation
Closed this issue · 0 comments
zhvnibek commented
@aidanakenes
In your /profile endpoint you may define the query validation right in the function arguments using Query as in https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#make-it-required. Please have a look at what useful tips this Query class proposes.
Your code may look like this:
@app.get(
'/profile',
description='Get profile info'
)
def get(username: str = Query(..., min_length=1, max_length=30, regex='yourregex')): # ... means required
# if the username is not valid, it will be rejected (raises fastapi.RequestValidationError)
# and will not get to the next line
parser.get_user(username=username) # use it directly, because the username is surely valid