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
{{ message }}
Repository navigation
Commit cdf4315
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/validate.rst
+146Lines changed: 146 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -299,6 +299,15 @@ The JSON Schema specification `recommends (but does not require) <https://json-s
299
299
300
300
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`.
301
301
302
+
.. note::
303
+
304
+
The ``regex`` format checker currently uses Python's :mod:`re` module
305
+
regardless of any custom `regex provider <regex-providers>` that may be
306
+
in use. A pattern accepted by Python's :mod:`re` but rejected by a
307
+
custom engine — such as a backreference when using RE2 — will pass the
308
+
format check but fail at match time. See :ref:`regex-providers` for
309
+
details.
310
+
302
311
email
303
312
^^^^^
304
313
@@ -307,3 +316,140 @@ Since in most cases "validating" an email address is an attempt instead to confi
307
316
The same applies to the ``idn-email`` format.
308
317
309
318
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.
319
+
320
+
321
+
.. _regex-providers:
322
+
323
+
Regex Providers
324
+
---------------
325
+
326
+
``jsonschema`` uses regular expressions when validating schemas.
327
+
By default, these operations use Python's built-in :mod:`re` module.
328
+
329
+
Python's :mod:`re` engine is vulnerable to catastrophic (or "pathological") backtracking,
330
+
which occurs when a regular expression and an input
331
+
cause the :mod:`re` module to run for exponentially long periods of time.
332
+
If you are validating user-submitted input against user-supplied schemas,
333
+
you may want to use a regular expression engine that isn't vulnerable.
334
+
335
+
336
+
The ``RegexProvider`` Protocol
337
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338
+
339
+
A regex provider is any class that satisfies the
340
+
`jsonschema.protocols.RegexProvider` protocol.
341
+
342
+
Two methods are required, as well as a class attribute named ``raises``:
343
+
344
+
``raises: tuple[type[Exception], ...]``
345
+
A tuple of exception classes that might be raised
346
+
when interacting with the regex provider.
347
+
348
+
``compile(pattern: str)``
349
+
Compile ``pattern`` and return the result.
350
+
351
+
This is expected to be equivalent to the behavior of
352
+
the builtin ``re.compile()`` function.
353
+
354
+
``search(pattern, text: str)``
355
+
Search for ``text`` in the ``pattern``.
356
+
357
+
``pattern`` may be a string, or the result returned by ``compile()``, above.
358
+
359
+
This is expected to be equivalent to the behavior of
360
+
the builtin ``re.search()`` function.
361
+
362
+
Depending on your use case and the features or drawbacks of the regex engine you're using,
363
+
it may be beneficial to use caching.
364
+
365
+
366
+
Installing a Provider at the Class Level
367
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368
+
369
+
Pass a regex provider instance to `jsonschema.validators.extend`
370
+
to derive a validator class that uses your regex provider:
371
+
372
+
.. code-block:: python
373
+
374
+
from jsonschema import validators
375
+
376
+
SafeValidator = validators.extend(
377
+
validators.Draft202012Validator,
378
+
regex_provider=MyProvider(),
379
+
)
380
+
381
+
All instances of ``SafeValidator`` will use ``MyProvider`` by default.
382
+
The same argument is accepted by `jsonschema.validators.create`
383
+
when building a validator class from scratch.
384
+
385
+
386
+
Per-Instance Override
387
+
~~~~~~~~~~~~~~~~~~~~~
388
+
389
+
Pass a provider instance to the validator constructor
390
+
to override the default for a single validator instance:
0 commit comments