Skip to content

Commit 697c9d1

Browse files
committed
Move all format docs to the docs.
1 parent 308d14a commit 697c9d1

File tree

2 files changed

+95
-137
lines changed

2 files changed

+95
-137
lines changed

docs/validate.rst

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,108 @@ any use for them. They are listed below, along with any limitations they come
235235
with.
236236

237237

238-
.. autofunction:: is_date
238+
.. function:: is_date
239239

240-
.. autofunction:: is_time
240+
Check if the instance is a date in ``YYYY-MM-DD`` format.
241241

242-
.. autofunction:: is_regex
242+
>>> is_date("1970-12-31")
243+
True
244+
>>> is_date("12/31/1970")
245+
False
246+
>>> is_date("0000-13-32")
247+
False
248+
249+
.. function:: is_time
250+
251+
Check if the instance is a time in ``hh:mm:ss`` format.
252+
253+
>>> is_time("23:59:59")
254+
True
255+
>>> is_time("11:59:59 PM")
256+
False
257+
>>> is_time("59:60:61")
258+
False
259+
260+
.. function:: is_regex
261+
262+
Check if the instance is a well-formed regular expression.
263+
264+
>>> is_regex("^(bob)?cat$")
265+
True
266+
>>> is_ipv6("^(bob?cat$")
267+
False
268+
269+
.. function:: is_uri
270+
271+
Check if the instance is a valid URI.
272+
273+
Also supports relative URIs.
274+
275+
>>> is_uri("ftp://[email protected]:8080/pub/os/")
276+
True
277+
>>> is_uri("http://www2.example.com:8000/pub/#os?user=joe.bloggs")
278+
True
279+
>>> is_uri(r"\\\\WINDOWS\My Files")
280+
False
281+
>>> is_uri("#/properties/foo")
282+
True
283+
284+
.. function:: is_email
243285

244-
.. autofunction:: is_uri
286+
Check if the instance is a valid e-mail address.
245287

246-
.. autofunction:: is_email
288+
Checking is based on `RFC 2822`_
247289

248-
.. autofunction:: is_ip_address
290+
>>> is_email("[email protected]")
291+
True
292+
>>> is_email("joe.bloggs")
293+
False
294+
295+
.. _RFC 2822: http://tools.ietf.org/html/rfc2822
296+
297+
.. function:: is_host_name
298+
299+
Check if the instance is a valid host name.
300+
301+
>>> is_host_name("www.example.com")
302+
True
303+
>>> is_host_name("my laptop")
304+
False
305+
>>> is_host_name(
306+
... "a.vvvvvvvvvvvvvvvvveeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrryyyyyyyy"
307+
... "yyyyyyyyy.long.host.name"
308+
... )
309+
False
310+
311+
.. note:: Does not perform a DNS lookup.
312+
313+
>>> is_host_name("www.example.doesnotexist")
314+
True
249315

250-
.. autofunction:: is_ipv6
316+
.. function:: is_ip_address
251317

252-
.. autofunction:: is_host_name
318+
Check if the instance is a valid IP address.
319+
320+
>>> is_ip_address("192.168.0.1")
321+
True
322+
>>> is_ip_address("::1")
323+
False
324+
>>> is_ip_address("256.256.256.256")
325+
False
326+
327+
On OSes with the ``socket.inet_pton`` function, an additional checker for
328+
``ipv6`` will be enabled:
329+
330+
.. function:: is_ipv6
331+
332+
Check if the instance is a valid IPv6 address.
333+
334+
>>> is_ipv6("::1")
335+
True
336+
>>> is_ipv6("192.168.0.1")
337+
False
338+
>>> is_ipv6("1:1:1:1:1:1:1:1:1")
339+
False
253340

254341

255342
If the iso8601_ library is present, a date-time checker will also be present.

jsonschema.py

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -555,21 +555,6 @@ def conforms(self, instance, format):
555555

556556
@FormatChecker.cls_checks("date")
557557
def is_date(instance):
558-
"""
559-
Check whether the instance matches a date in ``YYYY-MM-DD`` format.
560-
561-
:argument str instance: the instance to check
562-
:rtype: bool
563-
564-
>>> is_date("1970-12-31")
565-
True
566-
>>> is_date("12/31/1970")
567-
False
568-
>>> is_date("0000-13-32")
569-
False
570-
571-
"""
572-
573558
try:
574559
datetime.datetime.strptime(instance, "%Y-%m-%d")
575560
return True
@@ -579,21 +564,6 @@ def is_date(instance):
579564

580565
@FormatChecker.cls_checks("time")
581566
def is_time(instance):
582-
"""
583-
Check whether the instance matches a time in ``hh:mm:ss`` format.
584-
585-
:argument str instance: the instance to check
586-
:rtype: bool
587-
588-
>>> is_time("23:59:59")
589-
True
590-
>>> is_time("11:59:59 PM")
591-
False
592-
>>> is_time("59:60:61")
593-
False
594-
595-
"""
596-
597567
try:
598568
datetime.datetime.strptime(instance, "%H:%M:%S")
599569
return True
@@ -603,25 +573,6 @@ def is_time(instance):
603573

604574
@FormatChecker.cls_checks("uri")
605575
def is_uri(instance):
606-
"""
607-
Check whether the instance is a valid URI.
608-
609-
Also supports relative URIs.
610-
611-
:argument str instance: the instance to check
612-
:rtype: bool
613-
614-
>>> is_uri("ftp://[email protected]:8080/pub/os/")
615-
True
616-
>>> is_uri("http://www2.example.com:8000/pub/#os?user=joe.bloggs")
617-
True
618-
>>> is_uri(r"\\\\WINDOWS\My Files")
619-
False
620-
>>> is_uri("#/properties/foo")
621-
True
622-
623-
"""
624-
625576
# URI regex from http://snipplr.com/view/6889/
626577
abs_uri = (r"^([A-Za-z0-9+.-]+):(?://(?:((?:[A-Za-z0-9-._~!$&'()*+,;=:"
627578
r"]|%[0-9A-Fa-f]{2})*)@)?((?:[A-Za-z0-9-._~!$&'()*+,;=]|%[0"
@@ -639,23 +590,6 @@ def is_uri(instance):
639590

640591
@FormatChecker.cls_checks("email")
641592
def is_email(instance):
642-
"""
643-
Check whether the instance is a valid e-mail address.
644-
645-
Checking is based on `RFC 2822`_
646-
647-
:argument str instance: the instance to check
648-
:rtype: bool
649-
650-
>>> is_email("[email protected]")
651-
True
652-
>>> is_email("joe.bloggs")
653-
False
654-
655-
.. _RFC 2822: http://tools.ietf.org/html/rfc2822
656-
657-
"""
658-
659593
pattern = (r"^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_"
660594
r"`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b"
661595
r"\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z"
@@ -669,21 +603,6 @@ def is_email(instance):
669603

670604
@FormatChecker.cls_checks("ip-address")
671605
def is_ip_address(instance):
672-
"""
673-
Check whether the instance is a valid IP address.
674-
675-
:argument str instance: the instance to check
676-
:rtype: bool
677-
678-
>>> is_ip_address("192.168.0.1")
679-
True
680-
>>> is_ip_address("::1")
681-
False
682-
>>> is_ip_address("256.256.256.256")
683-
False
684-
685-
"""
686-
687606
try:
688607
socket.inet_aton(instance)
689608
return True
@@ -694,21 +613,6 @@ def is_ip_address(instance):
694613
if hasattr(socket, "inet_pton"):
695614
@FormatChecker.cls_checks("ipv6")
696615
def is_ipv6(instance):
697-
"""
698-
Check whether the instance is a valid IPv6 address.
699-
700-
:argument str instance: the instance to check
701-
:rtype: bool
702-
703-
>>> is_ipv6("::1")
704-
True
705-
>>> is_ipv6("192.168.0.1")
706-
False
707-
>>> is_ipv6("1:1:1:1:1:1:1:1:1")
708-
False
709-
710-
"""
711-
712616
try:
713617
socket.inet_pton(socket.AF_INET6, instance)
714618
return True
@@ -718,26 +622,6 @@ def is_ipv6(instance):
718622

719623
@FormatChecker.cls_checks("host-name")
720624
def is_host_name(instance):
721-
"""
722-
Check if the instance is a valid host name.
723-
724-
>>> is_host_name("www.example.com")
725-
True
726-
>>> is_host_name("my laptop")
727-
False
728-
>>> is_host_name(
729-
... "a.vvvvvvvvvvvvvvvvveeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrryyyyyyyy"
730-
... "yyyyyyyyy.long.host.name"
731-
... )
732-
False
733-
734-
.. note:: Does not perform a DNS lookup.
735-
736-
>>> is_host_name("www.example.doesnotexist")
737-
True
738-
739-
"""
740-
741625
pattern = "^[A-Za-z0-9][A-Za-z0-9\.\-]{1,255}$"
742626
if not re.match(pattern, instance):
743627
return False
@@ -750,19 +634,6 @@ def is_host_name(instance):
750634

751635
@FormatChecker.cls_checks("regex")
752636
def is_regex(instance):
753-
"""
754-
Check if the instance is a well-formed regular expression.
755-
756-
:argument str instance: the instance to check
757-
:rtype: bool
758-
759-
>>> is_regex("^(bob)?cat$")
760-
True
761-
>>> is_ipv6("^(bob?cat$")
762-
False
763-
764-
"""
765-
766637
try:
767638
re.compile(instance)
768639
return True

0 commit comments

Comments
 (0)