forked from cvent/json-schema-deref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readme.hbs
116 lines (96 loc) · 2.37 KB
/
readme.hbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# json-schema-deref
Dereference JSON pointers in a JSON schemas with their true resolved values.
A lighter synchronous version of this module is available as [json-schema-deref-sync](https://github.com/bojand/json-schema-deref-sync),
but omits web references and custom loaders.
## Installation
`npm install json-schema-deref`
## Overview
Let's say you have the following JSON Schema:
```json
{
"description": "Just some JSON schema.",
"title": "Basic Widget",
"type": "object",
"definitions": {
"id": {
"description": "unique identifier",
"type": "string",
"minLength": 1,
"readOnly": true
}
},
"properties": {
"id": {
"$ref": "#/definitions/id"
},
"foo": {
"$ref": "http://www.mysite.com/myschema.json#/definitions/foo"
},
"bar": {
"$ref": "bar.json"
}
}
}
```
Sometimes you just want that schema to be fully expanded, with `$ref`'s being their (true) resolved values:
```json
{
"description": "Just some JSON schema.",
"title": "Basic Widget",
"type": "object",
"definitions": {
"id": {
"description": "unique identifier",
"type": "string",
"minLength": 1,
"readOnly": true
}
},
"properties": {
"id": {
"description": "unique identifier",
"type": "string",
"minLength": 1,
"readOnly": true
},
"foo": {
"description": "foo property",
"readOnly": true,
"type": "number"
},
"bar": {
"description": "bar property",
"type": "boolean"
}
}
}
```
This utility lets you do that:
```js
var deref = require('json-schema-deref');
var myschema = require('schema.json');
deref(myschema, function(err, fullSchema) {
console.dir(fullSchema); // has the full expanded $refs
});
```
## API Reference
{{>all-docs~}}
## Custom Loader
Let's say we want to get $ref's from a MongoDB database, and our `$ref` objects in the JSON Schema might be something like:
```json
"foo": {
"$ref":"mongodb:507c35dd8fada716c89d0013"
}
```
Our custom loader function passed in the `options` `loader` parameter would look something like:
```js
function myMongoDBLoader(ref, option, fn) {
if(ref.indexOf('mongodb:') === 0) {
var id = ref.substring(8);
return collection.findOne({_id:id}, fn);
}
// not ours, pass back nothing to keep it the same
// or pass error and use failOnMissing to abort
return fn();
}
```