Skip to content

Commit 8b25910

Browse files
committed
Recommend the inspect CLI command in more places
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 76e7d8f commit 8b25910

File tree

3 files changed

+96
-21
lines changed

3 files changed

+96
-21
lines changed

content/2020-12/core/anchor.markdown

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,40 @@ itself.
6565

6666
{{</common-pitfall>}}
6767

68+
To debug anchors and how JSON Schema is interpreting your relative URIs, try
69+
the [`jsonschema
70+
inspect`](https://github.com/sourcemeta/jsonschema/blob/main/docs/inspect.markdown)
71+
command. This command prints detailed information about each schema reference
72+
and of each anchor in the schema. For example:
73+
74+
```sh
75+
$ jsonschema inspect schema.json
76+
77+
...
78+
79+
(ANCHOR) URI: https://example.com/person#internal-string
80+
Type : Static
81+
Root : https://example.com/person
82+
Pointer : /$defs/nonEmptyString
83+
Base : https://example.com/person
84+
Relative Pointer : /$defs/nonEmptyString
85+
Dialect : https://json-schema.org/draft/2020-12/schema
86+
Base Dialect : https://json-schema.org/draft/2020-12/schema
87+
Parent :
88+
Instance Location : /firstName
89+
Instance Location : /lastName
90+
91+
...
92+
93+
(REFERENCE) ORIGIN: /properties/firstName/$ref
94+
Type : Static
95+
Destination : https://example.com/person#internal-string
96+
- (w/o fragment) : https://example.com/person
97+
- (fragment) : internal-string
98+
99+
...
100+
```
101+
68102
## Examples
69103

70104
{{<schema `A schema that declares a helper schema associated with a location-independent identifier`>}}

content/2020-12/core/id.markdown

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,47 @@ or a non-locatable URI scheme such as a [Tag URI](https://www.taguri.org).
8484

8585
{{</best-practice>}}
8686

87+
To debug the role of the `$id` keyword on a schema (particularly schemas
88+
with embedded resources), try the [`jsonschema
89+
inspect`](https://github.com/sourcemeta/jsonschema/blob/main/docs/inspect.markdown)
90+
command. This command prints detailed information about each schema resource,
91+
subschema, location, and reference present in the schema. For example:
92+
93+
```sh
94+
$ jsonschema inspect schema.json
95+
(RESOURCE) URI: https://example.com/schema
96+
Type : Static
97+
Root : https://example.com/schema
98+
Pointer :
99+
Base : https://example.com/schema
100+
Relative Pointer :
101+
Dialect : https://json-schema.org/draft/2020-12/schema
102+
Base Dialect : https://json-schema.org/draft/2020-12/schema
103+
Parent : <NONE>
104+
Instance Location :
105+
106+
...
107+
108+
(SUBSCHEMA) URI: https://example.com/schema#/properties/foo
109+
Type : Static
110+
Root : https://example.com/schema
111+
Pointer : /properties/foo
112+
Base : https://example.com/schema
113+
Relative Pointer : /properties/foo
114+
Dialect : https://json-schema.org/draft/2020-12/schema
115+
Base Dialect : https://json-schema.org/draft/2020-12/schema
116+
Parent :
117+
Instance Location : /foo
118+
119+
...
120+
121+
(REFERENCE) ORIGIN: /$schema
122+
Type : Static
123+
Destination : https://json-schema.org/draft/2020-12/schema
124+
- (w/o fragment) : https://json-schema.org/draft/2020-12/schema
125+
- (fragment) : <NONE>
126+
```
127+
87128
## Examples
88129

89130
{{<schema `A schema that declares a potentially resolvable HTTP absolute URL identifier`>}}

content/2020-12/core/ref.markdown

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ be created out of simpler ones. A reference may set its URI fragment to a [JSON
3939
Pointer](https://www.rfc-editor.org/rfc/rfc6901) that determines the
4040
destination of the reference after first resolving the rest of the URI.
4141

42-
{{<common-pitfall>}}
42+
{{<common-pitfall>}}
4343

4444
**Avoid referencing other schema files using their file paths**. While some
4545
implementations support this by automatically constructing schema URIs that
@@ -48,9 +48,9 @@ standard and guaranteed mechanism of declaring a schema URI for identification
4848
and referencing purposes is through the [`$id`]({{< ref "2020-12/core/id" >}})
4949
keyword.
5050

51-
{{</common-pitfall>}}
51+
{{</common-pitfall>}}
5252

53-
{{<common-pitfall>}}
53+
{{<common-pitfall>}}
5454

5555
The target of a reference must be a schema. Referencing a JSON value that is
5656
not unambiguously recognised as a schema leads to undefined behaviour. This
@@ -59,7 +59,7 @@ referencing parts of a schema that a JSON Schema evaluator does not consider to
5959
be a subschema. For example, referencing the contents of the [`examples`]({{<
6060
ref "2020-12/meta-data/examples" >}}) keyword.
6161

62-
{{</common-pitfall>}}
62+
{{</common-pitfall>}}
6363

6464
References are either _internal_ (pointing at schemas within the same schema
6565
definition) or _external_ (pointing at schema resources outside the given
@@ -71,19 +71,19 @@ are notable examples of the latter. A relative reference from a schema embedded
7171
in an OpenAPI specification is resolved from the root of the API specification,
7272
and not from the root of the schema.
7373

74-
{{<best-practice>}}
74+
{{<best-practice>}}
7575

7676
It is highly recommended to make use of _external_ references to break down
7777
complex monolithic schemas into smaller schema files. However, for performance
7878
and integrity reasons, avoid resolving these external schemas (i.e. over HTTP
79-
or the filesystem) at runtime.
79+
or the filesystem) at runtime.
8080

8181
You can automatically inline external references at build time using the
8282
[`jsonschema
8383
bundle`](https://github.com/sourcemeta/jsonschema/blob/main/docs/bundle.markdown)
8484
command.
8585

86-
{{</best-practice>}}
86+
{{</best-practice>}}
8787

8888
Note that a reference to an absolute URI does not necessarily mean that the
8989
reference is external. Conversely, a reference to a relative URI does not
@@ -97,7 +97,7 @@ is considered internal. This internal-first lookup is what enables the standard
9797
](https://json-schema.org/blog/posts/bundling-json-schema-compound-documents)
9898
process.
9999

100-
{{<learning-more>}}
100+
{{<learning-more>}}
101101

102102
If you are having a hard time understanding references and some of its more
103103
subtle scenarios (like base URI resolution), it is probably because you don't
@@ -116,8 +116,8 @@ representation](https://www.rfc-editor.org/rfc/rfc6901#section-6).
116116

117117
{{</learning-more>}}
118118

119-
To debug references and how JSON Schema is interpreting your specific relative
120-
URIs, try the [`jsonschema
119+
To debug references and how JSON Schema is interpreting your relative URIs, try
120+
the [`jsonschema
121121
inspect`](https://github.com/sourcemeta/jsonschema/blob/main/docs/inspect.markdown)
122122
command. This command prints detailed information about each schema reference
123123
and of each location of the schema. For example:
@@ -142,22 +142,22 @@ $ jsonschema inspect schema.json
142142
"$schema": "https://json-schema.org/draft/2020-12/schema",
143143
"$id": "https://example.com/my-schema",
144144
"properties": {
145-
"byRelativeFragmentPointer": {
145+
"byRelativeFragmentPointer": {
146146
"$ref": "#/$defs/helper"
147147
},
148-
"byAbsoluteFragmentPointer": {
148+
"byAbsoluteFragmentPointer": {
149149
"$ref": "https://example.com/my-schema#/$defs/helper"
150150
},
151-
"byRelativeURI": {
151+
"byRelativeURI": {
152152
"$ref": "my-helper"
153153
},
154-
"byRelativeRootPathURI": {
154+
"byRelativeRootPathURI": {
155155
"$ref": "/my-helper"
156156
},
157-
"byRelativeBackslashURI": {
157+
"byRelativeBackslashURI": {
158158
"$ref": "my-schema/../my-helper"
159159
},
160-
"byAbsoluteURI": {
160+
"byAbsoluteURI": {
161161
"$ref": "https://example.com/my-helper"
162162
}
163163
},
@@ -175,16 +175,16 @@ $ jsonschema inspect schema.json
175175
"$schema": "https://json-schema.org/draft/2020-12/schema",
176176
"$id": "https://example.com/my-schema",
177177
"properties": {
178-
"byAbsoluteURI": {
178+
"byAbsoluteURI": {
179179
"$ref": "https://example.com/my-other-schema"
180180
},
181-
"byRelativeURI": {
181+
"byRelativeURI": {
182182
"$ref": "my-other-schema"
183183
},
184-
"byRelativeRootPathURI": {
184+
"byRelativeRootPathURI": {
185185
"$ref": "/my-other-schema"
186186
},
187-
"byRelativeBackslashURI": {
187+
"byRelativeBackslashURI": {
188188
"$ref": "my-schema/../my-other-schema"
189189
}
190190
}
@@ -195,7 +195,7 @@ $ jsonschema inspect schema.json
195195
{
196196
"$schema": "https://json-schema.org/draft/2020-12/schema",
197197
"properties": {
198-
"byAbsoluteURI": {
198+
"byAbsoluteURI": {
199199
"$ref": "urn:example:my-other-schema"
200200
}
201201
}

0 commit comments

Comments
 (0)