Opened 12 years ago

Last modified 7 weeks ago

#18119 assigned New feature

add DomainNameValidator to validate Internet Domain Names

Reported by: michele Owned by: Nina Menezes
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 (10)

by michele, 12 years ago

Attachment: domainnamevalidator.txt added

comment:1 by Claude Paroz, 12 years ago

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)

by michele, 12 years ago

Attachment: domainnamevalidator_2.txt added

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

comment:2 by michele, 12 years ago

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 by Berker Peksag, 9 years ago

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

comment:4 by Tim Graham, 9 years ago

Patch needs improvement: set

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

comment:5 by Thomas Güttler, 8 years ago

would be nice to have

comment:6 by Berker Peksag, 7 years ago

Patch needs improvement: unset

Updated PR.

comment:7 by Tim Graham, 7 years ago

Patch needs improvement: set

comment:8 by Nina Menezes, 7 weeks ago

Owner: changed from nobody to Nina Menezes
Status: newassigned
Note: See TracTickets for help on using tickets.
Back to Top