Skip to content

Commit 7d34d51

Browse files
committed
Prefer 'keyword' over 'validator' in docs.
In newer JSON Schema specifications we've standardized more on this language rather than calling things validators (and such a thing already has plenty of overloaded meaning here). This commit doesn't do any deprecation, so there's still some awkwardness in that ValidationError.validator is the keyword which failed validation, and Validator.VALIDATORS is a mapping of keywords to callables. We may choose to do so later, but for now will save some API churn in case something else changes.
1 parent 18a64d7 commit 7d34d51

File tree

12 files changed

+106
-108
lines changed

12 files changed

+106
-108
lines changed

docs/errors.rst

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,29 @@ raised or returned, depending on which method or function is used.
3636

3737
.. attribute:: validator
3838

39-
The name of the failed `validator
39+
The name of the failed `keyword
4040
<https://json-schema.org/draft/2020-12/json-schema-validation.html#name-a-vocabulary-for-structural>`_.
4141

4242
.. attribute:: validator_value
4343

44-
The value for the failed validator in the schema.
44+
The associated value for the failed keyword in the schema.
4545

4646
.. attribute:: schema
4747

4848
The full schema that this error came from. This is potentially a
4949
subschema from within the schema that was passed in originally,
50-
or even an entirely different schema if a :validator:`$ref` was
50+
or even an entirely different schema if a :kw:`$ref` was
5151
followed.
5252

5353
.. attribute:: relative_schema_path
5454

55-
A `collections.deque` containing the path to the failed
56-
validator within the schema.
55+
A `collections.deque` containing the path to the failed keyword
56+
within the schema.
5757

5858
.. attribute:: absolute_schema_path
5959

6060
A `collections.deque` containing the path to the failed
61-
validator within the schema, but always relative to the
61+
keyword within the schema, but always relative to the
6262
*original* schema as opposed to any subschema (i.e. the one
6363
originally passed into a validator class, *not* `schema`\).
6464

@@ -179,7 +179,7 @@ the specific part of the instance and subschema that caused each of the errors.
179179
This can be seen with the `ValidationError.instance` and
180180
`ValidationError.schema` attributes.
181181

182-
With validators like :validator:`anyOf`, the `ValidationError.context`
182+
With keywords like :kw:`anyOf`, the `ValidationError.context`
183183
attribute can be used to see the sub-errors which caused the failure. Since
184184
these errors actually came from two separate subschemas, it can be helpful to
185185
look at the `ValidationError.schema_path` attribute as well to see where
@@ -224,8 +224,8 @@ easier debugging.
224224
ErrorTrees
225225
----------
226226

227-
If you want to programmatically be able to query which properties or validators
228-
failed when validating a given instance, you probably will want to do so using
227+
If you want to programmatically query which validation keywords
228+
failed when validating a given instance, you may want to do so using
229229
`jsonschema.exceptions.ErrorTree` objects.
230230

231231
.. autoclass:: jsonschema.exceptions.ErrorTree
@@ -235,7 +235,7 @@ failed when validating a given instance, you probably will want to do so using
235235

236236
.. attribute:: errors
237237

238-
The mapping of validator names to the error objects (usually
238+
The mapping of validator keywords to the error objects (usually
239239
`jsonschema.exceptions.ValidationError`\s) at this level
240240
of the tree.
241241

@@ -301,18 +301,17 @@ the `ErrorTree.errors` attribute.
301301
>>> sorted(tree[0].errors)
302302
['enum', 'type']
303303

304-
Here we see that the :validator:`enum` and :validator:`type` validators failed
305-
for index ``0``. In fact `ErrorTree.errors` is a dict, whose values are
306-
the `ValidationError`\s, so we can get at those directly if we want
307-
them.
304+
Here we see that the :kw:`enum` and :kw:`type` keywords failed for
305+
index ``0``. In fact `ErrorTree.errors` is a dict, whose values are the
306+
`ValidationError`\s, so we can get at those directly if we want them.
308307

309308
.. doctest::
310309

311310
>>> print(tree[0].errors["type"].message)
312311
'spam' is not of type 'number'
313312

314-
Of course this means that if we want to know if a given named
315-
validator failed for a given index, we check for its presence in
313+
Of course this means that if we want to know if a given validation
314+
keyword failed for a given index, we check for its presence in
316315
`ErrorTree.errors`:
317316

318317
.. doctest::
@@ -323,9 +322,9 @@ validator failed for a given index, we check for its presence in
323322
>>> "minimum" in tree[0].errors
324323
False
325324

326-
Finally, if you were paying close enough attention, you'll notice that we
327-
haven't seen our :validator:`minItems` error appear anywhere yet. This is
328-
because :validator:`minItems` is an error that applies globally to the instance
325+
Finally, if you were paying close enough attention, you'll notice that
326+
we haven't seen our :kw:`minItems` error appear anywhere yet. This is
327+
because :kw:`minItems` is an error that applies globally to the instance
329328
itself. So it appears in the root node of the tree.
330329

331330
.. doctest::
@@ -336,9 +335,9 @@ itself. So it appears in the root node of the tree.
336335
That's all you need to know to use error trees.
337336

338337
To summarize, each tree contains child trees that can be accessed by
339-
indexing the tree to get the corresponding child tree for a given index
340-
into the instance. Each tree and child has a `ErrorTree.errors`
341-
attribute, a dict, that maps the failed validator name to the
338+
indexing the tree to get the corresponding child tree for a given
339+
index into the instance. Each tree and child has a `ErrorTree.errors`
340+
attribute, a dict, that maps the failed validation keyword to the
342341
corresponding validation error.
343342

344343

@@ -373,14 +372,14 @@ to guess the most relevant error in a given bunch.
373372
`sorted` or `max` will cause more relevant errors to be
374373
considered greater than less relevant ones.
375374

376-
Within the different validators that can fail, this function
377-
considers :validator:`anyOf` and :validator:`oneOf` to be *weak*
378-
validation errors, and will sort them lower than other validators at
379-
the same level in the instance.
375+
Within the different validation keywords that can fail, this
376+
function considers :kw:`anyOf` and :kw:`oneOf` to be *weak*
377+
validation errors, and will sort them lower than other errors at the
378+
same level in the instance.
380379

381-
If you want to change the set of weak [or strong] validators you can create
382-
a custom version of this function with `by_relevance` and provide a
383-
different set of each.
380+
If you want to change the set of weak [or strong] validation
381+
keywords you can create a custom version of this function with
382+
`by_relevance` and provide a different set of each.
384383

385384
.. doctest::
386385

docs/faq.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Frequently Asked Questions
66
My schema specifies format validation. Why do invalid instances seem valid?
77
---------------------------------------------------------------------------
88

9-
The :validator:`format` validator can be a bit of a stumbling block for new
9+
The :kw:`format` keyword can be a bit of a stumbling block for new
1010
users working with JSON Schema.
1111

1212
In a schema such as:
@@ -16,40 +16,40 @@ In a schema such as:
1616
{"type": "string", "format": "date"}
1717
1818
JSON Schema specifications have historically differentiated between the
19-
:validator:`format` validator and other validators. In particular, the
20-
:validator:`format` validator was specified to be *informational* as much
19+
:kw:`format` keyword and other keywords. In particular, the
20+
:kw:`format` keyword was specified to be *informational* as much
2121
as it may be used for validation.
2222

2323
In other words, for many use cases, schema authors may wish to use
24-
values for the :validator:`format` validator but have no expectation
24+
values for the :kw:`format` keyword but have no expectation
2525
they be validated alongside other required assertions in a schema.
2626

2727
Of course this does not represent all or even most use cases -- many
2828
schema authors *do* wish to assert that instances conform fully, even to
2929
the specific format mentioned.
3030

3131
In drafts prior to ``draft2019-09``, the decision on whether to
32-
automatically enable :validator:`format` validation was left up to
32+
automatically enable :kw:`format` validation was left up to
3333
validation implementations such as this one.
3434

3535
This library made the choice to leave it off by default, for two reasons:
3636

3737
* for forward compatibility and implementation complexity reasons
38-
-- if :validator:`format` validation were on by default, and a
38+
-- if :kw:`format` validation were on by default, and a
3939
future draft of JSON Schema introduced a hard-to-implement format,
4040
either the implementation of that format would block releases of
4141
this library until it were implemented, or the behavior surrounding
42-
:validator:`format` would need to be even more complex than simply
42+
:kw:`format` would need to be even more complex than simply
4343
defaulting to be on. It therefore was safer to start with it off,
4444
and defend against the expectation that a given format would always
4545
automatically work.
4646

4747
* given that a common use of JSON Schema is for portability across
4848
languages (and therefore implementations of JSON Schema), so that
49-
users be aware of this point itself regarding :validator:`format`
49+
users be aware of this point itself regarding :kw:`format`
5050
validation, and therefore remember to check any *other*
5151
implementations they were using to ensure they too were explicitly
52-
enabled for :validator:`format` validation.
52+
enabled for :kw:`format` validation.
5353

5454
As of ``draft2019-09`` however, the opt-out by default behavior
5555
mentioned here is now *required* for all validators.
@@ -132,11 +132,11 @@ Why doesn't my schema's default property set the default on my instance?
132132
------------------------------------------------------------------------
133133

134134
The basic answer is that the specification does not require that
135-
:validator:`default` actually do anything.
135+
:kw:`default` actually do anything.
136136

137137
For an inkling as to *why* it doesn't actually do anything, consider
138-
that none of the other validators modify the instance either. More
139-
importantly, having :validator:`default` modify the instance can produce
138+
that none of the other keywords modify the instance either. More
139+
importantly, having :kw:`default` modify the instance can produce
140140
quite peculiar things. It's perfectly valid (and perhaps even useful)
141141
to have a default that is not valid under the schema it lives in! So an
142142
instance modified by the default would pass validation the first time,
@@ -179,15 +179,15 @@ be valid under the schema.)
179179
# Example usage:
180180
obj = {}
181181
schema = {'properties': {'foo': {'default': 'bar'}}}
182-
# Note jsonschem.validate(obj, schema, cls=DefaultValidatingValidator)
183-
# will not work because the metaschema contains `default` directives.
182+
# Note jsonschema.validate(obj, schema, cls=DefaultValidatingValidator)
183+
# will not work because the metaschema contains `default` keywords.
184184
DefaultValidatingValidator(schema).validate(obj)
185185
assert obj == {'foo': 'bar'}
186186

187187

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

193193
.. note::

docs/jsonschema_role.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def setup(app):
3434

3535
path = os.path.join(app.config.cache_path, "spec.html")
3636
spec = fetch_or_load(path)
37-
app.add_role("validator", docutils_does_not_allow_using_classes(spec))
37+
app.add_role("kw", docutils_does_not_allow_using_classes(spec))
3838

3939
return dict(version=__version__, parallel_read_safe=True)
4040

@@ -84,9 +84,9 @@ def docutils_does_not_allow_using_classes(spec):
8484
keeping a dict.
8585
"""
8686

87-
def validator(name, raw_text, text, lineno, inliner):
87+
def keyword(name, raw_text, text, lineno, inliner):
8888
"""
89-
Link to the JSON Schema documentation for a validator.
89+
Link to the JSON Schema documentation for a keyword.
9090
9191
Arguments:
9292
@@ -143,4 +143,4 @@ def validator(name, raw_text, text, lineno, inliner):
143143
reference = nodes.reference(raw_text, text, refuri=uri)
144144
return [reference], []
145145

146-
return validator
146+
return keyword

docs/validate.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ The simplest way to validate an instance under a given schema is to use the
2424
The Validator Protocol
2525
-----------------------
2626

27-
`jsonschema` defines a protocol that all validator
28-
classes should adhere to.
27+
`jsonschema` defines a protocol that all validator classes should adhere
28+
to.
2929

3030
.. autoclass:: jsonschema.protocols.Validator
3131
:members:
@@ -38,7 +38,7 @@ more information see `creating-validators`.
3838
Type Checking
3939
-------------
4040

41-
To handle JSON Schema's :validator:`type` property, a `Validator` uses
41+
To handle JSON Schema's :kw:`type` keyword, a `Validator` uses
4242
an associated `TypeChecker`. The type checker provides an immutable
4343
mapping between names of types and functions that can test if an instance is
4444
of that type. The defaults are suitable for most users - each of the
@@ -65,7 +65,7 @@ Validating With Additional Types
6565
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6666

6767
Occasionally it can be useful to provide additional or alternate types when
68-
validating the JSON Schema's :validator:`type` property.
68+
validating JSON Schema's :kw:`type` keyword.
6969

7070
`jsonschema` tries to strike a balance between performance in the common
7171
case and generality. For instance, JSON Schema defines a ``number`` type, which
@@ -158,7 +158,7 @@ Draft 2020-12 meta-schema, you could use:
158158
Validating Formats
159159
------------------
160160

161-
JSON Schema defines the :validator:`format` property which can be used to check
161+
JSON Schema defines the :kw:`format` keyword which can be used to check
162162
if primitive types (``string``\s, ``number``\s, ``boolean``\s) conform to
163163
well-defined formats. By default, no validation is enforced, but optionally,
164164
validation can be enabled by hooking in a format-checking object into an
@@ -282,7 +282,7 @@ Checker Notes
282282
and that that recipient is the correct one the email is intended
283283
for, and since many valid email addresses are in many places
284284
incorrectly rejected, and many invalid email addresses are in many
285-
places incorrectly accepted, the ``email`` format validator only
285+
places incorrectly accepted, the ``email`` format keyword only
286286
provides a sanity check, not full rfc5322_ validation.
287287

288288
The same applies to the ``idn-email`` format.

jsonschema/_legacy_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def ignore_ref_siblings(schema):
66
"""
77
Ignore siblings of ``$ref`` if it is present.
88
9-
Otherwise, return all validators.
9+
Otherwise, return all keywords.
1010
1111
Suitable for use with `create`'s ``applicable_validators`` argument.
1212
"""
@@ -47,9 +47,9 @@ def dependencies_draft4_draft6_draft7(
4747
schema,
4848
):
4949
"""
50-
Support for the ``dependencies`` validator from pre-draft 2019-09.
50+
Support for the ``dependencies`` keyword from pre-draft 2019-09.
5151
52-
In later drafts, the validator was split into separate
52+
In later drafts, the keyword was split into separate
5353
``dependentRequired`` and ``dependentSchemas`` validators.
5454
"""
5555
if not validator.is_type(instance, "object"):

jsonschema/_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def find_evaluated_item_indexes_by_schema(validator, instance, schema):
224224

225225
try:
226226
evaluated_indexes += find_evaluated_item_indexes_by_schema(
227-
validator, instance, resolved)
227+
validator, instance, resolved,
228+
)
228229
finally:
229230
validator.resolver.pop_scope()
230231

jsonschema/exceptions.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,16 @@ def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES):
315315
316316
Arguments:
317317
weak (set):
318-
a collection of validator names to consider to be "weak".
319-
If there are two errors at the same level of the instance
320-
and one is in the set of weak validator names, the other
321-
error will take priority. By default, :validator:`anyOf` and
322-
:validator:`oneOf` are considered weak validators and will
323-
be superseded by other same-level validation errors.
318+
a collection of validation keywords to consider to be
319+
"weak". If there are two errors at the same level of the
320+
instance and one is in the set of weak validation keywords,
321+
the other error will take priority. By default, :kw:`anyOf`
322+
and :kw:`oneOf` are considered weak keywords and will be
323+
superseded by other same-level validation errors.
324324
325325
strong (set):
326-
a collection of validator names to consider to be "strong"
326+
a collection of validation keywords to consider to be
327+
"strong"
327328
"""
328329
def relevance(error):
329330
validator = error.validator
@@ -347,10 +348,10 @@ def best_match(errors, key=relevance):
347348
`ValidationError.path` is shorter) are considered better matches,
348349
since they indicate "more" is wrong with the instance.
349350
350-
If the resulting match is either :validator:`oneOf` or :validator:`anyOf`,
351-
the *opposite* assumption is made -- i.e. the deepest error is picked,
352-
since these validators only need to match once, and any other errors may
353-
not be relevant.
351+
If the resulting match is either :kw:`oneOf` or :kw:`anyOf`, the
352+
*opposite* assumption is made -- i.e. the deepest error is picked,
353+
since these keywords only need to match once, and any other errors
354+
may not be relevant.
354355
355356
Arguments:
356357
errors (collections.abc.Iterable):

0 commit comments

Comments
 (0)