@@ -19,160 +19,26 @@ The simplest way to validate an instance under a given schema is to use the
19
19
fundamentals underway at `Understanding JSON Schema
20
20
<https://json-schema.org/understanding-json-schema/> `_
21
21
22
+ .. _validator-protocol :
22
23
23
- The Validator Interface
24
+ The Validator Protocol
24
25
-----------------------
25
26
26
- `jsonschema ` defines an (informal) interface that all validator
27
+ `jsonschema ` defines a protocol that all validator
27
28
classes should adhere to.
28
29
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:
166
32
167
33
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
169
35
that extend or complement the ones included should adhere to it as well. For
170
36
more information see `creating-validators `.
171
37
172
38
Type Checking
173
39
-------------
174
40
175
- To handle JSON Schema's :validator: `type ` property, a `IValidator ` uses
41
+ To handle JSON Schema's :validator: `type ` property, a `Validator ` uses
176
42
an associated `TypeChecker `. The type checker provides an immutable
177
43
mapping between names of types and functions that can test if an instance is
178
44
of that type. The defaults are suitable for most users - each of the
@@ -216,7 +82,7 @@ given how common validating these types are.
216
82
If you *do * want the generality, or just want to add a few specific additional
217
83
types as being acceptable for a validator object, then you should update an
218
84
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 `.
220
86
221
87
.. code-block :: python
222
88
@@ -244,7 +110,7 @@ Versioned Validators
244
110
245
111
`jsonschema ` ships with validator classes for various versions of
246
112
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,
248
114
which each included validator class implements.
249
115
250
116
.. autoclass :: Draft202012Validator
@@ -289,7 +155,7 @@ JSON Schema defines the :validator:`format` property which can be used to check
289
155
if primitive types (``string ``\s , ``number ``\s , ``boolean ``\s ) conform to
290
156
well-defined formats. By default, no validation is enforced, but optionally,
291
157
validation can be enabled by hooking in a format-checking object into an
292
- `IValidator `.
158
+ `Validator `.
293
159
294
160
.. doctest ::
295
161
0 commit comments