Skip to content

Commit 45fadf3

Browse files
Moved the API docs out of the root ReadMe file and into a "docs" folder
1 parent 4d1c06f commit 45fadf3

File tree

8 files changed

+553
-487
lines changed

8 files changed

+553
-487
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ npm-debug.log
55
/.idea
66
/coverage
77
/dist/*.test.js
8+
/docs
89
karma.conf.js
910
/tests
1011
/www

README.md

Lines changed: 7 additions & 487 deletions
Large diffs are not rendered by default.

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"ignore": [
1919
"/bower_components",
20+
"/docs",
2021
"/lib",
2122
"/www",
2223
"/dist/*.test.js",

docs/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
JSON Schema $Ref Parser API
2+
==========================
3+
4+
### [`$RefParser`](ref-parser.md)
5+
- [`schema`](ref-parser.md#schema)
6+
- [`$refs`](ref-parser.md#refs)
7+
- [`dereference()`](ref-parser.md#dereferencepath-options-callback)
8+
- [`bundle()`](ref-parser.md#bundlepath-options-callback)
9+
- [`parse()`](ref-parser.md#parsepath-options-callback)
10+
- [`resolve()`](ref-parser.md#resolvepath-options-callback)
11+
12+
### [`$Refs`](refs.md)
13+
- [`circular`](refs.md#circular)
14+
- [`paths()`](refs.md#pathstypes)
15+
- [`values()`](refs.md#valuestypes)
16+
- [`isExpired()`](refs.md#isexpiredref)
17+
- [`expire()`](refs.md#expireref)
18+
- [`exists()`](refs.md#existsref)
19+
- [`get()`](refs.md#getref-options)
20+
- [`set()`](refs.md#setref-value-options)
21+
22+
### [`YAML`](yaml.md)
23+
- [`parse()`](yaml.md#parsetext)
24+
- [`stringify()`](yaml.md#stringifyvalue)
25+
26+
### [`Options`](options.md)
27+
28+
29+
Topics
30+
---------------------
31+
- [Class methods vs. Instance methods](#class-methods-vs-instance-methods)
32+
- [Callbacks vs. Promises](#callbacks-vs-promises)
33+
- [Circular references](#circular-refs)
34+
35+
36+
### Class methods vs. Instance methods
37+
All of JSON Schema $Ref Parser's methods are available as static (class) methods, and as instance methods. The static methods simply create a new [`$RefParser`](ref-parser.md) instance and then call the corresponding instance method. Thus, the following line...
38+
39+
```javascript
40+
$RefParser.resolve("my-schema.json");
41+
```
42+
43+
... is the same as this:
44+
45+
```javascript
46+
var parser = new $RefParser();
47+
parser.resolve("my-schema.json");
48+
```
49+
50+
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function. Also, having a `$RefParser` instance allows you to benefit from **[caching](options.md#caching)**, so the next time you call [`parser.resolve()`](ref-parser.md#resolveschema-options-callback), it won't need to re-download those files again (as long as the cache hasn't expired).
51+
52+
53+
### Callbacks vs. Promises
54+
Many people prefer [ES6 Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer. Every method accepts an optional callback _and_ returns a Promise. So pick your poison.
55+
56+
57+
Circular $Refs
58+
--------------------------
59+
JSON Schema files can contain [circular $ref pointers](https://gist.github.com/BigstickCarpet/d18278935fc73e3a0ee1), and JSON Schema $Ref Parser fully supports them. Circular references can be resolved and dereferenced just like any other reference. However, if you intend to serialize the dereferenced schema as JSON, then you should be aware that [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default, so you will need to [use a custom replacer function](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json).
60+
61+
You can disable circular references by setting the [`$refs.circular`](options.md) option to `false`. Then, if a circular reference is found, a `ReferenceError` will be thrown.
62+
63+
Another option is to use the [`bundle`](ref-parser.md#bundleschema-options-callback) method rather than the [`dereference`](ref-parser.md#dereferenceschema-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_ `$ref` pointers to _internal_ ones.
64+
65+
```javascript
66+
"person": {
67+
"properties": {
68+
"name": {
69+
"type": "string"
70+
},
71+
"spouse": {
72+
"type": {
73+
"$ref": "#/person" // circular reference
74+
}
75+
}
76+
}
77+
}
78+
```

docs/options.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Options
2+
==========================
3+
4+
All [`$RefParser`](ref-parser.md) methods accept an optional `options` parameter, which you can use to customize how the JSON Schema is parsed, resolved, dereferenced, etc.
5+
6+
If you pass an options parameter, you _don't_ need to specify _every_ option. Any options you don't specify will use their default values, as shown below.
7+
8+
```javascript
9+
$RefParser.dereference("my-schema.yaml", {
10+
allow: {
11+
json: false, // Don't allow JSON files
12+
empty: false // Don't allow empty files
13+
},
14+
$refs: {
15+
internal: false // Don't dereference internal $refs, only external
16+
},
17+
cache: {
18+
fs: 1, // Cache local files for 1 second
19+
http: 600 // Cache http URLs for 10 minutes
20+
}
21+
});
22+
```
23+
24+
|Option |Type |Default |Description
25+
|:----------------|:--------|:---------|:----------
26+
|`allow.json` |bool |true |Determines whether JSON files are supported
27+
|`allow.yaml` |bool |true |Determines whether YAML files are supported<br> (note: all JSON files are also valid YAML files)
28+
|`allow.empty` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an empty file
29+
|`allow.unknown` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an unknown/unsupported file type (such as HTML, text, image, etc.). The default is to resolve unknown files as a [`Buffer`](https://nodejs.org/api/buffer.html#buffer_class_buffer)
30+
|`$refs.internal` |bool |true |Determines whether internal `$ref` pointers (such as `#/definitions/widget`) will be dereferenced when calling [`dereference()`](ref-parser.md#dereferenceschema-options-callback). Either way, you'll still be able to get the value using [`$Refs.get()`](refs.md#getref-options)
31+
|`$refs.external` |bool |true |Determines whether external `$ref` pointers get resolved/dereferenced. If `false`, then no files/URLs will be retrieved. Use this if you only want to allow single-file schemas.
32+
|`$refs.circular` |bool |true |Determines whether [circular `$ref` pointers](README.md#circular-refs) are allowed. If `false`, then a `ReferenceError` will be thrown if the schema contains a circular reference.
33+
|`cache.fs` |number |60 |<a name="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
34+
|`cache.http` |number |300 |The length of time (in seconds) to cache HTTP URLs. The default is five minutes. Setting to zero will cache forever.
35+
|`cache.https` |number |300 |The length of time (in seconds) to cache HTTPS URLs. The default is five minutes. Setting to zero will cache forever.

docs/ref-parser.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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

Comments
 (0)