Skip to content

Commit 158f5db

Browse files
committed
Update type docs.
1 parent 5c4102b commit 158f5db

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

docs/validate.rst

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,24 +155,36 @@ a ``types`` argument on construction that specifies additional types, or which
155155
can be used to specify a different set of Python types to map to a given JSON
156156
type.
157157

158-
For instance, JSON defines a ``number`` type, which can be validated with a
159-
schema such as ``{"type" : "number"}``. By default, this will validate
160-
correctly for Python :class:`int`\s and :class:`float`\s. If you wanted to
161-
additionally validate :class:`decimal.Decimal` objects, you'd use
158+
``jsonschema`` tries to be as general as possible by default. For instance,
159+
JSON defines a ``number`` type, which can be validated with a schema such as
160+
``{"type" : "number"}``. By default, this will validate correctly for instances
161+
of Python :class:`number.Number`\s. This includes in particular :class:`int`\s
162+
and :class:`float`\s, along with `decimal.Decimal` objects, :class:`complex`
163+
numbers etc. See the numbers_ module documentation for more details.
164+
165+
For most purposes, if you want to add additional types as being acceptible for
166+
a validator, you should do so by inheriting from the relevant ``abc`` or by
167+
registering the existing class with the ``abc``. If this isn't an option, or if
168+
you otherwise prefer not doing so, :class:`IValidator`\s have a ``types``
169+
argument that can be used to provide additional or new types.
162170

163171
.. code-block:: python
164172
173+
class MyInteger(object):
174+
...
175+
165176
Draft3Validator(
166177
schema={"type" : "number"},
167-
types={"number" : (int, float, decimal.Decimal)},
178+
types={"number" : (numbers.Number, MyInteger)},
168179
)
169180
170181
The list of default Python types for each JSON type is available on each
171182
validator in the :attr:`IValidator.DEFAULT_TYPES` attribute. Note that you
172183
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 to add it to the
174-
ones being appended.
184+
types, so you may want to access the set of default types when specifying your
185+
additional type.
175186

187+
.. numbers:: http://docs.python.org/3.3/library/numbers.html
176188

177189
.. _versioned-validators:
178190

0 commit comments

Comments
 (0)