|
| 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