Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

json-schema-ref-parser

APIDevTools3.3mMITdeprecated9.0.9TypeScript support: included

Please switch to @apidevtools/json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers

json, schema, jsonschema, json-schema, json-pointer, $ref, dereference, resolve

readme

JSON Schema $Ref Parser

Parse, Resolve, and Dereference JSON Schema $ref pointers

Build Status Coverage Status

npm Dependencies License Buy us a tree

OS and Browser Compatibility

The Problem:

You've got a JSON Schema with $ref pointers to other files and/or URLs. Maybe you know all the referenced files ahead of time. Maybe you don't. Maybe some are local files, and others are remote URLs. Maybe they are a mix of JSON and YAML format. Maybe some of the files contain cross-references to each other.

{
  "definitions": {
    "person": {
      // references an external file
      "$ref": "schemas/people/Bruce-Wayne.json"
    },
    "place": {
      // references a sub-schema in an external file
      "$ref": "schemas/places.yaml#/definitions/Gotham-City"
    },
    "thing": {
      // references a URL
      "$ref": "http://wayne-enterprises.com/things/batmobile"
    },
    "color": {
      // references a value in an external file via an internal reference
      "$ref": "#/definitions/thing/properties/colors/black-as-the-night"
    }
  }
}

The Solution:

JSON Schema $Ref Parser is a full JSON Reference and JSON Pointer implementation that crawls even the most complex JSON Schemas and gives you simple, straightforward JavaScript objects.

  • Use JSON or YAML schemas — or even a mix of both!
  • Supports $ref pointers to external files and URLs, as well as custom sources such as databases
  • Can bundle multiple files into a single schema that only has internal $ref pointers
  • Can dereference your schema, producing a plain-old JavaScript object that's easy to work with
  • Supports circular references, nested references, back-references, and cross-references between files
  • Maintains object reference equality — $ref pointers to the same value always resolve to the same object instance
  • Tested in Node v10, v12, & v14, and all major web browsers on Windows, Mac, and Linux

Example

$RefParser.dereference(mySchema, (err, schema) => {
  if (err) {
    console.error(err);
  }
  else {
    // `schema` is just a normal JavaScript object that contains your entire JSON Schema,
    // including referenced files, combined into a single object
    console.log(schema.definitions.person.properties.firstName);
  }
})

Or use async/await syntax instead. The following example is the same as above:

try {
  let schema = await $RefParser.dereference(mySchema);
  console.log(schema.definitions.person.properties.firstName);
}
catch(err) {
  console.error(err);
}

For more detailed examples, please see the API Documentation

Installation

Install using npm:

npm install @apidevtools/json-schema-ref-parser

Usage

When using JSON Schema $Ref Parser in Node.js apps, you'll probably want to use CommonJS syntax:

const $RefParser = require("@apidevtools/json-schema-ref-parser");

When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:

import $RefParser from "@apidevtools/json-schema-ref-parser";

Browser support

JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.

To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

API Documentation

Full API documentation is available right here

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/json-schema-ref-parser.git

  2. Install dependencies
    npm install

  3. Run the tests
    npm test

License

JSON Schema $Ref Parser is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

Stoplight SauceLabs Coveralls

changelog

Change Log

See GitHub Releases for more information on newer releases.

v9.0.0 (2020-04-21)

Breaking Changes

Other Changes

Full Changelog

v8.0.0 (2020-03-13)

  • Moved JSON Schema $Ref Parser to the @APIDevTools scope on NPM

  • The "json-schema-ref-parser" NPM package is now just a wrapper around the scoped "@apidevtools/json-schema-ref-parser" package

Full Changelog

v7.1.0 (2019-06-21)

Full Changelog

v7.0.0 (2019-06-11)

  • Dropped support for Node 6

  • Updated all code to ES6+ syntax (async/await, template literals, arrow functions, etc.)

  • No longer including a pre-built bundle in the package. such as Webpack, Rollup, Parcel, or Browserify to include JSON Schema $Ref Parser in your app

Full Changelog

v6.0.0 (2018-10-04)

  • Dropped support for Bower, since it has been deprecated

  • Removed the debug dependency

Full Changelog

v5.1.0 (2018-07-11)

  • Improved the logic of the bundle() method to produce shorter reference paths when possible. This is not a breaking change, since both the old reference paths and the new reference paths are valid. The new ones are just shorter. Big thanks to @hipstersmoothie for PR #68, which helped a lot with this.

Full Changelog

v5.0.0 (2018-03-18)

This release contains two bug fixes related to file paths. They are technically breaking changes — hence the major version bump — but they're both edge cases that probably won't affect most users.

  • Fixed a bug in the $refs.paths() and $refs.values() methods that caused the path of the root schema file to always be represented as a URL, rather than a filesystem path (see this commit)

  • Merged PR #75, which resolves issue #76. Error messages no longer include the current working directory path when there is no file path.

Full Changelog

v4.1.0 (2018-01-17)

  • Updated dependencies

  • Improved the bundle() algorithm to favor direct references rather than indirect references (see PR #62 for details). This will produce different bundled output than previous versions for some schemas. Both the old output and the new output are valid, but the new output is arguably better.

Full Changelog

v4.0.0 (2017-10-13)

Breaking Changes

Minor Changes

Full Changelog

v3.3.0 (2017-08-09)

  • Updated dependencies

  • PR #30 - Added a browser field to the package.json file to support bundlers such as Browserify, Rollup, and Webpack

  • PR #45 - Implemented a temporary workaround for issue #42. JSON Schema $Ref Parser does not currently support named internal references, but support will be added in the next major release.

Full Changelog

v3.0.0 (2016-04-03)

Plug-ins !!!

That's the major new feature in this version. Originally requested in PR #8, and refined a few times over the past few months, the plug-in API is now finalized and ready to use. You can now define your own resolvers and parsers.

Breaking Changes

The available options have changed, mostly due to the new plug-in API. There's not a one-to-one mapping of old options to new options, so you'll have to read the docs and determine which options you need to set. If any. The out-of-the-box configuration works for most people.

All of the caching options have been removed. Instead, all files are now cached, and the entire cache is reset for each new parse operation. Caching options may come back in a future release, if there is enough demand for it. If you used the old caching options, please open an issue and explain your use-case and requirements. I need a better understanding of what caching functionality is actually needed by users.

Bug Fixes

Lots of little bug fixes. The only major bug fix is to support root-level $refs

Full Changelog

v2.2.0 (2016-01-03)

This version includes a complete rewrite of the bundle method method, mostly to fix this bug, but also to address a few edge-cases that weren't handled before. As a side-effect of this rewrite, there was also some pretty significant refactoring and code-cleanup done throughout the codebase.

Despite the significant code changes, there were no changes to any public-facing APIs, and all tests are passing as expected.

Full Changelog

v2.1.0 (2015-12-31)

JSON Schema $Ref Parser now automatically follows HTTP redirects. This is especially great for servers that automatically "ugrade" your connection from HTTP to HTTPS via a 301 redirect. Now that won't break your code.

There are a few new options that allow you to set the number of redirects (default is 5) and a few other HTTP request properties.

Full Changelog

v2.0.0 (2015-12-31)

Bumping the major version number because this change technically breaks backward-compatibility — although I doubt it will actually affect many people. Basically, if you're using JSON Schema $Ref Parser to download files from a CORS-enabled server that requires authentication, then you'll need to set the http.withCredentials option to true.

$RefParser.dereference('http://some.server.com/file.json', {
    http: { withCredentials: true }
});

Full Changelog