Skip to content

Commit cd1fbc3

Browse files
committed
Improve $ref examples
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 44aace2 commit cd1fbc3

1 file changed

Lines changed: 38 additions & 157 deletions

File tree

content/2020-12/core/ref.markdown

Lines changed: 38 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -137,186 +137,67 @@ $ jsonschema inspect schema.json
137137

138138
## Examples
139139

140-
{{<schema `Schema with a relative reference` >}}
140+
{{<schema `A schema that internally references the exact same helper schema in multiple equivalent ways`>}}
141141
{
142142
"$schema": "https://json-schema.org/draft/2020-12/schema",
143-
"$id": "https://example.com/schemas/product.json",
144-
"type": "object",
143+
"$id": "https://example.com/my-schema",
145144
"properties": {
146-
"productId": { "type": "integer" },
147-
"name": { "$ref": "string" }
145+
"byRelativeFragmentPointer": {
146+
"$ref": "#/$defs/helper"
147+
},
148+
"byAbsoluteFragmentPointer": {
149+
"$ref": "https://example.com/my-schema#/$defs/helper"
150+
},
151+
"byRelativeURI": {
152+
"$ref": "my-helper"
153+
},
154+
"byRelativeRootPathURI": {
155+
"$ref": "/my-helper"
156+
},
157+
"byRelativeBackslashURI": {
158+
"$ref": "my-schema/../my-helper"
159+
},
160+
"byAbsoluteURI": {
161+
"$ref": "https://example.com/my-helper"
162+
}
148163
},
149-
"required": [ "productId", "name" ],
150164
"$defs": {
151-
"string": {
152-
"$id": "string",
165+
"helper": {
166+
"$id": "my-helper",
153167
"type": "string"
154168
}
155169
}
156170
}
157171
{{</schema>}}
158172

159-
{{<instance-pass `An instance including all the required properties is valid` >}}
160-
{
161-
"productId": 123,
162-
"name": "Widget"
163-
}
164-
{{</instance-pass>}}
165-
166-
{{<instance-fail `An object instance with name property not set to string is invalid` >}}
167-
{
168-
"productId": 217,
169-
"name": 999
170-
}
171-
{{</instance-fail>}}
172-
173-
{{<schema `Schema with an absolute reference to the previous schema` >}}
173+
{{<schema `A schema that externally references the exact same schema URL in multiple equivalent ways`>}}
174174
{
175175
"$schema": "https://json-schema.org/draft/2020-12/schema",
176-
"$id": "https://example.com/schemas/order.json",
177-
"type": "object",
176+
"$id": "https://example.com/my-schema",
178177
"properties": {
179-
"items": {
180-
"type": "array",
181-
"items": { "$ref": "schemas/product.json" }
178+
"byAbsoluteURI": {
179+
"$ref": "https://example.com/my-other-schema"
180+
},
181+
"byRelativeURI": {
182+
"$ref": "my-other-schema"
183+
},
184+
"byRelativeRootPathURI": {
185+
"$ref": "/my-other-schema"
186+
},
187+
"byRelativeBackslashURI": {
188+
"$ref": "my-schema/../my-other-schema"
182189
}
183190
}
184191
}
185192
{{</schema>}}
186193

187-
{{<instance-pass `Each item in the "items" array includes both the "productId" and "name" properties required by the referenced product schema` >}}
188-
{
189-
"items": [
190-
{ "productId": 123, "name": "Widget" },
191-
{ "productId": 456, "name": "Gadget" }
192-
]
193-
}
194-
// Assuming http://example.com/schemas/product.json defines the product schema
195-
196-
{{</instance-pass>}}
197-
198-
{{<instance-fail `The first item is missing the "productId" property and the second item is missing the "name" property required by the product schema.` >}}
199-
{
200-
"items": [
201-
{ "name": "Widget" },
202-
{ "productId": 456 }
203-
]
204-
}
205-
{{</instance-fail>}}
206-
207-
{{<schema `Schema having an absolute reference with a JSON Pointer`>}}
208-
{
209-
"$schema": "https://json-schema.org/draft/2020-12/schema",
210-
"$id": "https://example.com/schemas/product.json",
211-
"$ref": "https://example.com/schemas/product.json#/$defs/string",
212-
"$defs": {
213-
"string": { "type": "string" }
214-
}
215-
}
216-
{{</schema>}}
217-
218-
{{<instance-pass `An instance with a string value is valid`>}}
219-
"John Doe"
220-
{{</instance-pass>}}
221-
222-
{{<instance-fail `An instance with a boolean value is invalid`>}}
223-
true
224-
{{</instance-fail>}}
225-
226-
{{<schema `Schema having absolute reference with an anchor`>}}
194+
{{<schema `A schema that externally references a schema URN in the only possible way (URNs are always absolute)`>}}
227195
{
228196
"$schema": "https://json-schema.org/draft/2020-12/schema",
229-
"$id": "https://example.com/schemas/product.json",
230-
"$ref": "https://example.com/schemas/product.json#string",
231-
"$defs": {
232-
"string": { "$anchor": "string", "type": "boolean" }
233-
}
234-
}
235-
{{</schema>}}
236-
237-
{{<instance-pass `An instance with a boolean value is valid`>}}
238-
false
239-
{{</instance-pass>}}
240-
241-
{{<instance-fail `An instance with a numeric value is invalid`>}}
242-
99
243-
{{</instance-fail>}}
244-
245-
{{<schema `Schema with a JSON Pointer`>}}
246-
{
247-
"$schema": "https://json-schema.org/draft/2020-12/schema",
248-
"$id": "https://example.com",
249-
"type": "object",
250197
"properties": {
251-
"name": { "$ref": "#/$defs/string" }
252-
},
253-
"required": [ "name" ],
254-
"$defs": {
255-
"string": { "type": "string" }
256-
}
257-
}
258-
{{</schema>}}
259-
260-
{{<instance-pass `Instance including all the required properties is valid` >}}
261-
{
262-
"name": "John Doe"
263-
}
264-
{{</instance-pass>}}
265-
266-
{{<instance-fail `Instance with name property set to boolean is invalid` >}}
267-
{
268-
"name": true
269-
}
270-
{{</instance-fail>}}
271-
272-
{{<schema `Schema with an anchor`>}}
273-
{
274-
"$schema": "https://json-schema.org/draft/2020-12/schema",
275-
"$id": "https://example.com",
276-
"type": "object",
277-
"properties": {
278-
"counter": { "$ref": "#counter" }
279-
},
280-
"required": [ "counter" ],
281-
"$defs": {
282-
"string": { "$anchor": "counter", "type": "number" }
283-
}
284-
}
285-
{{</schema>}}
286-
287-
{{<instance-pass `Instance including all the required properties is valid` >}}
288-
{
289-
"counter": 51
290-
}
291-
{{</instance-pass>}}
292-
293-
{{<instance-fail `Instance with counter property set to string is invalid` >}}
294-
{
295-
"counter": "59"
296-
}
297-
{{</instance-fail>}}
298-
299-
{{<schema `Schema with '$id' set to URN`>}}
300-
{
301-
"$schema": "https://json-schema.org/draft/2020-12/schema",
302-
"$id": "urn:example:vehicle",
303-
"$ref": "urn:example:phone",
304-
"$defs": {
305-
"phone": {
306-
"$id": "urn:example:phone",
307-
"type": "number"
198+
"byAbsoluteURI": {
199+
"$ref": "urn:example:my-other-schema"
308200
}
309201
}
310202
}
311203
{{</schema>}}
312-
313-
{{<instance-pass `An instance with a numeric value is valid` >}}
314-
7843559621
315-
{{</instance-pass>}}
316-
317-
{{<instance-fail `An instance with a value other than a number is invalid` >}}
318-
{
319-
"phone": 9866548907
320-
}
321-
{{</instance-fail>}}
322-

0 commit comments

Comments
 (0)