Re: [Django] #36809: Allow EmailValidator to customize error messages depending on the part that failed validation

1 view
Skip to first unread message

Django

unread,
Dec 23, 2025, 12:00:01 PM12/23/25
to django-...@googlegroups.com
#36809: Allow EmailValidator to customize error messages depending on the part that
failed validation
-------------------------------------+-------------------------------------
Reporter: Daniel E Onetti | Owner: Daniel E
Type: | Onetti
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: EmailValidator | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mike Edmunds):

* cc: Mike Edmunds (added)

Comment:

Natalia's proposal would also simplify implementation of #27029 EAI
address validation. (Or help developers who need that functionality sooner
subclass EmailValidator themselves.)

Django already uses "recipient" to mean a complete email address, possibly
including a friendly display name (e.g.,
django.core.mail.EmailMessage.all_recipients()). The official RFC 5322
term for the part before the @ is ''local-part'', but other common terms
are ''user'' (e.g., existing EmailValidator code), ''username'' (e.g.,
Python's email.headerregistry.Address.username), or ''mailbox''. I'd maybe
go with `validate_username()` and `validate_domain()` to align with
Python's email package.
--
Ticket URL: <https://code.djangoproject.com/ticket/36809#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 23, 2025, 12:24:07 PM12/23/25
to django-...@googlegroups.com
#36809: Allow EmailValidator to customize error messages depending on the part that
failed validation
-------------------------------------+-------------------------------------
Reporter: Daniel E Onetti | Owner: Daniel E
Type: | Onetti
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: EmailValidator | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

Thank you Mike for the validation! I appreciate it. Your naming proposal
sounds great!
--
Ticket URL: <https://code.djangoproject.com/ticket/36809#comment:7>

Django

unread,
Jan 28, 2026, 6:42:24 AM (2 days ago) Jan 28
to django-...@googlegroups.com
#36809: Allow EmailValidator to customize error messages depending on the part that
failed validation
-------------------------------------+-------------------------------------
Reporter: Daniel E Onetti | Owner: Daniel E
Type: | Onetti
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: EmailValidator | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jaffar Khan):

* cc: jaffar Khan (added)

Comment:

I want to work on this ticket as the current owner seem to no longer
active.
I think it is more genuine way to separate checks of username and domain
as Natalia proposed. I defined two methods **validate_username()** and
**validate_domain()** as:

{{{
def validate_username(self, user_part):
if not self.user_regex.match(user_part):
raise ValidationError(self.message, code=self.code,
params={"value": user_part})

def validate_domain(self, domain_part):
if domain_part not in self.domain_allowlist and not
self.domain_regex.match(domain_part):
raise ValidationError(self.message, code=self.code,
params={"value": domain_part})
}}}

then inside call, I called these two methods and removed the username and
domain checks as it looks no longer necessary:


{{{
def __call__(self, value):
# The maximum length of an email is 320 characters per RFC 3696
# section 3.
if not value or "@" not in value or len(value) > 320:
raise ValidationError(self.message, code=self.code,
params={"value": value})

user_part, domain_part = value.rsplit("@", 1)

self.validate_username(user_part)
self.validate_domain(domain_part)
}}}

I think it will be a more developer-friendly way. Please let me know
whether to create a PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/36809#comment:8>

Django

unread,
Jan 28, 2026, 8:18:16 AM (2 days ago) Jan 28
to django-...@googlegroups.com
#36809: Allow EmailValidator to customize error messages depending on the part that
failed validation
-------------------------------------+-------------------------------------
Reporter: Daniel E Onetti | Owner: Daniel E
Type: | Onetti
Cleanup/optimization | Status: assigned
Component: Core (Other) | Version: dev
Severity: Normal | Resolution:
Keywords: EmailValidator | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by jaffar Khan):

I checked **docs/ref/validators.txt**, and there is no description about
methods of classes, so I don't think documentation changes are needed.
--
Ticket URL: <https://code.djangoproject.com/ticket/36809#comment:9>
Reply all
Reply to author
Forward
0 new messages