|
| 1 | +`$RefParser` class |
| 2 | +========================== |
| 3 | + |
| 4 | +This is the default export of JSON Schema $Ref Parser. You can creates instances of this class using `new $RefParser()`, or you can just call its [static methods](README.md#class-methods-vs-instance-methods). |
| 5 | + |
| 6 | +##### Properties |
| 7 | +- [`schema`](#schema) |
| 8 | +- [`$refs`](#refs) |
| 9 | + |
| 10 | +##### Methods |
| 11 | +- [`dereference()`](#dereferencepath-options-callback) |
| 12 | +- [`bundle()`](#bundlepath-options-callback) |
| 13 | +- [`parse()`](#parsepath-options-callback) |
| 14 | +- [`resolve()`](#resolvepath-options-callback) |
| 15 | + |
| 16 | + |
| 17 | +### `Schema` |
| 18 | +The `schema` property is the parsed/bundled/dereferenced JSON Schema object. This is the same value that is passed to the callback function (or Promise) when calling the [`parse`](#parseschema-options-callback), [`bundle`](#bundleschema-options-callback), or [`dereference`](#dereferenceschema-options-callback) methods. |
| 19 | + |
| 20 | +```javascript |
| 21 | +var parser = new $RefParser(); |
| 22 | + |
| 23 | +parser.schema; // => null |
| 24 | + |
| 25 | +parser.dereference("my-schema.json") |
| 26 | + .then(function(schema) { |
| 27 | + typeof parser.schema; // => "object" |
| 28 | + |
| 29 | + schema === parser.schema; // => true |
| 30 | + }); |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +### `$refs` |
| 35 | +The `$refs` property is a [`$Refs`](refs.md) object, which lets you access all of the externally-referenced files in the schema, as well as easily get and set specific values in the schema using JSON pointers. |
| 36 | + |
| 37 | +This is the same value that is passed to the callback function (or Promise) when calling the [`resolve`](#resolveschema-options-callback) method. |
| 38 | + |
| 39 | +```javascript |
| 40 | +var parser = new $RefParser(); |
| 41 | + |
| 42 | +parser.$refs.paths(); // => [] empty array |
| 43 | + |
| 44 | +parser.dereference("my-schema.json") |
| 45 | + .then(function() { |
| 46 | + parser.$refs.paths(); // => ["my-schema.json"] |
| 47 | + }); |
| 48 | +``` |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +### `dereference(schema, [options], [callback])` |
| 53 | + |
| 54 | +- **schema** (_required_) - `string` or `object`<br> |
| 55 | +A JSON Schema object, or the file path or URL of a JSON Schema file. See the [`parse`](#parseschema-options-callback) method for more info. |
| 56 | + |
| 57 | +- **options** (_optional_) - `object`<br> |
| 58 | +See [options](options.md) for the full list of options |
| 59 | + |
| 60 | +- **callback** (_optional_) - `function(err, schema)`<br> |
| 61 | +A callback that will receive the dereferenced schema object |
| 62 | + |
| 63 | +- **Return Value:** `Promise`<br> |
| 64 | +See [Callbacks vs. Promises](README.md#callbacks-vs-promises) |
| 65 | + |
| 66 | +Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain _any_ `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references. |
| 67 | + |
| 68 | +The `dereference` method maintains object reference equality, meaning that all `$ref` pointers that point to the same object will be replaced with references to the same object. Again, this is great for programmatic usage, but it does introduce the risk of [circular references](README.md#circular-refs), so be careful if you intend to serialize the schema using [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). Consider using the [`bundle`](#bundlepath-options-callback) method instead, which does not create circular references. |
| 69 | + |
| 70 | +```javascript |
| 71 | +$RefParser.dereference("my-schema.yaml") |
| 72 | + .then(function(schema) { |
| 73 | + // The `schema` object is a normal JavaScript object, |
| 74 | + // so you can easily access any part of the schema using simple dot notation |
| 75 | + console.log(schema.definitions.person.properties.firstName); // => {type: "string"} |
| 76 | + |
| 77 | + // Object reference equality works as expected |
| 78 | + schema.definitions.thing === schema.definitions.batmobile; // => true |
| 79 | + }); |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +### `bundle(schema, [options], [callback])` |
| 84 | + |
| 85 | +- **schema** (_required_) - `string` or `object`<br> |
| 86 | +A JSON Schema object, or the file path or URL of a JSON Schema file. See the [`parse`](#parseschema-options-callback) method for more info. |
| 87 | + |
| 88 | +- **options** (_optional_) - `object`<br> |
| 89 | +See [options](options.md) for the full list of options |
| 90 | + |
| 91 | +- **callback** (_optional_) - `function(err, schema)`<br> |
| 92 | +A callback that will receive the bundled schema object |
| 93 | + |
| 94 | +- **Return Value:** `Promise`<br> |
| 95 | +See [Callbacks vs. Promises](README.md#callbacks-vs-promises) |
| 96 | + |
| 97 | +Bundles all referenced files/URLs into a single schema that only has _internal_ `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain _internal_ JSON references rather than being [fully-dereferenced](#dereferencepath-options-callback). |
| 98 | + |
| 99 | +This also eliminates the risk of [circular references](README.md#circular-refs), so the schema can be safely serialized using [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify). |
| 100 | + |
| 101 | +```javascript |
| 102 | +$RefParser.bundle("my-schema.yaml") |
| 103 | + .then(function(schema) { |
| 104 | + console.log(schema.definitions.person); // => {$ref: "#/definitions/schemas~1people~1Bruce-Wayne.json"} |
| 105 | + }); |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | +### `parse(schema, [options], [callback])` |
| 110 | + |
| 111 | +- **schema** (_required_) - `string` or `object`<br> |
| 112 | +A JSON Schema object, or the file path or URL of a JSON Schema file. |
| 113 | +<br><br> |
| 114 | +The path can be absolute or relative. In Node, the path is relative to `process.cwd()`. In the browser, it's relative to the URL of the page. |
| 115 | + |
| 116 | +- **options** (_optional_) - `object`<br> |
| 117 | +See [options](options.md) for the full list of options |
| 118 | + |
| 119 | +- **callback** (_optional_) - `function(err, schema)`<br> |
| 120 | +A callback that will receive the parsed schema object, or an error |
| 121 | + |
| 122 | +- **Return Value:** `Promise`<br> |
| 123 | +See [Callbacks vs. Promises](README.md#callbacks-vs-promises) |
| 124 | + |
| 125 | +> This method is used internally by other methods, such as [`bundle`](#bundlepath-options-callback) and [`dereference`](#dereferencepath-options-callback). You probably won't need to call this method yourself. |
| 126 | +
|
| 127 | +Parses the given JSON Schema file (in JSON or YAML format), and returns it as a JavaScript object. This method **does not** resolve `$ref` pointers or dereference anything. It simply parses _one_ file and returns it. |
| 128 | + |
| 129 | +```javascript |
| 130 | +$RefParser.parse("my-schema.yaml") |
| 131 | + .then(function(schema) { |
| 132 | + console.log(schema.definitions.person); // => {$ref: "schemas/people/Bruce-Wayne.json"} |
| 133 | + }); |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +### `resolve(schema, [options], [callback])` |
| 138 | + |
| 139 | +- **path** (_required_) - `string` or `object`<br> |
| 140 | +A JSON Schema object, or the file path or URL of a JSON Schema file. See the [`parse`](#parseschema-options-callback) method for more info. |
| 141 | + |
| 142 | +- **options** (_optional_) - `object`<br> |
| 143 | +See [options](options.md) for the full list of options |
| 144 | + |
| 145 | +- **callback** (_optional_) - `function(err, $refs)`<br> |
| 146 | +A callback that will receive a [`$Refs`](refs.md) object |
| 147 | + |
| 148 | +- **Return Value:** `Promise`<br> |
| 149 | +See [Callbacks vs. Promises](README.md#callbacks-vs-promises) |
| 150 | + |
| 151 | +> This method is used internally by other methods, such as [`bundle`](#bundleschema-options-callback) and [`dereference`](#dereferenceschema-options-callback). You probably won't need to call this method yourself. |
| 152 | +
|
| 153 | +Resolves all JSON references (`$ref` pointers) in the given JSON Schema file. If it references any other files/URLs, then they will be downloaded and resolved as well (unless `options.$refs.external` is false). This method **does not** dereference anything. It simply gives you a [`$Refs`](refs.md) object, which is a map of all the resolved references and their values. |
| 154 | + |
| 155 | +```javascript |
| 156 | +$RefParser.resolve("my-schema.yaml") |
| 157 | + .then(function($refs) { |
| 158 | + // $refs.paths() returns the paths of all the files in your schema |
| 159 | + var filePaths = $refs.paths(); |
| 160 | + |
| 161 | + // $refs.get() lets you query parts of your schema |
| 162 | + var name = $refs.get("schemas/people/Bruce-Wayne.json#/properties/name"); |
| 163 | + |
| 164 | + // $refs.set() lets you change parts of your schema |
| 165 | + $refs.set("schemas/people/Bruce-Wayne.json#/properties/favoriteColor/default", "black"); |
| 166 | + }); |
| 167 | +``` |
0 commit comments