Opened 4 years ago

Last modified 4 days ago

#18119 new New feature

add DomainNameValidator to validate Internet Domain Names

Reported by: michele Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords: validators
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Internet Domain Names are very present throughout, e.g. in URLs and e-mail addresses.

This patch adds django.core.validators.DomainNameValidator to check Internet Domain Names.

DomainNameValidator supports IDNA (utf) domain names, but it can reject them if optional accept_idna=False is passed to the constructor (defaults to accept).

DomainNameValidator is implemented as follows:

  • derives RegexValidator
  • uses the same RegEx of URLValidator (with the additional constrain to accept only 127 labels / 255 chars, as per RFC)
  • if plain validation fails, converts UTF -> ASCII with IDNA encoding and attempts to validate again (unless accept_idna=False)

Based on this Validator, I will submit separate patches to add DomainNameField support for models.

Attachments (2)

domainnamevalidator.txt (1.6 KB) - added by michele 4 years ago.
domainnamevalidator_2.txt (1.7 KB) - added by michele 4 years ago.
DomainNameValidator patch #2 - remove validator-own attributes before super()

Download all attachments as: .zip

Change History (8)

Changed 4 years ago by michele

Attachment: domainnamevalidator.txt added

comment:1 Changed 4 years ago by Claude Paroz

Needs documentation: set
Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

I think it is a good idea to centralize domain name validation. I detected at least three points where we could use this validator (django/core/mail/, django/core/ (URLValidator), django/utils/

For DRY reasons, the domain regex part should be shared with URLValidator. In __init__, just use self.accept_idna = kwargs.pop('accept_idna', True)

Changed 4 years ago by michele

Attachment: domainnamevalidator_2.txt added

DomainNameValidator patch #2 - remove validator-own attributes before super()

comment:2 Changed 4 years ago by michele

In attachment:domainnamevalidator_2.txt :

  • fix the constructor argument as per claudep suggestion
  • return a specific error message based on IDNA being accepted or not

comment:3 Changed 2 years ago by Berker Peksag

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Version: 1.4master

comment:4 Changed 22 months ago by Tim Graham

Patch needs improvement: set

This is blocked on #20003. Patch will need to be updated after that change.

comment:5 Changed 7 months ago by Thomas Güttler

would be nice to have

comment:6 Changed 4 days ago by Berker Peksag

Patch needs improvement: unset

Updated PR.

Note: See TracTickets for help on using tickets.
Back to Top