Skip to content

Commit 513f70b

Browse files
committed
Add check_validity_of to vaildator docs
Document the ability to replace `URIReference.is_valid` without mentioning that method at all.
1 parent e5f8f83 commit 513f70b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

docs/source/api-ref/validators.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
.. automethod:: rfc3986.validators.Validator.allow_use_of_password
1414

15+
.. automethod:: rfc3986.validators.Validator.check_validity_of
16+
1517
.. automethod:: rfc3986.validators.Validator.forbid_use_of_password
1618

1719
.. automethod:: rfc3986.validators.Validator.require_presence_of

docs/source/user/validating.rst

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ tricky. Different parts of the URI allow different characters. Those sets
77
sometimes overlap and othertimes they don't and it's not very convenient.
88
Luckily, |rfc3986| makes validating URIs far simpler.
99

10+
1011
Example Usage
1112
=============
1213

1314
First we need to create an instance of a
1415
:class:`~rfc3986.validators.Validator` which takes no parameters. After that
1516
we can call methods on the instance to indicate what we want to validate.
1617

17-
Let's assume that we're building something that takes user input for a URl and
18+
Allowing Only Trusted Domains and Schemes
19+
-----------------------------------------
20+
21+
Let's assume that we're building something that takes user input for a URL and
1822
we want to ensure that URL is only ever using a specific domain with https. In
1923
that case, our code would look like this:
2024

@@ -52,6 +56,9 @@ As it stands, our validator will allow the first two URLs to pass but will
5256
fail the third. This is specifically because we only allow URLs using
5357
``https`` as a scheme and ``github.com`` as the domain name.
5458

59+
Preventing Leaks of User Credentials
60+
------------------------------------
61+
5562
Next, let's imagine that we want to prevent leaking user credentials. In that
5663
case, we want to ensure that there is no password in the user information
5764
portion of the authority. In that case, our new validator would look like this:
@@ -87,6 +94,9 @@ portion of the authority. In that case, our new validator would look like this:
8794
...
8895
rfc3986.exceptions.PasswordForbidden
8996

97+
Requiring the Presence of Components
98+
------------------------------------
99+
90100
Up until now, we have assumed that we will get a URL that has the appropriate
91101
components for validation. For example, we assume that we will have a URL that
92102
has a scheme and hostname. However, our current validation doesn't require
@@ -134,7 +144,42 @@ item to our validator:
134144
... 'https://github.com/sigmavirus24/rfc3986'
135145
... ))
136146

137-
.. links
138147

148+
Checking the Validity of Components
149+
-----------------------------------
150+
151+
As of version 1.1.0, |rfc3986| allows users to check the validity of a URI
152+
Reference using a :class:`~rfc3986.validators.Validator`. Along with the above
153+
examples we can also check that a URI is valid per :rfc:`3986`. The validation
154+
of the components is pre-determined so all we need to do is specify which
155+
components we want to validate:
156+
157+
.. doctest::
158+
159+
>>> from rfc3986 import validators, uri_reference
160+
>>> valid_uri = uri_reference('https://github.com/')
161+
>>> validator = validators.Validator().allow_schemes(
162+
... 'https',
163+
... ).allow_hosts(
164+
... 'github.com',
165+
... ).forbid_use_of_password(
166+
... ).require_presence_of(
167+
... 'scheme', 'host',
168+
... ).check_validity_of(
169+
... 'scheme', 'host', 'path',
170+
... )
171+
>>> validator.validate(valid_uri)
172+
>>> invalid_uri = valid_uri.copy_with(path='/#invalid/path')
173+
>>> validator.validate(invalid_uri)
174+
Traceback (most recent call last):
175+
...
176+
rfc3986.exceptions.InvalidComponentsError
177+
178+
Paths are not allowed to contain a ``#`` character unless it's
179+
percent-encoded. This is why our ``invalid_uri`` raises an exception when we
180+
attempt to validate it.
181+
182+
183+
.. links
139184
.. _validating an email address:
140185
http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/

0 commit comments

Comments
 (0)