diff options
author | Stephen Rosen <sirosen@globus.org> | 2021-12-13 21:22:05 +0000 |
---|---|---|
committer | Stephen Rosen <sirosen@globus.org> | 2021-12-13 21:22:05 +0000 |
commit | 94dea8b44713f0c3d7b5d2c53941e1d56b3dc34a (patch) | |
tree | 2a4be1c5de530e37e1aa7ddc0919a4d37af1b0b3 | |
parent | 642a09f08318605b16563f47073d3e7b73025029 (diff) | |
download | jsonschema-94dea8b44713f0c3d7b5d2c53941e1d56b3dc34a.tar.gz |
Fix references to IValidator; add protocol tests
Several remaining cases referred to `IValidator` which is now just
`Validtator`. Fix these.
Add a test which ensures that all valdiators pass
`isinstance(x, Validator)` using the runtime-checkable protocol.
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | docs/spelling-wordlist.txt | 2 | ||||
-rw-r--r-- | docs/validate.rst | 8 | ||||
-rw-r--r-- | jsonschema/__init__.py | 1 | ||||
-rw-r--r-- | jsonschema/_types.py | 2 | ||||
-rw-r--r-- | jsonschema/tests/test_validators.py | 23 | ||||
-rw-r--r-- | jsonschema/validators.py | 4 |
7 files changed, 32 insertions, 10 deletions
@@ -74,7 +74,7 @@ Features and `Draft 3 <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.Draft3Validator>`_ -* `Lazy validation <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.IValidator.iter_errors>`_ +* `Lazy validation <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.Validator.iter_errors>`_ that can iteratively report *all* validation errors. * `Programmatic querying <https://python-jsonschema.readthedocs.io/en/latest/errors/>`_ diff --git a/docs/spelling-wordlist.txt b/docs/spelling-wordlist.txt index 7a6627b..36805a1 100644 --- a/docs/spelling-wordlist.txt +++ b/docs/spelling-wordlist.txt @@ -1,5 +1,5 @@ # this appears to be misinterpreting Napoleon types as prose, sigh... -IValidator +Validator TypeChecker UnknownType ValidationError diff --git a/docs/validate.rst b/docs/validate.rst index 576b0c5..6be8f9c 100644 --- a/docs/validate.rst +++ b/docs/validate.rst @@ -38,7 +38,7 @@ more information see `creating-validators`. Type Checking ------------- -To handle JSON Schema's :validator:`type` property, a `IValidator` uses +To handle JSON Schema's :validator:`type` property, a `Validator` uses an associated `TypeChecker`. The type checker provides an immutable mapping between names of types and functions that can test if an instance is of that type. The defaults are suitable for most users - each of the @@ -82,7 +82,7 @@ given how common validating these types are. If you *do* want the generality, or just want to add a few specific additional types as being acceptable for a validator object, then you should update an existing `TypeChecker` or create a new one. You may then create a new -`IValidator` via `jsonschema.validators.extend`. +`Validator` via `jsonschema.validators.extend`. .. code-block:: python @@ -110,7 +110,7 @@ Versioned Validators `jsonschema` ships with validator classes for various versions of the JSON Schema specification. For details on the methods and attributes -that each validator class provides see the `IValidator` interface, +that each validator class provides see the `Validator` interface, which each included validator class implements. .. autoclass:: Draft202012Validator @@ -155,7 +155,7 @@ JSON Schema defines the :validator:`format` property which can be used to check if primitive types (``string``\s, ``number``\s, ``boolean``\s) conform to well-defined formats. By default, no validation is enforced, but optionally, validation can be enabled by hooking in a format-checking object into an -`IValidator`. +`Validator`. .. doctest:: diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py index c7fa76b..cd2102a 100644 --- a/jsonschema/__init__.py +++ b/jsonschema/__init__.py @@ -26,6 +26,7 @@ from jsonschema.exceptions import ( SchemaError, ValidationError, ) +from jsonschema.protocols import Validator from jsonschema.validators import ( Draft3Validator, Draft4Validator, diff --git a/jsonschema/_types.py b/jsonschema/_types.py index 6ba2bad..f24e433 100644 --- a/jsonschema/_types.py +++ b/jsonschema/_types.py @@ -49,7 +49,7 @@ class TypeChecker(object): """ A ``type`` property checker. - A `TypeChecker` performs type checking for an `IValidator`. Type + A `TypeChecker` performs type checking for a `Validator`. Type checks to perform are updated using `TypeChecker.redefine` or `TypeChecker.redefine_many` and removed via `TypeChecker.remove`. Each of these return a new `TypeChecker` object. diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 429f868..9e52ce9 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -13,7 +13,13 @@ import warnings import attr -from jsonschema import FormatChecker, TypeChecker, exceptions, validators +from jsonschema import ( + FormatChecker, + TypeChecker, + exceptions, + protocols, + validators, +) from jsonschema.tests._helpers import bug @@ -2128,6 +2134,21 @@ class TestRefResolver(TestCase): self.assertIn("Failed to pop the scope", str(exc.exception)) +class TestValidatorProtocol(TestCase): + def test_each_validator_is_instance_of_protocol(self): + schema = {} + for validator_cls in [ + validators.Draft3Validator, + validators.Draft4Validator, + validators.Draft6Validator, + validators.Draft7Validator, + validators.Draft201909Validator, + validators.Draft202012Validator, + ]: + validator = validator_cls(schema) + assert isinstance(validator, protocols.Validator) + + def sorted_errors(errors): def key(error): return ( diff --git a/jsonschema/validators.py b/jsonschema/validators.py index c229efc..e037c4b 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -917,7 +917,7 @@ def validate(instance, schema, cls=None, *args, **kwargs): If you know you have a valid schema already, especially if you intend to validate multiple instances with the same schema, you - likely would prefer using the `IValidator.validate` method directly + likely would prefer using the `Validator.validate` method directly on a specific validator (e.g. ``Draft7Validator.validate``). @@ -931,7 +931,7 @@ def validate(instance, schema, cls=None, *args, **kwargs): The schema to validate with - cls (IValidator): + cls (Validator): The class that will be used to validate the instance. |