Skip to content

Commit 652acbf

Browse files
Started updating the docs for the new options
1 parent 516a703 commit 652acbf

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ $RefParser.dereference(mySchema, function(err, schema) {
6767
console.error(err);
6868
}
6969
else {
70-
// `schema` is just a normal JavaScript object that contains your
71-
// entire JSON Schema - even if it spans multiple files
70+
// `schema` is just a normal JavaScript object that contains your entire JSON Schema,
71+
// including referenced files, combined into a single object
7272
console.log(schema.definitions.person.properties.firstName);
7373
}
7474
});

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var parser = new $RefParser();
5050
parser.bundle("my-schema.json");
5151
```
5252

53-
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).
53+
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#cache)**, 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).
5454

5555

5656
### Callbacks vs. Promises

docs/options.md

+83-16
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,99 @@ Options
33

44
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.
55

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.
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.
7+
8+
Example
9+
-------------------
710

811
```javascript
912
$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
13+
parse: {
14+
json: false, // Disable the JSON parser
15+
yaml: {
16+
empty: false // Don't allow empty YAML files
1617
},
17-
cache: {
18-
fs: 1, // Cache local files for 1 second
19-
http: 600 // Cache http URLs for 10 minutes
18+
text: {
19+
ext: [".txt", ".html"], // Parse .txt and .html files as plain text (strings)
20+
encoding: 'utf16' // Use UTF-16 encoding
21+
}
22+
},
23+
resolve: {
24+
file: false, // Don't resolve local file references
25+
http: {
26+
cache: 30000, // Cache downloaded files for 30 seconds
27+
timeout: 2000 // 2 second timeout
28+
}
29+
},
30+
dereference: {
31+
circular: false // Don't allow circular $refs
32+
}
33+
});
34+
```
35+
36+
37+
`parse` Options
38+
-------------------
39+
The `parse` options determine how different types of files will be parsed. JSON Schema $Ref Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add your own custom parsers if you want.
40+
41+
#### Disabling a parser
42+
To disable a parser, just set it to `false`, like this:
43+
44+
```javascript
45+
// Disable the JSON parser
46+
$RefParser.dereference("my-schema.yaml", { parse: { json: false } });
47+
```
48+
49+
#### `order`
50+
Parsers run in a specific order, relative to other parsers. For example, a parser with `order: 5` will run _before_ a parser with `order: 10`. If a parser is unable to successfully parse a file, then the next parser is tried, until one succeeds or they all fail.
51+
52+
You can change the order in which parsers run, which is useful if you know that most of your referenced files will be a certain type, or if you add your own custom parser that you want to run _first_.
53+
54+
```javascript
55+
// Run the plain-text parser first
56+
$RefParser.dereference("my-schema.yaml", { parse: { text: { order: 1 } } });
57+
```
58+
59+
#### `ext`
60+
Each parser has a list of file extensions that it will try to parse. For example, the plain-text parser will parse most common text files, such as `.txt`, `.html`, `.xml`, etc.
61+
62+
Multiple parsers can contain the same file extensions. For example, both the JSON parser _and_ the YAML parser will parse `.json` files. In this case, the JSON parser will run _first_, because it has `order: 100` and the YAML parser has `order: 200`.
63+
64+
You can set your own file extensions for any parser. Each extension can be a string or a regular expression to test against the _full file path_. Here's an example:
65+
66+
```javascript
67+
$RefParser.dereference("my-schema.yaml", {
68+
parse: {
69+
text: {
70+
// parse text, html, and readme files as plain-text
71+
ext: [".txt", ".html", /docs\/README/i]
2072
}
73+
}
2174
});
2275
```
2376

77+
#### `empty`
78+
All of the built-in parsers allow empty files by default. The JSON and YAML parsers will parse empty files as `null`. The text parser will parse empty files as an empty string. The binary parser will parse empty files as an empty byte array.
79+
80+
You can set `empty: false` on any parser, which will cause an error to be thrown if a file empty.
81+
82+
#### Parser-specific options
83+
Parsers can have other options that are specific to that parser. Currently, the only such option is `text.encoding`, which allows you to set the encoding for parsing text-based files. The default encoding is `utf8`.
84+
85+
#### Adding a custom parser
86+
TODO
87+
88+
89+
`resolve` Options
90+
-------------------
91+
92+
93+
`dereference` Options
94+
-------------------
95+
96+
2497
|Option |Type |Default |Description
2598
|:---------------------|:--------|:---------|:----------
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.
3299
|`$refs.circular` |bool or "ignore" |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.<br><br> If set to `"ignore"`, then circular references will _not_ be dereferenced, even when calling [`dereference()`](ref-parser.md#dereferenceschema-options-callback). No error will be thrown, but the [`$Refs.circular`](refs.md#circular) property will still be set to `true`.
33100
|`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.
34101
|`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.

0 commit comments

Comments
 (0)