Skip to content

Commit fa9600a

Browse files
- Added an allow.circular option to control whether circular references are allowed
- Added a `$refs.circular` property to detect when a schema contains circular references
1 parent 4308e0c commit fa9600a

12 files changed

Lines changed: 12983 additions & 12770 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ $RefParser.dereference("my-schema.yaml", {
289289
|`allow.yaml` |bool |true |Determines whether YAML files are supported<br> (note: all JSON files are also valid YAML files)
290290
|`allow.empty` |bool |true |Determines whether it's ok for a `$ref` pointer to point to an empty file
291291
|`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)
292+
|`allow.circular` |bool |true |Determines whether [circular references](#circular-refs) are allowed. If `false`, then a `ReferenceError` will be thrown if the schema contains a circular reference.
292293
|`$refs.internal` |bool |true |Determines whether internal `$ref` pointers (such as `#/definitions/widget`) will be dereferenced when calling [`dereference()`](#dereferencepath-options-callback). Either way, you'll still be able to get the value using [`$Refs.get()`](#refsgetref-options)
293294
|`$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.
294295
|`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.
@@ -318,6 +319,24 @@ When you call the [`resolve`](#resolvepath-options-callback) method, the value t
318319

319320
This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
320321

322+
323+
### `$Refs.circular`
324+
325+
- **Type:** `boolean`
326+
327+
This property is `true` if the schema contains any [circular references](#circular-refs). You may want to check this property before serializing the dereferenced schema as JSON, since [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default.
328+
329+
```javascript
330+
var parser = new $RefParser();
331+
parser.dereference("my-schema.json")
332+
.then(function() {
333+
if (parser.$refs.circular) {
334+
console.log('The schema contains circular references');
335+
}
336+
});
337+
```
338+
339+
321340
### `$Refs.paths([types])`
322341

323342
- **types** (_optional_) - `string` (one or more)<br>
@@ -569,6 +588,8 @@ Circular $Refs
569588
--------------------------
570589
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).
571590

591+
You can disable circular references by setting the [`allow.circular`](#options) option to `false`. Then, if a circular reference is found, a `ReferenceError` will be thrown.
592+
572593
Another option is to use the [`bundle`](#bundlepath-options-callback) method rather than the [`dereference`](#dereferencepath-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_ `$ref` pointers to _internal_ ones.
573594

574595
```javascript

0 commit comments

Comments
 (0)