Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmailStr is not case-sensitive on local part of email per RFC 5321 #798

Closed
henriklindgren opened this issue Sep 9, 2019 · 6 comments
Closed
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome

Comments

@henriklindgren
Copy link
Contributor

Bug

EmailStr validate-method performs lower() on whole string including local part, violating RFC 5321 by not conforming to

The local-part of a mailbox MUST BE treated as case sensitive.
https://tools.ietf.org/rfc/rfc5321.txt

See
https://github.com/samuelcolvin/pydantic/blob/79017111aa030873f2dc54576e2c768d154b1be5/pydantic/networks.py#L383

  • OS: Any
  • Python version import sys; print(sys.version): Any
  • Pydantic version import pydantic; print(pydantic.VERSION): 0.32.2

Where possible please include a self contained code snippet describing your bug:

import pydantic
assert pydantic.EmailStr.validate('AbC@example.com') == 'AbC@example.com'
@henriklindgren henriklindgren added the bug V1 Bug related to Pydantic V1.X label Sep 9, 2019
@samuelcolvin
Copy link
Member

Thanks for reporting, PR welcome to fix this.

@dmontagu
Copy link
Contributor

dmontagu commented Oct 7, 2019

Closing as it looks like this was addressed by #801. Thanks for the contribution @henriklindgren!

@dmontagu dmontagu closed this as completed Oct 7, 2019
@slavugan
Copy link

slavugan commented Apr 5, 2021

this RFC 5321 looks outdated as most email services works with emails as case insensitive, and following this RFC just complicates our life. just IMHO.

@emilpaw
Copy link

emilpaw commented Apr 8, 2021

Is there a simple way to get the old behavior back?

@myrataltyyev
Copy link

myrataltyyev commented Jan 5, 2023

If you want case insensitive email, inherit EmailStr and convert the value to lowercase.

class CustomEmailStr(EmailStr):
    @classmethod
    def validate(cls, value: EmailStr) -> EmailStr:
        email = validate_email(value)[1]
        return email.lower()

Note: validate method overrode the EmailStr's validate method. That's why had to include the lines of the EmailStr validation.

alexdrydew pushed a commit to alexdrydew/pydantic that referenced this issue Dec 23, 2023
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@amirsoroush
Copy link

If you want case insensitive email, inherit EmailStr and convert the value to lowercase.

class CustomEmailStr(EmailStr):
    @classmethod
    def validate(cls, value: EmailStr) -> EmailStr:
        email = validate_email(value)[1]
        return email.lower()

Note: validate method overrode the EmailStr's validate method. That's why had to include the lines of the EmailStr validation.

It's now called _validate():

https://github.com/pydantic/pydantic/blob/main/pydantic/networks.py#L418

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X help wanted Pull Request welcome
Projects
None yet
Development

No branches or pull requests

7 participants