UNPKG

6.27 kBMarkdownView Raw
1JSON Schema $Ref Parser
2============================
3#### Parse, Resolve, and Dereference JSON Schema $ref pointers
4
5[![Build Status](https://api.travis-ci.com/APIDevTools/json-schema-ref-parser.svg?branch=master)](https://travis-ci.com/APIDevTools/json-schema-ref-parser)
6[![Coverage Status](https://coveralls.io/repos/github/APIDevTools/json-schema-ref-parser/badge.svg?branch=master)](https://coveralls.io/github/APIDevTools/json-schema-ref-parser)
7
8[![npm](https://img.shields.io/npm/v/json-schema-ref-parser.svg)](https://www.npmjs.com/package/json-schema-ref-parser)
9[![Dependencies](https://david-dm.org/APIDevTools/json-schema-ref-parser.svg)](https://david-dm.org/APIDevTools/json-schema-ref-parser)
10[![License](https://img.shields.io/npm/l/json-schema-ref-parser.svg)](LICENSE)
11
12
13[![OS and Browser Compatibility](https://apidevtools.org/img/badges/ci-badges-with-ie.svg)](https://travis-ci.com/APIDevTools/json-schema-ref-parser)
14
15
16The Problem:
17--------------------------
18You'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.
19
20```javascript
21{
22 "definitions": {
23 "person": {
24 // references an external file
25 "$ref": "schemas/people/Bruce-Wayne.json"
26 },
27 "place": {
28 // references a sub-schema in an external file
29 "$ref": "schemas/places.yaml#/definitions/Gotham-City"
30 },
31 "thing": {
32 // references a URL
33 "$ref": "http://wayne-enterprises.com/things/batmobile"
34 },
35 "color": {
36 // references a value in an external file via an internal reference
37 "$ref": "#/definitions/thing/properties/colors/black-as-the-night"
38 }
39 }
40}
41```
42
43
44The Solution:
45--------------------------
46JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03) and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward JavaScript objects.
47
48- Use **JSON** or **YAML** schemas — or even a mix of both!
49- Supports `$ref` pointers to external files and URLs, as well as [custom sources](https://apidevtools.org/json-schema-ref-parser/docs/plugins/resolvers.html) such as databases
50- Can [bundle](https://apidevtools.org/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) multiple files into a single schema that only has _internal_ `$ref` pointers
51- Can [dereference](https://apidevtools.org/json-schema-ref-parser/docs/ref-parser.html#dereferencepath-options-callback) your schema, producing a plain-old JavaScript object that's easy to work with
52- Supports [circular references](https://apidevtools.org/json-schema-ref-parser/docs/#circular-refs), nested references, back-references, and cross-references between files
53- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object instance
54- [Tested](https://apidevtools.org/json-schema-ref-parser/test/) in Node, io.js, and all major web browsers on Windows, Mac, and Linux
55
56
57Example
58--------------------------
59
60```javascript
61$RefParser.dereference(mySchema, function(err, schema) {
62 if (err) {
63 console.error(err);
64 }
65 else {
66 // `schema` is just a normal JavaScript object that contains your entire JSON Schema,
67 // including referenced files, combined into a single object
68 console.log(schema.definitions.person.properties.firstName);
69 }
70});
71```
72
73Or use [Promises syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead. The following example is the same as above:
74
75```javascript
76$RefParser.dereference(mySchema)
77 .then(function(schema) {
78 console.log(schema.definitions.person.properties.firstName);
79 })
80 .catch(function(err) {
81 console.error(err);
82 });
83```
84
85For more detailed examples, please see the [API Documentation](https://apidevtools.org/json-schema-ref-parser/docs/)
86
87
88Installation
89--------------------------
90#### Node
91Install using [npm](https://docs.npmjs.com/about-npm/):
92
93```bash
94npm install json-schema-ref-parser
95```
96
97Then require it in your code:
98
99```javascript
100var $RefParser = require('json-schema-ref-parser');
101```
102
103#### Web Browsers
104Reference [`ref-parser.js`](dist/ref-parser.js) or [`ref-parser.min.js`](dist/ref-parser.min.js) in your HTML:
105
106```html
107<script src="https://unpkg.com/json-schema-ref-parser/dist/ref-parser.min.js"></script>
108<script>
109 $RefParser.dereference(mySchema)
110 .then(function(schema) {
111 console.log(schema.definitions.person.properties.firstName);
112 })
113 .catch(function(err) {
114 console.error(err);
115 });
116</script>
117```
118
119
120API Documentation
121--------------------------
122Full API documentation is available [right here](https://apidevtools.org/json-schema-ref-parser/docs/)
123
124
125Contributing
126--------------------------
127I welcome any contributions, enhancements, and bug-fixes. [File an issue](https://github.com/APIDevTools/json-schema-ref-parser/issues) on GitHub and [submit a pull request](https://github.com/APIDevTools/json-schema-ref-parser/pulls).
128
129#### Building/Testing
130To build/test the project locally on your computer:
131
1321. __Clone this repo__<br>
133`git clone https://github.com/APIDevTools/json-schema-ref-parser.git`
134
1352. __Install dependencies__<br>
136`npm install`
137
1383. __Run the build script__<br>
139`npm run build`
140
1414. __Run the tests__<br>
142`npm test`
143
1445. __Start the local web server__<br>
145`npm start` (then browse to [http://localhost:8080/test/](http://localhost:8080/test/))
146
147
148License
149--------------------------
150JSON Schema $Ref Parser is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
151
152Big Thanks To
153--------------------------
154Thanks to these awesome companies for their support of Open Source developers ❤
155
156[![Travis CI](https://jsdevtools.org/img/badges/travis-ci.svg)](https://travis-ci.com)
157[![SauceLabs](https://jsdevtools.org/img/badges/sauce-labs.svg)](https://saucelabs.com)
158[![Coveralls](https://jsdevtools.org/img/badges/coveralls.svg)](https://coveralls.io)