DEV Community

Cover image for FastAPI - The Good, the bad and the ugly.

FastAPI - The Good, the bad and the ugly.

Muhtasim Fuad Rafid on August 04, 2020

FastAPI is a relatively new web framework for Python claiming to be one of the fastest Python frameworks available. In this article, I will discuss...
Collapse
 
matixezor profile image
Mateusz Romański

@fuadrafid
Actually to pass validation error message you can just use pydantic @validator decorator. For example:

class User(BaseModel):
    phone: str

    @validator('phone')
    def phone_validator(cls, v):
        if v and len(v) != 9 or not v.isdigit():
            raise ValueError('Invalid phone number')
        return v
Enter fullscreen mode Exit fullscreen mode

Then on validation error this will be the response body:

{
  "detail": [
    {
      "loc": [
        "body",
        "phone"
      ],
      "msg": "Invalid phone number",
      "type": "value_error"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid • Edited

Thank you for reading!
You are suggesting custom validators, I was talking about the default validators provided by pydantic. These work fine, but image having a project with 50+ dataclasses, writing validators for each of the variables isn't really efficient.
You can see Springboot's validators. A simple message inside the annotation itself, simple and efficient.

Collapse
 
vitalik_28 profile image
Vitaliy Kucheryaviy

Hi ! good thoughts

About your last issue..
I'm working on the project django-ninja ( github.com/vitalik/django-ninja ) which also uses Pydantic for validation
And also think on some ways to give users a way to customize validation messages...

Do you have any design in mind ? like how would you prefer to customize validation message and on which layer ?

Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid • Edited

Hi,
Thank for sharing your work. I think the validation can be incorporated with the dtos with an annotation and validation checks should be at the middleware layer.

Collapse
 
tfutada profile image
Takashi Futada

Thanks. It's a good read. As mentioned in Cons 2, async/await like Node.js style could be a big advantage in case that the web app is not CPU bounded and needs to deal with a lot of requests dispatching them to backend servers, database and APIs. I am on it.

Collapse
 
mirzadelic profile image
Mirza Delic

I am having the same problem with validations with FastAPI.
Django Rest Framework is doing a great job with this, example:

{
    "name": ["This field is required."]
}
Enter fullscreen mode Exit fullscreen mode

Did you found any solution for better formating of validation errors in FastAPI?

Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid

Hello Mirza,
I haven't really looked into it. But you can look into this GitHub thread:
github.com/tiangolo/fastapi/issues...

Collapse
 
miloszstx profile image
Miłosz Gałganek

Like Takashi Futada commented here—I think the async/await Python keywords is a huge plus for FastAPI, no need for a third-party framework to do asynchronous. My colleague wrote a comparison of the two frameworks that you might find interesting: stxnext.com/blog/fastapi-vs-flask-...

Collapse
 
shuv1824 profile image
Shah Nawaz Shuvo

Nice post. I have recently started using FastAPI. I think it will be much better when a stable version of it will be released. Waiting for version 1.0.0 actually. I have high hopes for FastAPI.

Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid

Thanks. The future of FastAPI looks good to me too. Happy coding!

Collapse
 
meadsteve profile image
Steve B

[Advertisement warning] regarding con point 2 I wrote a dependency injection framework which works well with fastapi and addresses this point: github.com/meadsteve/lagom

Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid • Edited

Thanks for sharing your work! It seems feature rich and easy to use. Will try it out in the future.

Collapse
 
rashik4567 profile image
Rashik

After all, the performance is a very high value for fastapi... though i am mastered in django, i am learning fastapi instead of flask. I think FastAPI will be better as time goes.

Collapse
 
fuadrafid profile image
Muhtasim Fuad Rafid

Thank you for reading! Hope it gets better with time.

Collapse
 
theinfosecguy profile image
Keshav Malik

Although the documentation is good but the resources available for integrating it with other services are very few. Hopefully it will get better with time.

Btw, nice article! :)