Skip to content

Commit add447f

Browse files
committed
Try to be as explicit as possible on the million different uses of 'validator'.
Closes #175.
1 parent fcb1787 commit add447f

File tree

4 files changed

+113
-102
lines changed

4 files changed

+113
-102
lines changed

docs/creating.rst

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,87 @@
11
.. _creating-validators:
22

3-
================================
4-
Creating or Extending Validators
5-
================================
3+
=======================================
4+
Creating or Extending Validator Classes
5+
=======================================
66

77
.. currentmodule:: jsonschema.validators
88

99
.. autofunction:: create
1010

11-
Create a new validator (class).
11+
Create a new validator class.
1212

1313
:argument dict meta_schema: the meta schema for the new validator class
1414

15-
:argument dict validators: a mapping from validator names to functions that
16-
validate the given name. Each function should take 4 arguments: a
17-
validator instance, the value of the current validator property in the
18-
instance being validated, the instance, and the schema.
15+
:argument dict validators: a mapping from names to callables, where
16+
each callable will validate the schema property with the given
17+
name.
18+
19+
Each callable should take 4 arguments:
1920

20-
:argument str version: an identifier for the version that this validator
21-
will validate. If provided, the returned validator class will have its
22-
``__name__`` set to include the version, and also will have
23-
:func:`validates` automatically called for the given version.
21+
1. a validator instance,
22+
2. the value of the property being validated within the instance
23+
3. the instance
24+
4. the schema
2425

25-
:argument dict default_types: a default mapping to use for instances of the
26-
validator when mapping between JSON types to Python types. The default
27-
for this argument is probably fine. Instances of the returned validator
26+
:argument str version: an identifier for the version that this validator
27+
class will validate. If provided, the returned validator class
28+
will have its ``__name__`` set to include the version, and also
29+
will have :func:`validates` automatically called for the given
30+
version.
31+
32+
:argument dict default_types: a default mapping to use for instances
33+
of the validator class when mapping between JSON types to Python
34+
types. The default for this argument is probably fine. Instances
2835
can still have their types customized on a per-instance basis.
2936

3037
:returns: a new :class:`jsonschema.IValidator` class
3138

3239

3340
.. autofunction:: extend
3441

35-
Create a new validator that extends an existing validator class.
42+
Create a new validator class by extending an existing one.
3643

3744
:argument jsonschema.IValidator validator: an existing validator class
3845

39-
:argument dict validators: a set of new validators to add to the new
40-
validator.
46+
:argument dict validators: a mapping of new validator callables to extend
47+
with, whose structure is as in :func:`create`\ .
4148

4249
.. note::
4350

44-
Any validators with the same name as an existing one will
45-
(silently) replace the old validator entirely.
51+
Any validator callables with the same name as an existing one will
52+
(silently) replace the old validator callable entirely, effectively
53+
overriding any validation done in the "parent" validator class.
4654

47-
If you wish to extend an old validator, call it directly in the
48-
replacing validator function by retrieving it using
49-
``OldValidator.VALIDATORS["the validator"]``.
55+
If you wish to instead extend the behavior of a parent's
56+
validator callable, delegate and call it directly in
57+
the new validator function by retrieving it using
58+
``OldValidator.VALIDATORS["validator_name"]``.
5059

51-
:argument str version: a version for the new validator
60+
:argument str version: a version for the new validator class
5261

5362
:returns: a new :class:`jsonschema.IValidator` class
5463

5564
.. note:: Meta Schemas
5665

57-
The new validator will just keep the old validator's meta schema.
58-
59-
If you wish to change or extend the meta schema in the new validator,
60-
modify ``META_SCHEMA`` directly on the returned class.
66+
The new validator class will have its parent's meta schema.
6167

62-
The meta schema on the new validator will not be a copy, so you'll
63-
probably want to copy it before modifying it to not affect the old
64-
validator.
68+
If you wish to change or extend the meta schema in the new
69+
validator class, modify ``META_SCHEMA`` directly on the returned
70+
class. Note that no implicit copying is done, so a copy should
71+
likely be made before modifying it, in order to not affect the
72+
old validator.
6573

6674

6775
.. autofunction:: validator_for
6876

69-
Retrieve the validator appropriate for validating the given schema.
77+
Retrieve the validator class appropriate for validating the given schema.
7078

7179
Uses the :validator:`$schema` property that should be present in the given
72-
schema to look up the appropriate validator.
80+
schema to look up the appropriate validator class.
7381

7482
:argument schema: the schema to look at
75-
:argument default: the default to return if the appropriate validator
76-
cannot be determined. If unprovided, the default will be to just return
83+
:argument default: the default to return if the appropriate validator class
84+
cannot be determined. If unprovided, the default is to return
7785
:class:`Draft4Validator`
7886

7987

docs/errors.rst

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

3737
.. attribute:: validator
3838

39-
The failed `validator
39+
The name of the failed `validator
4040
<http://json-schema.org/latest/json-schema-validation.html#anchor12>`_.
4141

4242
.. attribute:: validator_value
@@ -46,8 +46,9 @@ raised or returned, depending on which method or function is used.
4646
.. attribute:: schema
4747

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

5253
.. attribute:: relative_schema_path
5354

@@ -59,7 +60,7 @@ raised or returned, depending on which method or function is used.
5960
A :class:`collections.deque` containing the path to the failed
6061
validator within the schema, but always relative to the
6162
*original* schema as opposed to any subschema (i.e. the one
62-
originally passed into a validator, *not* :attr:`schema`\).
63+
originally passed into a validator class, *not* :attr:`schema`\).
6364

6465
.. attribute:: schema_path
6566

@@ -86,12 +87,12 @@ raised or returned, depending on which method or function is used.
8687

8788
.. attribute:: instance
8889

89-
The instance that was being validated. This will differ from the
90-
instance originally passed into validate if the validator was in the
91-
process of validating a (possibly nested) element within the top-level
92-
instance. The path within the top-level instance (i.e.
93-
:attr:`ValidationError.path`) could be used to find this object, but it
94-
is provided for convenience.
90+
The instance that was being validated. This will differ from
91+
the instance originally passed into :meth:`validate` if the
92+
validator object was in the process of validating a (possibly
93+
nested) element within the top-level instance. The path within
94+
the top-level instance (i.e. :attr:`ValidationError.path`) could
95+
be used to find this object, but it is provided for convenience.
9596

9697
.. attribute:: context
9798

@@ -266,8 +267,9 @@ more easily than by just iterating over the error objects.
266267
tree = ErrorTree(v.iter_errors(instance))
267268

268269
As you can see, :class:`ErrorTree` takes an iterable of
269-
:class:`ValidationError`\s when constructing a tree so you can directly pass it
270-
the return value of a validator's :attr:`~IValidator.iter_errors` method.
270+
:class:`ValidationError`\s when constructing a tree so you
271+
can directly pass it the return value of a validator object's
272+
:attr:`~IValidator.iter_errors` method.
271273

272274
:class:`ErrorTree`\s support a number of useful operations. The first one we
273275
might want to perform is to check whether a given element in our instance
@@ -303,8 +305,9 @@ them.
303305
>>> print(tree[0].errors["type"].message)
304306
'spam' is not of type 'number'
305307

306-
Of course this means that if we want to know if a given validator failed for a
307-
given index, we check for its presence in :attr:`~ErrorTree.errors`:
308+
Of course this means that if we want to know if a given named
309+
validator failed for a given index, we check for its presence in
310+
:attr:`~ErrorTree.errors`:
308311

309312
.. doctest::
310313

@@ -326,10 +329,11 @@ itself. So it appears in the root node of the tree.
326329

327330
That's all you need to know to use error trees.
328331

329-
To summarize, each tree contains child trees that can be accessed by indexing
330-
the tree to get the corresponding child tree for a given index into the
331-
instance. Each tree and child has a :attr:`~ErrorTree.errors` attribute, a
332-
dict, that maps the failed validator to the corresponding validation error.
332+
To summarize, each tree contains child trees that can be accessed by
333+
indexing the tree to get the corresponding child tree for a given index
334+
into the instance. Each tree and child has a :attr:`~ErrorTree.errors`
335+
attribute, a dict, that maps the failed validator name to the
336+
corresponding validation error.
333337

334338

335339
best_match and relevance
@@ -425,10 +429,11 @@ to guess the most relevant error in a given bunch.
425429

426430
Create a key function that can be used to sort errors by relevance.
427431

428-
:argument set weak: a collection of validators to consider to be "weak". If
429-
there are two errors at the same level of the instance and one is in
430-
the set of weak validators, the other error will take priority. By
431-
default, :validator:`anyOf` and :validator:`oneOf` are considered weak
432-
validators and will be superceded by other same-level validation
433-
errors.
434-
:argument set strong: a collection of validators to consider to be "strong"
432+
:argument set weak: a collection of validator names to consider to
433+
be "weak". If there are two errors at the same level of the
434+
instance and one is in the set of weak validator names, the
435+
other error will take priority. By default, :validator:`anyOf`
436+
and :validator:`oneOf` are considered weak validators and will
437+
be superceded by other same-level validation errors.
438+
:argument set strong: a collection of validator names to consider to
439+
be "strong"

docs/faq.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ It's perfectly valid (and perhaps even useful) to have a default that is not
1616
valid under the schema it lives in! So an instance modified by the default
1717
would pass validation the first time, but fail the second!
1818

19-
Still, filling in defaults is a thing that is useful. :mod:`jsonschema` allows
20-
you to :doc:`define your own validators <creating>`, so you can easily create a
21-
:class:`IValidator` that does do default setting. Here's some code to get you
22-
started:
19+
Still, filling in defaults is a thing that is useful. :mod:`jsonschema`
20+
allows you to :doc:`define your own validator classes and callables
21+
<creating>`, so you can easily create a :class:`IValidator` that does do
22+
default setting. Here's some code to get you started:
2323

2424
.. code-block:: python
2525

docs/validate.rst

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,28 @@ The simplest way to validate an instance under a given schema is to use the
2323
The Validator Interface
2424
-----------------------
2525

26-
:mod:`jsonschema` defines an (informal) interface that all validators should
27-
adhere to.
26+
:mod:`jsonschema` defines an (informal) interface that all validator
27+
classes should adhere to.
2828

2929
.. class:: IValidator(schema, types=(), resolver=None, format_checker=None)
3030

31-
:argument dict schema: the schema that the validator will validate with. It
32-
is assumed to be valid, and providing an invalid
33-
schema can lead to undefined behavior. See
34-
:meth:`IValidator.check_schema` to validate a schema
35-
first.
36-
:argument types: Override or extend the list of known types when validating
37-
the :validator:`type` property. Should map strings (type
38-
names) to class objects that will be checked via
39-
:func:`isinstance`. See :ref:`validating-types` for
40-
details.
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+
:meth:`IValidator.check_schema` to validate a schema first.
35+
:argument types: Override or extend the list of known types when
36+
validating the :validator:`type` property. Should map strings (type
37+
names) to class objects that will be checked via :func:`isinstance`.
38+
See :ref:`validating-types` for details.
4139
:type types: dict or iterable of 2-tuples
42-
:argument resolver: an instance of :class:`RefResolver` that will be used
43-
to resolve :validator:`$ref` properties (JSON
44-
references). If unprovided, one will be created.
45-
:argument format_checker: an instance of :class:`FormatChecker` whose
46-
:meth:`~conforms` method will be called to check
47-
and see if instances conform to each
48-
:validator:`format` property present in the
49-
schema. If unprovided, no validation will be done
50-
for :validator:`format`.
40+
:argument resolver: an instance of :class:`RefResolver` that will be
41+
used to resolve :validator:`$ref` properties (JSON references). If
42+
unprovided, one will be created.
43+
:argument format_checker: an instance of :class:`FormatChecker`
44+
whose :meth:`~conforms` method will be called to check and see if
45+
instances conform to each :validator:`format` property present
46+
in the schema. If unprovided, no validation will be done for
47+
:validator:`format`.
5148

5249
.. attribute:: DEFAULT_TYPES
5350

@@ -61,13 +58,13 @@ adhere to.
6158

6259
.. attribute:: VALIDATORS
6360

64-
A mapping of validators (:class:`str`\s) to functions that validate the
65-
validator property with that name. For more information see
66-
:ref:`creating-validators`.
61+
A mapping of validator names (:class:`str`\s) to functions
62+
that validate the validator property with that name. For more
63+
information see :ref:`creating-validators`.
6764

6865
.. attribute:: schema
6966

70-
The schema that was passed in when initializing the validator.
67+
The schema that was passed in when initializing the object.
7168

7269

7370
.. classmethod:: check_schema(schema)
@@ -124,10 +121,11 @@ adhere to.
124121
ValidationError: [2, 3, 4] is too long
125122

126123

127-
All of the :ref:`versioned validators <versioned-validators>` that are included
128-
with :mod:`jsonschema` adhere to the interface, and implementors of validators
129-
that extend or complement the ones included should adhere to it as well. For
130-
more information see :ref:`creating-validators`.
124+
All of the :ref:`versioned validators <versioned-validators>` that
125+
are included with :mod:`jsonschema` adhere to the interface, and
126+
implementors of validator classes that extend or complement the
127+
ones included should adhere to it as well. For more information see
128+
:ref:`creating-validators`.
131129

132130

133131
.. _validating-types:
@@ -154,7 +152,7 @@ more general instance checks can introduce significant slowdown, especially
154152
given how common validating these types are.
155153

156154
If you *do* want the generality, or just want to add a few specific additional
157-
types as being acceptible for a validator, :class:`IValidator`\s have a
155+
types as being acceptible for a validator object, :class:`IValidator`\s have a
158156
``types`` argument that can be used to provide additional or new types.
159157

160158
.. code-block:: python
@@ -168,20 +166,20 @@ types as being acceptible for a validator, :class:`IValidator`\s have a
168166
)
169167
170168
The list of default Python types for each JSON type is available on each
171-
validator in the :attr:`IValidator.DEFAULT_TYPES` attribute. Note that you
172-
need to specify all types to match if you override one of the existing JSON
173-
types, so you may want to access the set of default types when specifying your
174-
additional type.
169+
validator object in the :attr:`IValidator.DEFAULT_TYPES` attribute. Note
170+
that you need to specify all types to match if you override one of the
171+
existing JSON types, so you may want to access the set of default types
172+
when specifying your additional type.
175173

176174
.. _versioned-validators:
177175

178176
Versioned Validators
179177
--------------------
180178

181-
:mod:`jsonschema` ships with validators for various versions of the JSON Schema
182-
specification. For details on the methods and attributes that each validator
183-
provides see the :class:`IValidator` interface, which each validator
184-
implements.
179+
:mod:`jsonschema` ships with validator classes for various versions of
180+
the JSON Schema specification. For details on the methods and attributes
181+
that each validator class provides see the :class:`IValidator` interface,
182+
which each included validator class implements.
185183

186184
.. autoclass:: Draft3Validator
187185

0 commit comments

Comments
 (0)