0
@router.post('/update_attributes/{username}')
def update_attributes(request: Request, username: str, email: str = Form(...)):
    pass

How can I handle empty strings as input on my form? I get errors if the email textfield on my form is not filled in.

reckoner
  • 2,861
  • 3
  • 33
  • 43
  • Does this answer your question? [How to document default None/null in OpenAPI/Swagger using FastAPI?](https://stackoverflow.com/questions/72214347/how-to-document-default-none-null-in-openapi-swagger-using-fastapi) – Chris Oct 21 '22 at 05:31
  • Please have a look at [this answer](https://stackoverflow.com/a/73261442/17865804) ("About Optional Parameters" part) as well. – Chris Oct 21 '22 at 05:33

1 Answers1

2

The ellipses (...) means it is required. So leaving it empty would result in errors. If you want it not to be required, you can replace the ellipses with None like so:

@router.post('/update_attributes/{username}')
def update_attributes(request: Request, username: str, email: str = Form(None)):
    pass
JarroVGIT
  • 4,291
  • 1
  • 17
  • 29
  • Do you know where this is documented? I looked hard and could not find anything. – reckoner Oct 20 '22 at 22:39
  • https://fastapi.tiangolo.com/tutorial/query-params-str-validations/?h=el#required-with-ellipsis Although that is in the `Query` docs, the principle is the same for all parameter types (Form, File, Body etc) – JarroVGIT Oct 21 '22 at 05:45