When you call the resolve
method, the value that gets passed to the callback function (or Promise) is a $Refs
object. This same object is accessible via the parser.$refs
property of $RefParser
objects.
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.
- Type:
boolean
This property is true
if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify()
does not support circular references by default.
var parser = new $RefParser();
parser.dereference("my-schema.json")
.then(function() {
if (parser.$refs.circular) {
console.log('The schema contains circular references');
}
});
-
types (optional) -
string
(one or more)
Optionally only return certain types of paths ("file", "http", etc.) -
Return Value:
array
ofstring
Returns the paths/URLs of all the files in your schema (including the main schema file).
$RefParser.resolve("my-schema.json")
.then(function($refs) {
// Get the paths of ALL files in the schema
$refs.paths();
// Get the paths of local files only
$refs.paths("file");
// Get all URLs
$refs.paths("http");
});
-
types (optional) -
string
(one or more)
Optionally only return values from certain locations ("file", "http", etc.) -
Return Value:
object
Returns a map of paths/URLs and their correspond values.
$RefParser.resolve("my-schema.json")
.then(function($refs) {
// Get ALL paths & values in the schema
// (this is the same as $refs.toJSON())
var values = $refs.values();
values["schemas/people/Bruce-Wayne.json"];
values["schemas/places.yaml"];
values["http://wayne-enterprises.com/things/batmobile"];
});
-
$ref (required) -
string
The JSON Reference path, optionally with a JSON Pointer in the hash -
Return Value:
boolean
Returnstrue
if the given path exists in the schema; otherwise, returnsfalse
$RefParser.resolve("my-schema.json")
.then(function($refs) {
$refs.exists("schemas/places.yaml#/definitions/Gotham-City"); // => true
$refs.exists("schemas/places.yaml#/definitions/Metropolis"); // => false
});
-
$ref (required) -
string
The JSON Reference path, optionally with a JSON Pointer in the hash -
Return Value:
boolean
Gets the value at the given path in the schema. Throws an error if the path does not exist.
$RefParser.resolve("my-schema.json")
.then(function($refs) {
var value = $refs.get("schemas/people/Bruce-Wayne.json#/properties/address");
});
-
$ref (required) -
string
The JSON Reference path, optionally with a JSON Pointer in the hash -
value (required)
The value to assign. Can be anything (object, string, number, etc.)
Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
$RefParser.resolve("my-schema.json")
.then(function($refs) {
$refs.set("schemas/people/Bruce-Wayne.json#/properties/favoriteColor/default", "black");
});