Skip to content

Commit 84199e9

Browse files
committed
Elaborate a bit more in the referencing doc
1 parent 19bdf61 commit 84199e9

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

docs/referencing.rst

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,53 @@ The examples below essentially follow these two steps.
1818
.. [1] One that in fact is independent of this `jsonschema` library itself, and may some day be used by other tools or implementations.
1919
2020
21+
Introduction to the `referencing <referencing:index>` API
22+
---------------------------------------------------------
23+
24+
There are 3 main objects to be aware of in the `referencing` API:
25+
26+
* `referencing.Registry`, which represents a specific immutable set of JSON Schemas (either in-memory or retrievable)
27+
* `referencing.Specification`, which represents a specific *version* of the JSON Schema specification, which can have differing referencing behavior.
28+
JSON Schema-specific specifications live in the `referencing.jsonschema` module and are named like `referencing.jsonschema.DRAFT202012`.
29+
* `referencing.Resource`, which represents a specific JSON Schema (often a Python `dict`) *along* with a specific `referencing.Specification` it is to be interpreted under.
30+
31+
As a concrete example, the simple schema ``{"type": "integer"}`` may be interpreted as a schema under either Draft 2020-12 or Draft 4 of the JSON Schema specification (amongst others); in draft 2020-12, the float ``2.0`` must be considered an integer, whereas in draft 4, it potentially is not.
32+
If you mean the former (i.e. to associate this schema with draft 2020-12), you'd use ``referencing.Resource(contents={"type": "integer"}, specification=referencing.jsonschema.DRAFT202012)``, whereas for the latter you'd use `referencing.jsonschema.DRAFT4`.
33+
34+
.. seealso:: the JSON Schema :kw:`$schema` keyword
35+
36+
Which should generally be used to remove all ambiguity and identify *internally* to the schema what version it is written for.
37+
38+
A schema may be identified via one or more URIs, either because they contain an :kw:`$id` keyword (in suitable versions of the JSON Schema specification) which indicates their canonical URI, or simply because you wish to externally associate a URI with the schema, regardless of whether it contains an ``$id`` keyword.
39+
You could add the aforementioned simple schema to a `referencing.Registry` by creating an empty registry and then identifying it via some URI:
40+
41+
.. testcode::
42+
43+
from referencing import Registry, Resource
44+
from referencing.jsonschema import DRAFT202012
45+
schema = Resource(contents={"type": "integer"}, specification=DRAFT202012)
46+
registry = Registry().with_resource(uri="http://example.com/my/schema", resource=schema)
47+
print(registry)
48+
49+
.. testoutput::
50+
51+
<Registry (1 uncrawled resource)>
52+
53+
.. note::
54+
55+
`referencing.Registry` is an entirely immutable object.
56+
All of its methods which add schemas (resources) to itself return *new* registry objects containing the added schemas.
57+
58+
You could also confirm your schema is in the registry if you'd like, via `referencing.Registry.contents`, which will show you the contents of a resource at a given URI:
59+
60+
.. testcode::
61+
62+
print(registry.contents("http://example.com/my/schema"))
63+
64+
.. testoutput::
65+
66+
{'type': 'integer'}
67+
2168
Common Scenarios
2269
----------------
2370

@@ -244,7 +291,19 @@ Older versions of `jsonschema` used a different object -- `_RefResolver` -- for
244291

245292
If you are not already constructing your own `_RefResolver`, this change should be transparent to you (or even recognizably improved, as the point of the migration was to improve the quality of the referencing implementation and enable some new functionality).
246293

247-
If you *were* configuring your own `_RefResolver`, here's how to migrate to the newer APIs:
294+
.. table:: Rough equivalence between `_RefResolver` and `referencing.Registry` APIs
295+
:widths: auto
296+
297+
=========================================================== =====================================================================================================================
298+
Old API New API
299+
=========================================================== =====================================================================================================================
300+
``RefResolver.from_schema({"$id": "urn:example:foo", ...}`` ``Registry().with_resource(uri="urn:example:foo", resource=Resource.from_contents({"$id": "urn:example:foo", ...}))``
301+
Overriding ``RefResolver.resolve_from_url`` Passing a callable to `referencing.Registry`\ 's ``retrieve`` argument
302+
``DraftNValidator(..., resolver=_RefResolver(...))`` `` DraftNValidator(..., registry=Registry().with_resources(...))``
303+
=========================================================== =====================================================================================================================
304+
305+
306+
Here are some more specifics on how to migrate to the newer APIs:
248307

249308
The ``store`` argument
250309
~~~~~~~~~~~~~~~~~~~~~~

docs/requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jinja2==3.1.2
4040
# sphinx-autoapi
4141
file:.#egg=jsonschema
4242
# via -r docs/requirements.in
43-
jsonschema-specifications==2023.3.1
43+
jsonschema-specifications==2023.3.2
4444
# via jsonschema
4545
kiwisolver==1.4.4
4646
# via matplotlib
@@ -82,7 +82,7 @@ pytz==2022.7.1
8282
# via babel
8383
pyyaml==6.0
8484
# via sphinx-autoapi
85-
referencing==0.21.0
85+
referencing==0.21.1
8686
# via
8787
# jsonschema
8888
# jsonschema-specifications
@@ -113,7 +113,7 @@ sphinx-basic-ng==1.0.0b1
113113
# via furo
114114
sphinx-copybutton==0.5.1
115115
# via -r docs/requirements.in
116-
sphinx-json-schema-spec==2023.2.2
116+
sphinx-json-schema-spec==2023.2.4
117117
# via -r docs/requirements.in
118118
sphinxcontrib-applehelp==1.0.4
119119
# via sphinx
@@ -135,5 +135,5 @@ unidecode==1.3.6
135135
# via sphinx-autoapi
136136
urllib3==1.26.14
137137
# via requests
138-
wrapt==1.14.1
138+
wrapt==1.15.0
139139
# via astroid

docs/spelling-wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ValidationError
66

77
# 0th, sigh...
88
th
9+
amongst
910
callables
1011
# non-codeblocked cls from autoapi
1112
cls

0 commit comments

Comments
 (0)