JSON Schema $Ref Parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers

The $Refs class

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.

Properties
Methods

circular

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.

let parser = new $RefParser();
await parser.dereference("my-schema.json");

if (parser.$refs.circular) {
  console.log('The schema contains circular references');
}

paths([types])

let $refs = await $RefParser.resolve("my-schema.json");

// 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");

values([types])

let $refs = await $RefParser.resolve("my-schema.json");

// Get ALL paths & values in the schema
// (this is the same as $refs.toJSON())
let values = $refs.values();

values["schemas/people/Bruce-Wayne.json"];
values["schemas/places.yaml"];
values["http://wayne-enterprises.com/things/batmobile"];

exists($ref)

let $refs = await $RefParser.resolve("my-schema.json");

$refs.exists("schemas/places.yaml#/definitions/Gotham-City"); // => true
$refs.exists("schemas/places.yaml#/definitions/Metropolis");  // => false

get($ref)

let $refs = await $RefParser.resolve("my-schema.json");
let value = $refs.get("schemas/people/Bruce-Wayne.json#/properties/address");

set($ref, value)

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.

let $refs = await $RefParser.resolve("my-schema.json");
$refs.set("schemas/people/Bruce-Wayne.json#/properties/favoriteColor/default", "black");