Skip to content

Commit ff003c4

Browse files
committed
Update doc examples to use newer drafts' validators.
Obviously what was here works fine, but better to show the examples with a more recent spec.
1 parent 60bb7b4 commit ff003c4

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

docs/errors.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ raised or returned, depending on which method or function is used.
3737
.. attribute:: validator
3838

3939
The name of the failed `validator
40-
<https://json-schema.org/draft-04/json-schema-validation.html#rfc.section.5>`_.
40+
<https://json-schema.org/draft/2020-12/json-schema-validation.html#name-a-vocabulary-for-structural>`_.
4141

4242
.. attribute:: validator_value
4343

@@ -140,7 +140,7 @@ These attributes can be clarified with a short example:
140140
}
141141
}
142142
instance = [{}, 3, "foo"]
143-
v = Draft7Validator(schema)
143+
v = Draft202012Validator(schema)
144144
errors = sorted(v.iter_errors(instance), key=lambda e: e.path)
145145

146146
The error messages in this situation are not very helpful on their own.
@@ -254,7 +254,7 @@ For clarity's sake, the given instance has three errors under this schema:
254254

255255
.. testcode::
256256

257-
v = Draft3Validator(schema)
257+
v = Draft202012Validator(schema)
258258
for error in sorted(v.iter_errors(["spam", 2]), key=str):
259259
print(error.message)
260260

@@ -350,14 +350,14 @@ to guess the most relevant error in a given bunch.
350350

351351
.. doctest::
352352

353-
>>> from jsonschema import Draft7Validator
353+
>>> from jsonschema import Draft202012Validator
354354
>>> from jsonschema.exceptions import best_match
355355

356356
>>> schema = {
357357
... "type": "array",
358358
... "minItems": 3,
359359
... }
360-
>>> print(best_match(Draft7Validator(schema).iter_errors(11)).message)
360+
>>> print(best_match(Draft202012Validator(schema).iter_errors(11)).message)
361361
11 is not of type 'array'
362362

363363

@@ -395,7 +395,7 @@ to guess the most relevant error in a given bunch.
395395
... },
396396
... }
397397
>>> instance = {"name": 123, "phones": {"home": [123]}}
398-
>>> errors = Draft7Validator(schema).iter_errors(instance)
398+
>>> errors = Draft202012Validator(schema).iter_errors(instance)
399399
>>> [
400400
... e.path[-1]
401401
... for e in sorted(errors, key=exceptions.relevance)

docs/faq.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ be valid under the schema.)
152152

153153
.. testcode::
154154

155-
from jsonschema import Draft7Validator, validators
155+
from jsonschema import Draft202012Validator, validators
156156

157157

158158
def extend_with_default(validator_class):
@@ -173,21 +173,21 @@ be valid under the schema.)
173173
)
174174

175175

176-
DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator)
176+
DefaultValidatingValidator = extend_with_default(Draft202012Validator)
177177

178178

179179
# Example usage:
180180
obj = {}
181181
schema = {'properties': {'foo': {'default': 'bar'}}}
182-
# Note jsonschem.validate(obj, schema, cls=DefaultValidatingDraft7Validator)
182+
# Note jsonschem.validate(obj, schema, cls=DefaultValidatingValidator)
183183
# will not work because the metaschema contains `default` directives.
184-
DefaultValidatingDraft7Validator(schema).validate(obj)
184+
DefaultValidatingValidator(schema).validate(obj)
185185
assert obj == {'foo': 'bar'}
186186

187187

188188
See the above-linked document for more info on how this works, but
189189
basically, it just extends the :validator:`properties` validator on
190-
a `jsonschema.Draft7Validator` to then go ahead and update all the
190+
a `jsonschema.Draft202012Validator` to then go ahead and update all the
191191
defaults.
192192

193193
.. note::
@@ -224,7 +224,7 @@ defaults.
224224
}
225225

226226
obj = {}
227-
DefaultValidatingDraft7Validator(schema).validate(obj)
227+
DefaultValidatingValidator(schema).validate(obj)
228228
assert obj == {'outer-object': {'inner-object': 'INNER-DEFAULT'}}
229229

230230
...but if you don't provide a default value for your object, then
@@ -235,7 +235,7 @@ defaults.
235235

236236
del schema["properties"]["outer-object"]["default"]
237237
obj2 = {}
238-
DefaultValidatingDraft7Validator(schema).validate(obj2)
238+
DefaultValidatingValidator(schema).validate(obj2)
239239
assert obj2 == {} # whoops
240240

241241

docs/validate.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ existing `TypeChecker` or create a new one. You may then create a new
9393

9494
def is_my_int(checker, instance):
9595
return (
96-
Draft3Validator.TYPE_CHECKER.is_type(instance, "number") or
96+
Draft202012Validator.TYPE_CHECKER.is_type(instance, "number") or
9797
isinstance(instance, MyInteger)
9898
)
9999

100-
type_checker = Draft3Validator.TYPE_CHECKER.redefine("number", is_my_int)
100+
type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
101+
"number", is_my_int,
102+
)
101103

102104
CustomValidator = validators.extend(
103-
Draft3Validator,
105+
Draft202012Validator,
104106
type_checker=type_checker,
105107
)
106108
validator = CustomValidator(schema={"type" : "number"})
@@ -132,14 +134,14 @@ which each included validator class implements.
132134

133135

134136
For example, if you wanted to validate a schema you created against the
135-
Draft 7 meta-schema, you could use:
137+
Draft 2020-12 meta-schema, you could use:
136138

137139
.. testcode::
138140

139-
from jsonschema import Draft7Validator
141+
from jsonschema import Draft202012Validator
140142

141143
schema = {
142-
"$schema": "http://json-schema.org/draft-07/schema#",
144+
"$schema": Draft202012Validator.META_SCHEMA["$id"],
143145

144146
"type": "object",
145147
"properties": {
@@ -148,7 +150,7 @@ Draft 7 meta-schema, you could use:
148150
},
149151
"required": ["email"]
150152
}
151-
Draft7Validator.check_schema(schema)
153+
Draft202012Validator.check_schema(schema)
152154

153155

154156
.. _validating formats:
@@ -168,7 +170,7 @@ validation can be enabled by hooking in a format-checking object into an
168170
>>> validate(
169171
... instance="-12",
170172
... schema={"format" : "ipv4"},
171-
... format_checker=draft7_format_checker,
173+
... format_checker=draft202012_format_checker,
172174
... )
173175
Traceback (most recent call last):
174176
...

0 commit comments

Comments
 (0)