You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/validate.rst
+61-61Lines changed: 61 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ The simplest way to validate an instance under a given schema is to use the
24
24
.. _validator-protocol:
25
25
26
26
The Validator Protocol
27
-
-----------------------
27
+
----------------------
28
28
29
29
`jsonschema` defines a `protocol <typing.Protocol>` that all validator classes adhere to.
30
30
@@ -173,7 +173,7 @@ Validating Formats
173
173
------------------
174
174
175
175
JSON Schema defines the :kw:`format` keyword which can be used to check if primitive types (``string``\s, ``number``\s, ``boolean``\s) conform to well-defined formats.
176
-
By default, no validation is enforced, but optionally, validation can be enabled by hooking in a format-checking object into a `Validator`.
176
+
By default, as per the specification, no validation is enforced. Optionally however, validation can be enabled by hooking a `format-checking object <jsonschema.FormatChecker>` into a `Validator`.
177
177
178
178
.. doctest::
179
179
@@ -187,70 +187,31 @@ By default, no validation is enforced, but optionally, validation can be enabled
187
187
...
188
188
ValidationError: "-12" is not a "ipv4"
189
189
190
-
.. autoclass:: FormatChecker
191
-
:members:
192
-
:exclude-members: cls_checks
193
-
194
-
.. attribute:: checkers
195
-
196
-
A mapping of currently known formats to tuple of functions that validate them and errors that should be caught.
197
-
New checkers can be added and removed either per-instance or globally for all checkers using the `FormatChecker.checks` decorator.
198
-
199
-
.. classmethod:: cls_checks(format, raises=())
200
-
201
-
Register a decorated function as *globally* validating a new format.
202
-
203
-
Any instance created after this function is called will pick up the supplied checker.
204
-
205
-
:argument str format: the format that the decorated function will check
206
-
:argument Exception raises: the exception(s) raised
207
-
by the decorated function when an invalid instance is
208
-
found. The exception object will be accessible as the
Some formats require additional dependencies to be installed.
213
192
214
-
Use `FormatChecker.checks` on an instance instead.
193
+
The easiest way to ensure you have what is needed is to install ``jsonschema`` using the ``format`` or ``format-nongpl`` extras.
215
194
216
-
217
-
.. autoexception:: FormatError
218
-
:noindex:
219
-
:members:
220
-
221
-
222
-
There are a number of default checkers that `FormatChecker`\s know how
223
-
to validate. Their names can be viewed by inspecting the
224
-
`FormatChecker.checkers` attribute. Certain checkers will only be
225
-
available if an appropriate package is available for use. The easiest way to
226
-
ensure you have what is needed is to install ``jsonschema`` using the
227
-
``format`` or ``format_nongpl`` collection of optional dependencies -- e.g.
195
+
For example:
228
196
229
197
.. code-block:: sh
230
198
231
-
$ pip install jsonschema[format]
232
-
233
-
which will install all of the below dependencies for all formats.
199
+
$ pip install jsonschema[format]
234
200
235
201
Or if you want to install MIT-license compatible dependencies only:
236
202
237
203
.. code-block:: sh
238
204
239
-
$ pip install jsonschema[format_nongpl]
205
+
$ pip install jsonschema[format-nongpl]
240
206
241
-
The non-GPL extra is intended to not install any direct dependencies
242
-
that are GPL (but that of course end-users should do their own verification).
243
-
At the moment, it supports all the available checkers except for ``iri`` and
244
-
``iri-reference``.
207
+
The non-GPL extra is intended to not install any direct dependencies that are GPL licensed (but that of course end-users should do their own verification).
208
+
At the moment, it supports all the available checkers except for ``iri`` and ``iri-reference``.
245
209
246
-
The more specific list of available checkers, along with their requirement
247
-
(if any,) are listed below.
210
+
The more specific list of formats along with any additional dependencies they have is shown below.
248
211
249
-
.. note::
212
+
.. warning::
250
213
251
-
If the following packages are not installed when using a checker
252
-
that requires it, validation will succeed without throwing an error,
253
-
as specified by the JSON Schema specification.
214
+
If a dependency is not installed when using a checker that requires it, validation will succeed without throwing an error, as also specified by the specification.
Use `FormatChecker.checks` on an instance instead.
277
+
278
+
.. autoexception:: FormatError
279
+
:noindex:
280
+
:members:
281
+
282
+
283
+
Format-Specific Notes
284
+
~~~~~~~~~~~~~~~~~~~~~
285
+
286
+
regex
287
+
^^^^^
288
+
289
+
The JSON Schema specification `recommends (but does not require) <https://json-schema.org/draft/2020-12/json-schema-core.html#name-regular-expressions>`_ that implementations use ECMA 262 regular expressions.
290
+
291
+
Given that there is no current library in Python capable of supporting the ECMA 262 dialect, the ``regex`` format will instead validate *Python* regular expressions, which are the ones used by this implementation for other keywords like :kw:`pattern` or :kw:`patternProperties`.
292
+
293
+
email
294
+
^^^^^
295
+
296
+
Since in most cases "validating" an email address is an attempt instead to confirm that mail sent to it will deliver to a recipient, and that that recipient is the correct one the email is intended for, and since many valid email addresses are in many places incorrectly rejected, and many invalid email addresses are in many places incorrectly accepted, the ``email`` format keyword only provides a sanity check, not full :RFC:`5322` validation.
291
297
292
-
Since in most cases "validating" an email address is an attempt
293
-
instead to confirm that mail sent to it will deliver to a recipient,
294
-
and that that recipient is the correct one the email is intended
295
-
for, and since many valid email addresses are in many places
296
-
incorrectly rejected, and many invalid email addresses are in many
297
-
places incorrectly accepted, the ``email`` format keyword only
298
-
provides a sanity check, not full rfc5322_ validation.
298
+
The same applies to the ``idn-email`` format.
299
299
300
-
The same applies to the ``idn-email`` format.
300
+
If you indeed want a particular well-specified set of emails to be considered valid, you can use `FormatChecker.checks` to provide your specific definition.
0 commit comments