@@ -7,14 +7,18 @@ tricky. Different parts of the URI allow different characters. Those sets
7
7
sometimes overlap and othertimes they don't and it's not very convenient.
8
8
Luckily, |rfc3986 | makes validating URIs far simpler.
9
9
10
+
10
11
Example Usage
11
12
=============
12
13
13
14
First we need to create an instance of a
14
15
:class: `~rfc3986.validators.Validator ` which takes no parameters. After that
15
16
we can call methods on the instance to indicate what we want to validate.
16
17
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
18
22
we want to ensure that URL is only ever using a specific domain with https. In
19
23
that case, our code would look like this:
20
24
@@ -52,6 +56,9 @@ As it stands, our validator will allow the first two URLs to pass but will
52
56
fail the third. This is specifically because we only allow URLs using
53
57
``https `` as a scheme and ``github.com `` as the domain name.
54
58
59
+ Preventing Leaks of User Credentials
60
+ ------------------------------------
61
+
55
62
Next, let's imagine that we want to prevent leaking user credentials. In that
56
63
case, we want to ensure that there is no password in the user information
57
64
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:
87
94
...
88
95
rfc3986.exceptions.PasswordForbidden
89
96
97
+ Requiring the Presence of Components
98
+ ------------------------------------
99
+
90
100
Up until now, we have assumed that we will get a URL that has the appropriate
91
101
components for validation. For example, we assume that we will have a URL that
92
102
has a scheme and hostname. However, our current validation doesn't require
@@ -134,7 +144,42 @@ item to our validator:
134
144
... ' https://github.com/sigmavirus24/rfc3986'
135
145
... ))
136
146
137
- .. links
138
147
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
139
184
.. _validating an email address :
140
185
http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx/
0 commit comments