Skip to content

Commit b03a63c

Browse files
committed
Use rfc3987
1 parent 5f0d289 commit b03a63c

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

docs/validate.rst

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,6 @@ with.
290290
>>> is_ipv6("^(bob?cat$")
291291
False
292292

293-
.. autofunction:: is_uri
294-
295-
Check if the instance is a valid URI.
296-
297-
Also supports relative URIs.
298-
299-
>>> is_uri("ftp://[email protected]:8080/pub/os/")
300-
True
301-
>>> is_uri("http://www2.example.com:8000/pub/#os?user=joe.bloggs")
302-
True
303-
>>> is_uri(r"\\\\WINDOWS\My Files")
304-
False
305-
>>> is_uri("#/properties/foo")
306-
True
307-
308293
.. autofunction:: is_email
309294

310295
Check if the instance is a valid e-mail address.
@@ -371,6 +356,24 @@ On OSes with the ``socket.inet_pton`` function, an additional checker for
371356
False
372357

373358

359+
If the rfc3987_ library is present, a checker for URIs will be present.
360+
361+
.. function:: is_uri
362+
363+
Check if the instance is a valid URI.
364+
365+
Also supports relative URIs.
366+
367+
>>> is_uri("ftp://[email protected]:8080/pub/os/")
368+
True
369+
>>> is_uri("http://www2.example.com:8000/pub/#os?user=joe.bloggs")
370+
True
371+
>>> is_uri(r"\\\\WINDOWS\My Files")
372+
False
373+
>>> is_uri("#/properties/foo")
374+
True
375+
376+
374377
If the isodate_ library is present, a date-time checker will also be present.
375378

376379
.. function:: is_date_time(instance)
@@ -409,4 +412,5 @@ CSS will be enabled:
409412
True
410413

411414
.. _isodate: http://pypi.python.org/pypi/isodate/
415+
.. _rfc3987: http://pypi.python.org/pypi/rfc3987/
412416
.. _webcolors: http://pypi.python.org/pypi/webcolors/

jsonschema.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,6 @@
2121
import socket
2222
import sys
2323

24-
try:
25-
import webcolors
26-
except ImportError:
27-
webcolors = None
28-
29-
try:
30-
import isodate
31-
except ImportError:
32-
isodate = None
33-
34-
3524
__version__ = "1.0.0-dev"
3625

3726
PY3 = sys.version_info[0] >= 3
@@ -531,23 +520,6 @@ def is_time(instance):
531520
return False
532521

533522

534-
@FormatChecker.cls_checks("uri")
535-
def is_uri(instance):
536-
# URI regex from http://snipplr.com/view/6889/
537-
abs_uri = (r"^([A-Za-z0-9+.-]+):(?://(?:((?:[A-Za-z0-9-._~!$&'()*+,;=:"
538-
r"]|%[0-9A-Fa-f]{2})*)@)?((?:[A-Za-z0-9-._~!$&'()*+,;=]|%[0"
539-
r"-9A-Fa-f]{2})*)(?::(\d*))?(/(?:[A-Za-z0-9-._~!$&'()*+,;=:"
540-
r"@/]|%[0-9A-Fa-f]{2})*)?|(/?(?:[A-Za-z0-9-._~!$&'()*+,;=:@"
541-
r"]|%[0-9A-Fa-f]{2})+(?:[A-Za-z0-9-._~!$&'()*+,;=:@/]|%[0-9"
542-
r"A-Fa-f]{2})*)?)(?:\?((?:[A-Za-z0-9-._~!$&'()*+,;=:/?@]|%["
543-
r"0-9A-Fa-f]{2})*))?(?:#((?:[A-Za-z0-9-._~!$&'()*+,;=:/?@]|"
544-
r"%[0-9A-Fa-f]{2})*))?$")
545-
if re.match(abs_uri, instance):
546-
return True
547-
rel_uri = r"^(?:#((?:[A-Za-z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-Fa-f]{2})*))?$"
548-
return bool(re.match(rel_uri, instance))
549-
550-
551523
@FormatChecker.cls_checks("email")
552524
def is_email(instance):
553525
return "@" in instance
@@ -593,7 +565,25 @@ def is_regex(instance):
593565
return False
594566

595567

596-
if isodate is not None:
568+
try:
569+
import rfc3987
570+
except ImportError:
571+
pass
572+
else:
573+
@FormatChecker.cls_checks("uri")
574+
def is_uri(instance):
575+
try:
576+
rfc3987.parse(instance, rule="URI_reference")
577+
except ValueError:
578+
return False
579+
return True
580+
581+
582+
try:
583+
import isodate
584+
except ImportError:
585+
pass
586+
else:
597587
@FormatChecker.cls_checks("date-time")
598588
def is_date_time(instance):
599589
try:
@@ -603,7 +593,11 @@ def is_date_time(instance):
603593
return False
604594

605595

606-
if webcolors is not None:
596+
try:
597+
import webcolors
598+
except ImportError:
599+
pass
600+
else:
607601
def is_css_color_code(instance):
608602
try:
609603
webcolors.normalize_hex(instance)

0 commit comments

Comments
 (0)