Skip to content

Commit 8046232

Browse files
committed
Make it clearer that format: regex is Python regexes in this implementation.
The same applies to pattern, patternProperties, etc.
1 parent ee024ff commit 8046232

File tree

2 files changed

+63
-61
lines changed

2 files changed

+63
-61
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ They can be used when installing in order to include additional dependencies, e.
105105
106106
$ pip install jsonschema'[format]'
107107
108+
You will also want to read the `format validation documentation <https://python-jsonschema.readthedocs.io/en/latest/validate/#validating-formats>`_.
109+
108110
.. start cut from PyPI
109111
110112
Running the Test Suite

docs/validate.rst

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The simplest way to validate an instance under a given schema is to use the
2424
.. _validator-protocol:
2525

2626
The Validator Protocol
27-
-----------------------
27+
----------------------
2828

2929
`jsonschema` defines a `protocol <typing.Protocol>` that all validator classes adhere to.
3030

@@ -173,7 +173,7 @@ Validating Formats
173173
------------------
174174

175175
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`.
177177

178178
.. doctest::
179179

@@ -187,70 +187,31 @@ By default, no validation is enforced, but optionally, validation can be enabled
187187
...
188188
ValidationError: "-12" is not a "ipv4"
189189

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
209-
`jsonschema.exceptions.ValidationError.cause` attribute
210-
of the resulting validation error.
211190

212-
.. deprecated:: v4.14.0
191+
Some formats require additional dependencies to be installed.
213192

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.
215194

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:
228196

229197
.. code-block:: sh
230198
231-
$ pip install jsonschema[format]
232-
233-
which will install all of the below dependencies for all formats.
199+
$ pip install jsonschema[format]
234200
235201
Or if you want to install MIT-license compatible dependencies only:
236202

237203
.. code-block:: sh
238204
239-
$ pip install jsonschema[format_nongpl]
205+
$ pip install jsonschema[format-nongpl]
240206
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``.
245209

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.
248211

249-
.. note::
212+
.. warning::
250213

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.
254215

255216
========================= ====================
256217
Checker Notes
@@ -283,18 +244,57 @@ Checker Notes
283244
.. _rfc3339-validator: https://pypi.org/project/rfc3339-validator/
284245
.. _rfc3986-validator: https://pypi.org/project/rfc3986-validator/
285246
.. _rfc3987: https://pypi.org/pypi/rfc3987/
286-
.. _rfc5322: https://tools.ietf.org/html/rfc5322#section-3.4.1
287247
.. _uri-template: https://pypi.org/pypi/uri-template/
288248
.. _webcolors: https://pypi.org/pypi/webcolors/
289249

290-
.. note::
250+
The supported mechanism for ensuring these dependencies are present is again as shown above, not by directly installing the packages.
251+
252+
.. autoclass:: FormatChecker
253+
:members:
254+
:exclude-members: cls_checks
255+
256+
.. attribute:: checkers
257+
258+
A mapping of currently known formats to tuple of functions that validate them and errors that should be caught.
259+
New checkers can be added and removed either per-instance or globally for all checkers using the `FormatChecker.checks` decorator.
260+
261+
.. classmethod:: cls_checks(format, raises=())
262+
263+
Register a decorated function as *globally* validating a new format.
264+
265+
Any instance created after this function is called will pick up the supplied checker.
266+
267+
:argument str format: the format that the decorated function will check
268+
:argument Exception raises: the exception(s) raised
269+
by the decorated function when an invalid instance is
270+
found. The exception object will be accessible as the
271+
`jsonschema.exceptions.ValidationError.cause` attribute
272+
of the resulting validation error.
273+
274+
.. deprecated:: v4.14.0
275+
276+
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.
291297

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.
299299

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

Comments
 (0)