Skip to content
Discussion options

You must be logged in to vote

Because HTML browsers submit empty form inputs as an empty string ("") rather than omitting them or sending null, Pydantic attempts to validate "" against your EmailStr and min_length constraints, resulting in a validation error.

The best solution is to use Pydantic's BeforeValidator to intercept the data before standard validation runs, converting any empty string into None.

If you apply constraints like min_length at the top level of the Annotated type (e.g. Field(min_length=6)), Pydantic will attempt to apply the constraint to None values as well. This leads to a TypeError (e.g., object of type 'NoneType' has no len()).

To prevent this, the constraint should be nested inside the EmailStr

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@krono
Comment options

Answer selected by krono
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants