#20784: RegexValidator should accept a new parameter to perform reversed 
validation
------------------------------+----------------------------
     Reporter:  devfeng       |      Owner:  nobody
         Type:  New feature   |     Status:  new
    Component:  Core (Other)  |    Version:  master
     Severity:  Normal        |   Keywords:  RegexValidator
 Triage Stage:  Unreviewed    |  Has patch:  1
Easy pickings:  0             |      UI/UX:  0
------------------------------+----------------------------
 In current implementation, RegexValidator only raises ValidationError when
 pattern DOES NOT match regex, while it is pretty common to use a
 "reversed" RegexValidator that raises ValidationError when pattern MATCHES
 regex.  A typical scenario is to catch potential XSS inputs in form field
 validation (if it matches then the validator should panic).

 Technically such reversed match could be performed by tweaking the regex
 itself, however in real world, not everybody is a regex master and there
 are a lot of people who may prefer a more straightforward solution as
 simple as "not some_condition".

 In my own projects, I've written a ReversedRegexValidator by subclassing
 RegexValidator and overriding the __call__() method to change it's
 behavior (basically it just copied everything and then removed the "not"
 statement).  It worked well, however there are some problems:

 1. RegexValidator uses some Django internal utils that are not documented
 during upgrades.  For example, in Django 1.4.x, RegexValidator used
 smart_unicode() from utils.encoding, while in Django 1.5.x, it changed to
 force_text().  The custom ReversedRegexValidator will need to be upgraded
 as well for such internal change.
 2. Overriding the whole __call__() method in order to just remove (or
 have) a "not" operation seems to be too much.  But that's the current
 problem with RegexValidator.

 So my suggestion is to add a new parameter, say "reverse", to
 RegexValidator.  By default it's False and won't change anything to
 existing codes, but a user can very easily change it's matching behavior
 by passing reverse=True.

 I've had my changes ready for review.  Test cases have been updated as
 well.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20784>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.63ec669a2c3e45aec5143ad14ab10724%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to