Opened 15 years ago
Closed 14 years ago
#13511 closed Cleanup/optimization (fixed)
RegexValidator pattern is optional and should be required
Reported by: | David Fischer | Owned by: | David Fischer |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | validators, regexvalidator |
Cc: | djfische@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
While working on ticket #13341, I noticed that the code for RegexValidator could use a slight cleanup. Specifically, the first parameter of RegexValidator -- the regex pattern -- is optional. However, if it is not provided, a TypeError is thrown because the default value of None
is passed to re.compile
. The regex pattern should be required.
Attachments (2)
Change History (11)
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 15 years ago
Cc: | added |
---|---|
Owner: | changed from | to
by , 15 years ago
Attachment: | 13511-regexvalidator.diff added |
---|
comment:3 by , 15 years ago
Has patch: | set |
---|---|
Status: | new → assigned |
I switched the regex
parameter to required to match the documentation. While running the unit tests (forms & validation), I noticed that URLValidator
relied on the fact that the regex
parameter was optional by having a regex
instance variable. I corrected that. However, this brings up a potential backwards compatibility issue.
The issue with this patch is that anybody who subclassed RegexValidator
and relied on regex
to be optional will have to update their code. However, it should be pretty obvious what went wrong and they probably should not have done that in the first place. I'm not sure how strict to be on maintaining backwards compatibility, but I think that breaking it in this small way will result in the cleanest code.
comment:4 by , 14 years ago
I just face this bug when subclassing RegexValidator. I think using self.regex instead regex as re.compile's parameter works and keeps backward compatilibity. Though, using RegexValidator without regex parameter will work in the same way that re.compile("") works.
-
django/core/validators.py
diff --git a/django/core/validators.py b/django/core/validators.py
a b 29 29 self.code = code 30 30 31 31 if isinstance(self.regex, basestring): 32 self.regex = re.compile( regex)32 self.regex = re.compile(self.regex) 33 33 34 34 def __call__(self, value): 35 35 """
comment:5 by , 14 years ago
@darkrho
That looks like it will work and it is better than what I had attached. It passes the unit test suite (validators) and it allows you to subclass RegexValidator. As you mentioned, creating a RegexValidator without specifying the "regex" will function the same as if the empty string was passed (a RegexValidator that always matches). I think that is fine, but maybe it needs to be communicated in the docs. The docs currently specify that "regex" is not optional. It can either be left like it is because a RegexValidator that matches everything is pretty useless, but perhaps the docs should just say that by default it matches everything.
comment:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Cleanup/optimization |
comment:7 by , 14 years ago
Needs tests: | set |
---|
by , 14 years ago
Attachment: | 13511-regexvalidator2.diff added |
---|
Patch leaves "regex" as optional, but documents and tests functionality
comment:8 by , 14 years ago
Easy pickings: | unset |
---|
I added a patch based on darkrho's suggestion. It will not break backward compatibility with subclasses of RegexValidator. I added a note in the docs that if the user does not specify any regex, then RegexValidator will match any passed string.
switched regex parameter to be required