#25595 closed Bug (fixed)
Invalid regexp in URLValidator can't handle file:// schemes
| Reported by: | Marcin Nowak | Owned by: | Adam Zapletal |
|---|---|---|---|
| Component: | Core (Other) | Version: | 1.8 |
| Severity: | Normal | Keywords: | |
| Cc: | Adam Zapletal | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Regexp does not allow to use file scheme in URLValidator.
Steps to reproduce
from django.core.validators import URLValidator
URLValidator(schemes=['file'])('file:///tmp/somefile')
Expected result
No exception should be raised.
Current result
ValidationError: [u'Enter a valid URL.']
Change History (9)
comment:1 by , 10 years ago
| Component: | Uncategorized → Core (Other) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 10 years ago
It's URLValidator not URIValidator, I think you might need your own custom validator for this. The current regex is already bloated, I'm not sure we should continue to complexify it...
comment:3 by , 10 years ago
I had a similar skepticism. If we don't make a change, then let's clarify the documentation to prevent further tickets about this.
comment:4 by , 10 years ago
I thinks this is both a documentation and code problem. The URLValidator should raise a value error, if a unsupported schema is set. In the documentation it should be explained with RFC 1738 schemas are supported. Currently one gets the impression that all are.
comment:5 by , 10 years ago
Interestingly enough, if you include the optional host parameter in RFC1738 it works: file://localhost/tmp/file/name
comment:6 by , 20 months ago
| Cc: | added |
|---|---|
| Has patch: | set |
| Owner: | changed from to |
| Status: | new → assigned |
I opened a PR adding a warning about this to the documentation. I'm happy to handle it in a different way if that'd be better.
For what it's worth, if someone out there wants to validate local file URIs in addition to URLs, here is an idea to start with:
@deconstructible class CustomURLValidator(URLValidator): def __init__(self, **kwargs): super().__init__(**kwargs) self.schemes.append('file') def __call__(self, value): if value.startswith('file:///'): value = value.replace('file:///', 'file://localhost/', 1) return super().__call__(value)
comment:7 by , 20 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Can this be fixed without special casing the file scheme in the validation?