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
Copy file name to clipboardExpand all lines: docs/intro.rst
+150Lines changed: 150 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,153 @@ You could also confirm your resource is in the registry if you'd like, via `refe
52
52
.. testoutput::
53
53
54
54
{'type': 'integer'}
55
+
56
+
Populating Registries
57
+
---------------------
58
+
59
+
There are a few different methods you can use to populate registries with resources.
60
+
Which one you want to use depends on things like:
61
+
62
+
* do you already have an instance of `referencing.Resource`, or are you creating one out of some loaded JSON?
63
+
If not, does the JSON have some sort of identifier that can be used to determine which specification it belongs to (e.g. the JSON Schema ``$schema`` keyword)?
64
+
* does your resource have an internal ID (e.g. the JSON Schema ``$id`` keyword)?
65
+
* do you have additional (external) URIs you want to refer to the same resource as well?
66
+
* do you have one resource to add or many?
67
+
68
+
We'll assume for example's sake that we're dealing with JSON Schema resources for the following examples, and we'll furthermore assume you have some initial `referencing.Registry` to add them to, perhaps an empty one:
69
+
70
+
.. testcode::
71
+
72
+
from referencing import Registry
73
+
initial_registry = Registry()
74
+
75
+
Recall that registries are immutable, so we'll be "adding" our resources by creating new registries containing the additional resource(s) we add.
76
+
77
+
In the ideal case, you have a JSON Schema with an internal ID, and which also identifies itself for a specific version of JSON Schema e.g.:
If you have such a schema in some JSON text, and wish to add a resource to our registry and be able to identify it using its internal ID (``urn:example:my-schema``) you can simply use:
which is telling you that the resource you've tried to create is ambiguous -- there's no way to know which version of JSON Schema you intend it to be written for.
137
+
138
+
You can of course instead directly create a `Resource`, instead of using `Resource.from_contents`, which will allow you to specify which version of JSON Schema you're intending your schema to be written for:
139
+
140
+
.. testcode::
141
+
142
+
import referencing.jsonschema
143
+
second = Resource(contents=another, specification=referencing.jsonschema.DRAFT202012)
which is now saying that there's no way to add this resource to a registry directly, as it has no ``$id`` -- you must provide whatever URI you intend to use to refer to this resource to be able to add it.
185
+
186
+
You can do so using `referencing.Registry.with_resource` instead of the `@ operator <referencing.Registry.__rmatmul__>` which we have used thus far, and which takes the explicit URI you wish to use as an argument:
If you have more than one resource to add, you can use `Registry.with_resources` (with an ``s``) to add many at once, or, if they meet the criteria to use ``@``, you can use ``[one, two, three] @ registry`` to add all three resources at once.
203
+
204
+
You may also want to have a look at `Registry.with_contents` for a further method to add resources to a registry without constructing a `Resource` object yourself.
0 commit comments