Skip to content

Commit c0b626e

Browse files
committed
add bundling docs
1 parent e2ac46d commit c0b626e

3 files changed

Lines changed: 115 additions & 61 deletions

File tree

.jekyll-metadata

4.88 KB
Binary file not shown.

_docs/schema/basics.md

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -497,70 +497,15 @@ In order to support scenarios where schemas cannot be registered ahead of time,
497497

498498
The URI that is passed may need to be transformed, based on the schemas you're dealing with. For instance if you're loading schemas from a local filesystem, and the schema `$ref`s use relative paths, you may need to prepend the working folder to the URI in order to locate it.
499499

500-
<!-- ## Bundling
500+
## Bundling schemas {#schema-bundling}
501501

502-
JSON Schema can be bundled so that they include all of their referenced documents. This process can make sharing schemas significantly easier as only a single file need to be shared.
502+
When you need to distribute a schema and all of its referenced documents as a single file, use schema bundling.
503503

504-
This library bundles schemas by collecting all of the referenced documents along with the root schema into a new schema's `$defs` keyword, then adding a `$ref` to the definition for the root schema.
504+
The dedicated [Bundling](/schema/bundling) page explains:
505505

506-
For example, given this root and external schema:
507-
508-
```jsonc
509-
// root
510-
{
511-
"$schema": "https://json-schema.org/draft/2020-12/schema",
512-
"$id": "https://json-everything.net/foo",
513-
"type": "object",
514-
"properties": {
515-
"bar": {
516-
"$ref": "bar"
517-
}
518-
}
519-
}
520-
521-
// external
522-
{
523-
"$schema": "https://json-schema.org/draft/2020-12/schema",
524-
"$id": "https://json-everything.net/bar",
525-
"type": "string"
526-
}
527-
```
528-
529-
calling `schema.Bundle()`
530-
531-
```c#
532-
var bundled = rootSchema.Bundle();
533-
```
534-
535-
generates the following bundled schema:
536-
537-
```json
538-
{
539-
"$schema": "https://json-schema.org/draft/2020-12/schema",
540-
"$id": "https://json-everything.net/foo(bundled)",
541-
"$defs": {
542-
"26f6f0d167": {
543-
"$schema": "https://json-schema.org/draft/2020-12/schema",
544-
"$id": "https://json-everything.net/foo",
545-
"type": "object",
546-
"properties": {
547-
"bar": {
548-
"$ref": "bar"
549-
}
550-
}
551-
},
552-
"c6e748adb1": {
553-
"$schema": "https://json-schema.org/draft/2020-12/schema",
554-
"$id": "https://json-everything.net/bar",
555-
"type": "string"
556-
}
557-
},
558-
"$ref": "https://json-everything.net/foo"
559-
}
560-
```
561-
562-
> This process requires that all external documents are registered or automatic resolution be enabled.
563-
{: .prompt-info } -->
506+
- how `SchemaRegistry.CreateBundle()` structures bundled output
507+
- which reference types are supported
508+
- setup requirements and a full example
564509

565510
# Customizing error messages {#schema-errors}
566511

_docs/schema/bundling.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
layout: page
3+
title: Bundling JSON Schemas
4+
bookmark: Bundling
5+
permalink: /schema/:title/
6+
icon: fas fa-tag
7+
order: "01.015"
8+
---
9+
Bundling creates a single schema document that contains a root schema and all schemas it references. This is useful when sharing schemas with other teams or shipping self-contained validation assets.
10+
11+
In _JsonSchema.Net_, bundling is performed by `SchemaRegistry.CreateBundle()`.
12+
13+
## How bundling works
14+
15+
`CreateBundle(rootUri, bundleUri, options)` will create a new Draft 2020-12 schema that:
16+
17+
- sets `$id` to `bundleUri`
18+
- sets top-level `$ref` to `rootUri`
19+
- adds a `$defs` keyword for the root schema as well as any schemas it references
20+
21+
The keys in `$defs` are the resolved reference URIs as strings.
22+
23+
The process traverses only standard `$ref` references. Dynamic and recursive references (`$dynamicRef`, `$recursiveRef`) are not supported for bundling and will throw `NotSupportedException`.
24+
25+
If a referenced document cannot be resolved, `RefResolutionException` is thrown.
26+
27+
If `rootUri` cannot be found in the registry, the method returns `null`.
28+
29+
## Prerequisites
30+
31+
Before bundling:
32+
33+
- ensure the root schema is available in a `SchemaRegistry`
34+
- ensure all referenced schemas are either:
35+
- pre-registered in that registry, or
36+
- resolvable via `SchemaRegistry.Fetch`
37+
38+
> Bundling does not infer schemas from arbitrary files. References must resolve through the registry.
39+
{: .prompt-info }
40+
41+
## Basic example
42+
43+
```c#
44+
_ = JsonSchema.FromText(
45+
"""
46+
{
47+
"$schema": "https://json-schema.org/draft/2020-12/schema",
48+
"$id": "https://schemas.example.com/person",
49+
"type": "object",
50+
"properties": {
51+
"address": { "$ref": "https://schemas.example.com/address" }
52+
}
53+
}
54+
""");
55+
56+
_ = JsonSchema.FromText(
57+
"""
58+
{
59+
"$schema": "https://json-schema.org/draft/2020-12/schema",
60+
"$id": "https://schemas.example.com/address",
61+
"type": "object",
62+
"properties": {
63+
"street": { "type": "string" },
64+
"postalCode": { "type": "string" }
65+
},
66+
"required": ["street", "postalCode"]
67+
}
68+
""");
69+
70+
var bundled = SchemaRegistry.Global.CreateBundle(
71+
new Uri("https://schemas.example.com/person"),
72+
new Uri("https://schemas.example.com/person.bundle")
73+
);
74+
75+
if (bundled is null)
76+
throw new InvalidOperationException("Root schema was not found.");
77+
```
78+
79+
The resulting bundle will have this shape:
80+
81+
```json
82+
{
83+
"$schema": "https://json-schema.org/draft/2020-12/schema",
84+
"$id": "https://schemas.example.com/person.bundle",
85+
"$ref": "https://schemas.example.com/person",
86+
"$defs": {
87+
"https://schemas.example.com/person": { "...": "root schema" },
88+
"https://schemas.example.com/address": { "...": "referenced schema" }
89+
}
90+
}
91+
```
92+
93+
## Build options
94+
95+
You can pass a `BuildOptions` instance as the third parameter when creating the bundle.
96+
97+
If `options` is omitted, `CreateBundle()` will use the global registry.
98+
99+
Use custom options when you need specific build behavior (for example, custom dialect/registry settings used by your environment).
100+
101+
## When to use bundling
102+
103+
Bundling is a good fit when:
104+
105+
- consumers need a single document instead of a graph of remote references
106+
- deployment environments have restricted network access
107+
- schema artifacts are versioned and distributed as build outputs
108+
109+
For interactive or frequently changing schema graphs, keeping references external can still be a better operational model.

0 commit comments

Comments
 (0)