EmailStr
noun
say it: “EE-mayl STRING” /ˈiˌmeɪl strɪŋ/
Pydantic's type for an email address: it checks that the text has an @ and a domain after it, but not that the mailbox exists.
What does EmailStr check?
EmailStr is Pydantic's type for an email address: the text must have the shape of one, with an @ and a domain
after it. It can't tell whether the mailbox exists; only sending a message can. It needs the email-validator
package, which fastapi[standard] installs.
from pydantic import BaseModel, EmailStr, ValidationError
class Application(BaseModel):
email: EmailStr
print(Application(email="rosa@example.com").email) # rosa@example.com
try:
Application(email="rosa.example.com") # no @
except ValidationError:
print("refused") # refused
In a sentence
- “Because the email field is an EmailStr, an application with rosa.example.com is refused with a 422.”
- “An EmailStr let sam@example.com through, since it checks the shape of an address, not whether anyone reads it.”
Level: Apprentice · More FastAPI terms at this level
Updated