Opened 12 years ago

Last modified 7 years ago

#18119 new New feature

add DomainNameValidator to validate Internet Domain Names

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

Description

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 12 years ago.
domainnamevalidator_2.txt (1.7 KB) - added by michele 12 years ago.
DomainNameValidator patch #2 - remove validator-own attributes before super()

Download all attachments as: .zip

Change History (9)

Changed 12 years ago by michele

Attachment: domainnamevalidator.txt added

comment:1 Changed 12 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/message.py, django/core/validators.py (URLValidator), django/utils/html.py).

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 12 years ago by michele

Attachment: domainnamevalidator_2.txt added

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

comment:2 Changed 12 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 9 years ago by Berker Peksag

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

comment:4 Changed 9 years 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 8 years ago by Thomas Güttler

would be nice to have

comment:6 Changed 7 years ago by Berker Peksag

Patch needs improvement: unset

Updated PR.

comment:7 Changed 7 years ago by Tim Graham

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top