You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enable doctests for the rest of the referencing page.
I recall this being broken previously where I couldn't simultaneously
get doctesting to work while not breaking syntax highlighting, but that
now appears to work.
Copy file name to clipboardExpand all lines: docs/referencing.rst
+31-18Lines changed: 31 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ We may wish to have other schemas we write be able to make use of this schema, a
91
91
92
92
To do so we make use of APIs from the referencing library to create a `referencing.Registry` which maps the URIs above to this schema:
93
93
94
-
.. code:: python
94
+
.. testcode::
95
95
96
96
from referencing import Registry, Resource
97
97
schema = Resource.from_contents(
@@ -130,7 +130,7 @@ which has the same functional effect.
130
130
You can now pass this registry to your `Validator`, which allows a schema passed to it to make use of the aforementioned URIs to refer to our non-negative integer schema.
131
131
Here for instance is an example which validates that instances are JSON objects with non-negative integral values:
132
132
133
-
.. code:: python
133
+
.. testcode::
134
134
135
135
from jsonschema import Draft202012Validator
136
136
validator = Draft202012Validator(
@@ -141,7 +141,7 @@ Here for instance is an example which validates that instances are JSON objects
141
141
registry=registry, # the critical argument, our registry from above
142
142
)
143
143
validator.validate({"foo": 37})
144
-
validator.validate({"foo": -37}) # Uh oh!
144
+
assert not validator.is_valid({"foo": -37}) # Uh oh!
145
145
146
146
.. _ref-filesystem:
147
147
@@ -154,7 +154,7 @@ If however you wish to *dynamically* read files off of the file system, perhaps
154
154
155
155
Here we resolve any schema beginning with ``http://localhost`` to a directory ``/tmp/schemas`` on the local filesystem (note of course that this will not work if run directly unless you have populated that directory with some schemas):
156
156
157
-
.. code:: python
157
+
.. testcode::
158
158
159
159
from pathlib import Path
160
160
import json
@@ -177,7 +177,7 @@ Such a registry can then be used with `Validator` objects in the same way shown
177
177
178
178
We can mix the two examples above if we wish for some in-memory schemas to be available in addition to the filesystem schemas, e.g.:
@@ -194,7 +194,7 @@ As long as you deserialize what you have retrieved into Python objects, you may
194
194
195
195
Here for instance we retrieve YAML documents in a way similar to the `above <ref-filesystem>` using PyYAML:
196
196
197
-
.. code:: python
197
+
.. testcode::
198
198
199
199
from pathlib import Path
200
200
import yaml
@@ -234,7 +234,7 @@ However, if you as a schema author are in a situation where you indeed do wish t
234
234
235
235
Here is how one would configure a registry to automatically retrieve schemas from the `JSON Schema Store <https://www.schemastore.org>`_ on the fly using the `httpx <https://www.python-httpx.org/>`_:
236
236
237
-
.. code:: python
237
+
.. testcode::
238
238
239
239
from referencing import Registry, Resource
240
240
import httpx
@@ -247,7 +247,24 @@ Here is how one would configure a registry to automatically retrieve schemas fro
247
247
248
248
Given such a registry, we can now, for instance, validate instances against schemas from the schema store by passing the ``registry`` we configured to our `Validator` as in previous examples:
0 commit comments