You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ var parser = new $RefParser();
50
50
parser.bundle("my-schema.json");
51
51
```
52
52
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).
Copy file name to clipboardExpand all lines: docs/options.md
+83-16
Original file line number
Diff line number
Diff line change
@@ -3,32 +3,99 @@ Options
3
3
4
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
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.
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
+
-------------------
7
10
8
11
```javascript
9
12
$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
16
17
},
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:
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_.
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]
20
72
}
73
+
}
21
74
});
22
75
```
23
76
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`.
|`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
99
|`$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`.
33
100
|`cache.fs` |number |60 |<aname="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
34
101
|`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