Skip to content

Commit 4828032

Browse files
authored
Merge pull request #890 from sirosen/add-validator-protocol
Add `jsonschema.protocols.IValidator`
2 parents ac7b838 + 3a62925 commit 4828032

File tree

12 files changed

+209
-157
lines changed

12 files changed

+209
-157
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Features
7474
and
7575
`Draft 3 <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.Draft3Validator>`_
7676

77-
* `Lazy validation <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.IValidator.iter_errors>`_
77+
* `Lazy validation <https://python-jsonschema.readthedocs.io/en/latest/validate/#jsonschema.protocols.Validator.iter_errors>`_
7878
that can iteratively report *all* validation errors.
7979

8080
* `Programmatic querying <https://python-jsonschema.readthedocs.io/en/latest/errors/>`_

docs/creating.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ Any validating function that validates against a subschema should call
2323
instance, or schema, it should pass one or both of the ``path`` or
2424
``schema_path`` arguments to ``descend`` in order to properly maintain
2525
where in the instance or schema respectively the error occurred.
26+
27+
The Validator Protocol
28+
----------------------
29+
30+
``jsonschema`` defines a `protocol <typing.Protocol>`,
31+
`jsonschema.protocols.Validator` which can be used in type annotations to
32+
describe the type of a validator object.
33+
34+
For full details, see `validator-protocol`.

docs/errors.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ error objects.
275275
As you can see, `jsonschema.exceptions.ErrorTree` takes an
276276
iterable of `ValidationError`\s when constructing a tree so
277277
you can directly pass it the return value of a validator object's
278-
`jsonschema.IValidator.iter_errors` method.
278+
`jsonschema.protocols.Validator.iter_errors` method.
279279

280280
`ErrorTree`\s support a number of useful operations. The first one we
281281
might want to perform is to check whether a given element in our instance

docs/faq.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ but fail the second!
9090

9191
Still, filling in defaults is a thing that is useful. `jsonschema`
9292
allows you to `define your own validator classes and callables
93-
<creating>`, so you can easily create an `jsonschema.IValidator` that
94-
does do default setting. Here's some code to get you started. (In
93+
<creating>`, so you can easily create an `jsonschema.protocols.Validator`
94+
that does do default setting. Here's some code to get you started. (In
9595
this code, we add the default properties to each object *before* the
9696
properties are validated, so the default values themselves will need to
9797
be valid under the schema.)

docs/spelling-wordlist.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# this appears to be misinterpreting Napoleon types as prose, sigh...
2-
IValidator
2+
Validator
33
TypeChecker
44
UnknownType
55
ValidationError

docs/validate.rst

Lines changed: 10 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -19,160 +19,26 @@ The simplest way to validate an instance under a given schema is to use the
1919
fundamentals underway at `Understanding JSON Schema
2020
<https://json-schema.org/understanding-json-schema/>`_
2121
22+
.. _validator-protocol:
2223

23-
The Validator Interface
24+
The Validator Protocol
2425
-----------------------
2526

26-
`jsonschema` defines an (informal) interface that all validator
27+
`jsonschema` defines a protocol that all validator
2728
classes should adhere to.
2829

29-
.. class:: IValidator(schema, types=(), resolver=None, format_checker=None)
30-
31-
:argument dict schema: the schema that the validator object
32-
will validate with. It is assumed to be valid, and providing
33-
an invalid schema can lead to undefined behavior. See
34-
`IValidator.check_schema` to validate a schema first.
35-
:argument resolver: an instance of `RefResolver` that will be
36-
used to resolve :validator:`$ref` properties (JSON references). If
37-
unprovided, one will be created.
38-
:argument format_checker: an instance of `FormatChecker`
39-
whose `FormatChecker.conforms` method will be called to
40-
check and see if instances conform to each :validator:`format`
41-
property present in the schema. If unprovided, no validation
42-
will be done for :validator:`format`. Certain formats require
43-
additional packages to be installed (ipv5, uri, color, date-time).
44-
The required packages can be found at the bottom of this page.
45-
:argument types:
46-
.. deprecated:: 3.0.0
47-
48-
Use `TypeChecker.redefine` and
49-
`jsonschema.validators.extend` instead of this argument.
50-
51-
See `validating-types` for details.
52-
53-
If used, this overrides or extends the list of known types when
54-
validating the :validator:`type` property.
55-
56-
What is provided should map strings (type names) to class objects
57-
that will be checked via `isinstance`.
58-
59-
60-
.. attribute:: META_SCHEMA
61-
62-
An object representing the validator's meta schema (the schema that
63-
describes valid schemas in the given version).
64-
65-
.. attribute:: VALIDATORS
66-
67-
A mapping of validator names (`str`\s) to functions
68-
that validate the validator property with that name. For more
69-
information see `creating-validators`.
70-
71-
.. attribute:: TYPE_CHECKER
72-
73-
A `TypeChecker` that will be used when validating :validator:`type`
74-
properties in JSON schemas.
75-
76-
.. attribute:: schema
77-
78-
The schema that was passed in when initializing the object.
79-
80-
.. attribute:: DEFAULT_TYPES
81-
82-
.. deprecated:: 3.0.0
83-
84-
Use of this attribute is deprecated in favor of the new `type
85-
checkers <TypeChecker>`.
86-
87-
See `validating-types` for details.
88-
89-
For backwards compatibility on existing validator classes, a mapping of
90-
JSON types to Python class objects which define the Python types for
91-
each JSON type.
92-
93-
Any existing code using this attribute should likely transition to
94-
using `TypeChecker.is_type`.
95-
96-
97-
.. classmethod:: check_schema(schema)
98-
99-
Validate the given schema against the validator's `META_SCHEMA`.
100-
101-
:raises: `jsonschema.exceptions.SchemaError` if the schema
102-
is invalid
103-
104-
.. method:: is_type(instance, type)
105-
106-
Check if the instance is of the given (JSON Schema) type.
107-
108-
:type type: str
109-
:rtype: bool
110-
:raises: `jsonschema.exceptions.UnknownType` if ``type``
111-
is not a known type.
112-
113-
.. method:: is_valid(instance)
114-
115-
Check if the instance is valid under the current `schema`.
116-
117-
:rtype: bool
118-
119-
>>> schema = {"maxItems" : 2}
120-
>>> Draft3Validator(schema).is_valid([2, 3, 4])
121-
False
122-
123-
.. method:: iter_errors(instance)
124-
125-
Lazily yield each of the validation errors in the given instance.
126-
127-
:rtype: an `collections.abc.Iterable` of
128-
`jsonschema.exceptions.ValidationError`\s
129-
130-
>>> schema = {
131-
... "type" : "array",
132-
... "items" : {"enum" : [1, 2, 3]},
133-
... "maxItems" : 2,
134-
... }
135-
>>> v = Draft3Validator(schema)
136-
>>> for error in sorted(v.iter_errors([2, 3, 4]), key=str):
137-
... print(error.message)
138-
4 is not one of [1, 2, 3]
139-
[2, 3, 4] is too long
140-
141-
.. method:: validate(instance)
142-
143-
Check if the instance is valid under the current `schema`.
144-
145-
:raises: `jsonschema.exceptions.ValidationError` if the
146-
instance is invalid
147-
148-
>>> schema = {"maxItems" : 2}
149-
>>> Draft3Validator(schema).validate([2, 3, 4])
150-
Traceback (most recent call last):
151-
...
152-
ValidationError: [2, 3, 4] is too long
153-
154-
.. method:: evolve(**kwargs)
155-
156-
Create a new validator like this one, but with given changes.
157-
158-
Preserves all other attributes, so can be used to e.g. create a
159-
validator with a different schema but with the same :validator:`$ref`
160-
resolution behavior.
161-
162-
>>> validator = Draft202012Validator({})
163-
>>> validator.evolve(schema={"type": "number"})
164-
Draft202012Validator(schema={'type': 'number'}, format_checker=None)
165-
30+
.. autoclass:: jsonschema.protocols.Validator
31+
:members:
16632

16733
All of the `versioned validators <versioned-validators>` that are included with
168-
`jsonschema` adhere to the interface, and implementers of validator classes
34+
`jsonschema` adhere to the protocol, and implementers of validator classes
16935
that extend or complement the ones included should adhere to it as well. For
17036
more information see `creating-validators`.
17137

17238
Type Checking
17339
-------------
17440

175-
To handle JSON Schema's :validator:`type` property, a `IValidator` uses
41+
To handle JSON Schema's :validator:`type` property, a `Validator` uses
17642
an associated `TypeChecker`. The type checker provides an immutable
17743
mapping between names of types and functions that can test if an instance is
17844
of that type. The defaults are suitable for most users - each of the
@@ -216,7 +82,7 @@ given how common validating these types are.
21682
If you *do* want the generality, or just want to add a few specific additional
21783
types as being acceptable for a validator object, then you should update an
21884
existing `TypeChecker` or create a new one. You may then create a new
219-
`IValidator` via `jsonschema.validators.extend`.
85+
`Validator` via `jsonschema.validators.extend`.
22086

22187
.. code-block:: python
22288
@@ -244,7 +110,7 @@ Versioned Validators
244110

245111
`jsonschema` ships with validator classes for various versions of
246112
the JSON Schema specification. For details on the methods and attributes
247-
that each validator class provides see the `IValidator` interface,
113+
that each validator class provides see the `Validator` interface,
248114
which each included validator class implements.
249115

250116
.. autoclass:: Draft202012Validator
@@ -289,7 +155,7 @@ JSON Schema defines the :validator:`format` property which can be used to check
289155
if primitive types (``string``\s, ``number``\s, ``boolean``\s) conform to
290156
well-defined formats. By default, no validation is enforced, but optionally,
291157
validation can be enabled by hooking in a format-checking object into an
292-
`IValidator`.
158+
`Validator`.
293159

294160
.. doctest::
295161

jsonschema/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
SchemaError,
2727
ValidationError,
2828
)
29+
from jsonschema.protocols import Validator
2930
from jsonschema.validators import (
3031
Draft3Validator,
3132
Draft4Validator,

jsonschema/_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TypeChecker(object):
4949
"""
5050
A ``type`` property checker.
5151
52-
A `TypeChecker` performs type checking for an `IValidator`. Type
52+
A `TypeChecker` performs type checking for a `Validator`. Type
5353
checks to perform are updated using `TypeChecker.redefine` or
5454
`TypeChecker.redefine_many` and removed via `TypeChecker.remove`.
5555
Each of these return a new `TypeChecker` object.

0 commit comments

Comments
 (0)