Fastapi: Optional custom

Created on 9 Aug 2020  路  4Comments  路  Source: tiangolo/fastapi

email: Optional[str] = Query(..., description="email", min_length=10, max_length=50,regex="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$")

result

{
    "detail": [
        {
            "loc": [
                "query",
                "email"
            ],
            "msg": "string does not match regex \"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$\"",
            "type": "value_error.str.regex",
            "ctx": {
                "pattern": "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
            }
        }
    ]
}

"msg": "string does not match regex \"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$\""
I think it's ugly
How to custom

answered question

Most helpful comment

You can use EmailStr from Pydantic:

from pydantic import EmailStr

All 4 comments

You can use EmailStr from Pydantic:

from pydantic import EmailStr

Is the problem resolved @dyhawks? If it is, do you mind closing the issue then?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Thanks for the help here @Kludex and @ycd ! :clap: :bow:

Was this page helpful?
0 / 5 - 0 ratings