@apidevtools/json-schema-ref-parser 10.0.0 → 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/bundle.d.ts +10 -0
- package/dist/lib/bundle.js +265 -0
- package/dist/lib/dereference.d.ts +9 -0
- package/dist/lib/dereference.js +206 -0
- package/dist/lib/index.d.ts +206 -0
- package/dist/lib/index.js +223 -0
- package/dist/lib/normalize-args.d.ts +10 -0
- package/dist/lib/normalize-args.js +42 -0
- package/dist/lib/options.d.ts +77 -0
- package/dist/lib/options.js +119 -0
- package/dist/lib/parse.d.ts +8 -0
- package/dist/lib/parse.js +177 -0
- package/dist/lib/parsers/binary.d.ts +3 -0
- package/dist/lib/parsers/binary.js +35 -0
- package/dist/lib/parsers/json.d.ts +3 -0
- package/dist/lib/parsers/json.js +57 -0
- package/dist/lib/parsers/text.d.ts +3 -0
- package/dist/lib/parsers/text.js +42 -0
- package/dist/lib/parsers/yaml.d.ts +3 -0
- package/dist/lib/parsers/yaml.js +65 -0
- package/dist/lib/pointer.d.ts +87 -0
- package/dist/lib/pointer.js +256 -0
- package/dist/lib/ref.d.ts +181 -0
- package/dist/lib/ref.js +239 -0
- package/dist/lib/refs.d.ts +127 -0
- package/dist/lib/refs.js +223 -0
- package/dist/lib/resolve-external.d.ts +14 -0
- package/dist/lib/resolve-external.js +148 -0
- package/dist/lib/resolvers/file.d.ts +3 -0
- package/dist/lib/resolvers/file.js +76 -0
- package/dist/lib/resolvers/http.d.ts +3 -0
- package/dist/lib/resolvers/http.js +159 -0
- package/dist/lib/types/index.d.ts +104 -0
- package/dist/lib/types/index.js +2 -0
- package/dist/lib/util/errors.d.ts +50 -0
- package/dist/lib/util/errors.js +106 -0
- package/dist/lib/util/maybe.d.ts +3 -0
- package/dist/lib/util/maybe.js +24 -0
- package/dist/lib/util/next.d.ts +2 -0
- package/dist/lib/util/next.js +16 -0
- package/dist/lib/util/plugins.d.ts +36 -0
- package/dist/lib/util/plugins.js +144 -0
- package/dist/lib/util/url.d.ts +93 -0
- package/{cjs → dist/lib}/util/url.js +134 -102
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.js +23 -0
- package/lib/{bundle.js → bundle.ts} +105 -101
- package/lib/{dereference.js → dereference.ts} +113 -52
- package/lib/index.ts +413 -0
- package/lib/{normalize-args.js → normalize-args.ts} +7 -14
- package/lib/options.ts +202 -0
- package/lib/parse.ts +153 -0
- package/lib/parsers/binary.ts +39 -0
- package/lib/parsers/{json.js → json.ts} +9 -22
- package/lib/parsers/text.ts +46 -0
- package/lib/parsers/{yaml.js → yaml.ts} +15 -19
- package/lib/pointer.ts +296 -0
- package/lib/ref.ts +288 -0
- package/lib/refs.ts +238 -0
- package/lib/{resolve-external.js → resolve-external.ts} +39 -36
- package/lib/resolvers/file.ts +40 -0
- package/lib/resolvers/http.ts +136 -0
- package/lib/tsconfig.json +103 -0
- package/lib/types/index.ts +135 -0
- package/lib/util/errors.ts +141 -0
- package/lib/util/maybe.ts +22 -0
- package/lib/util/next.ts +13 -0
- package/lib/util/{plugins.js → plugins.ts} +58 -57
- package/lib/util/{url.js → url.ts} +64 -83
- package/package.json +44 -45
- package/cjs/bundle.js +0 -304
- package/cjs/dereference.js +0 -258
- package/cjs/index.js +0 -603
- package/cjs/normalize-args.js +0 -64
- package/cjs/options.js +0 -125
- package/cjs/package.json +0 -3
- package/cjs/parse.js +0 -338
- package/cjs/parsers/binary.js +0 -54
- package/cjs/parsers/json.js +0 -199
- package/cjs/parsers/text.js +0 -61
- package/cjs/parsers/yaml.js +0 -239
- package/cjs/pointer.js +0 -290
- package/cjs/ref.js +0 -333
- package/cjs/refs.js +0 -214
- package/cjs/resolve-external.js +0 -333
- package/cjs/resolvers/file.js +0 -106
- package/cjs/resolvers/http.js +0 -184
- package/cjs/util/errors.js +0 -401
- package/cjs/util/plugins.js +0 -159
- package/cjs/util/projectDir.cjs +0 -6
- package/lib/index.d.ts +0 -496
- package/lib/index.js +0 -290
- package/lib/options.js +0 -128
- package/lib/parse.js +0 -162
- package/lib/parsers/binary.js +0 -53
- package/lib/parsers/text.js +0 -64
- package/lib/pointer.js +0 -293
- package/lib/ref.js +0 -292
- package/lib/refs.js +0 -196
- package/lib/resolvers/file.js +0 -63
- package/lib/resolvers/http.js +0 -155
- package/lib/util/errors.js +0 -134
- package/lib/util/projectDir.cjs +0 -6
package/lib/index.js
DELETED
|
@@ -1,290 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-unused-vars */
|
|
2
|
-
import $Refs from "./refs.js";
|
|
3
|
-
import _parse from "./parse.js";
|
|
4
|
-
import normalizeArgs from "./normalize-args.js";
|
|
5
|
-
import resolveExternal from "./resolve-external.js";
|
|
6
|
-
import _bundle from "./bundle.js";
|
|
7
|
-
import _dereference from "./dereference.js";
|
|
8
|
-
import * as url from "./util/url.js";
|
|
9
|
-
import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } from "./util/errors.js";
|
|
10
|
-
import maybe from "call-me-maybe";
|
|
11
|
-
import { ono } from "@jsdevtools/ono";
|
|
12
|
-
|
|
13
|
-
export default $RefParser;
|
|
14
|
-
export { JSONParserError };
|
|
15
|
-
export { InvalidPointerError };
|
|
16
|
-
export { MissingPointerError };
|
|
17
|
-
export { ResolverError };
|
|
18
|
-
export { ParserError };
|
|
19
|
-
export { UnmatchedParserError };
|
|
20
|
-
export { UnmatchedResolverError };
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
|
|
24
|
-
* and provides methods for traversing, manipulating, and dereferencing those references.
|
|
25
|
-
*
|
|
26
|
-
* @constructor
|
|
27
|
-
*/
|
|
28
|
-
function $RefParser () {
|
|
29
|
-
/**
|
|
30
|
-
* The parsed (and possibly dereferenced) JSON schema object
|
|
31
|
-
*
|
|
32
|
-
* @type {object}
|
|
33
|
-
* @readonly
|
|
34
|
-
*/
|
|
35
|
-
this.schema = null;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* The resolved JSON references
|
|
39
|
-
*
|
|
40
|
-
* @type {$Refs}
|
|
41
|
-
* @readonly
|
|
42
|
-
*/
|
|
43
|
-
this.$refs = new $Refs();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Parses the given JSON schema.
|
|
48
|
-
* This method does not resolve any JSON references.
|
|
49
|
-
* It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
|
|
50
|
-
*
|
|
51
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
52
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
53
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed
|
|
54
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
|
|
55
|
-
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
|
|
56
|
-
*/
|
|
57
|
-
$RefParser.parse = function parse (path, schema, options, callback) {
|
|
58
|
-
let Class = this; // eslint-disable-line consistent-this
|
|
59
|
-
let instance = new Class();
|
|
60
|
-
return instance.parse.apply(instance, arguments);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Parses the given JSON schema.
|
|
65
|
-
* This method does not resolve any JSON references.
|
|
66
|
-
* It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
|
|
67
|
-
*
|
|
68
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
69
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
70
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed
|
|
71
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
|
|
72
|
-
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
|
|
73
|
-
*/
|
|
74
|
-
$RefParser.prototype.parse = async function parse (path, schema, options, callback) {
|
|
75
|
-
let args = normalizeArgs(arguments);
|
|
76
|
-
let promise;
|
|
77
|
-
|
|
78
|
-
if (!args.path && !args.schema) {
|
|
79
|
-
let err = ono(`Expected a file path, URL, or object. Got ${args.path || args.schema}`);
|
|
80
|
-
return maybe(args.callback, Promise.reject(err));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Reset everything
|
|
84
|
-
this.schema = null;
|
|
85
|
-
this.$refs = new $Refs();
|
|
86
|
-
|
|
87
|
-
// If the path is a filesystem path, then convert it to a URL.
|
|
88
|
-
// NOTE: According to the JSON Reference spec, these should already be URLs,
|
|
89
|
-
// but, in practice, many people use local filesystem paths instead.
|
|
90
|
-
// So we're being generous here and doing the conversion automatically.
|
|
91
|
-
// This is not intended to be a 100% bulletproof solution.
|
|
92
|
-
// If it doesn't work for your use-case, then use a URL instead.
|
|
93
|
-
let pathType = "http";
|
|
94
|
-
if (url.isFileSystemPath(args.path)) {
|
|
95
|
-
args.path = url.fromFileSystemPath(args.path);
|
|
96
|
-
pathType = "file";
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Resolve the absolute path of the schema
|
|
100
|
-
args.path = url.resolve(url.cwd(), args.path);
|
|
101
|
-
|
|
102
|
-
if (args.schema && typeof args.schema === "object") {
|
|
103
|
-
// A schema object was passed-in.
|
|
104
|
-
// So immediately add a new $Ref with the schema object as its value
|
|
105
|
-
let $ref = this.$refs._add(args.path);
|
|
106
|
-
$ref.value = args.schema;
|
|
107
|
-
$ref.pathType = pathType;
|
|
108
|
-
promise = Promise.resolve(args.schema);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
// Parse the schema file/url
|
|
112
|
-
promise = _parse(args.path, this.$refs, args.options);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let me = this;
|
|
116
|
-
try {
|
|
117
|
-
let result = await promise;
|
|
118
|
-
|
|
119
|
-
if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
|
|
120
|
-
me.schema = result;
|
|
121
|
-
return maybe(args.callback, Promise.resolve(me.schema));
|
|
122
|
-
}
|
|
123
|
-
else if (args.options.continueOnError) {
|
|
124
|
-
me.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
|
|
125
|
-
return maybe(args.callback, Promise.resolve(me.schema));
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
throw ono.syntax(`"${me.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
catch (err) {
|
|
132
|
-
if (!args.options.continueOnError || !isHandledError(err)) {
|
|
133
|
-
return maybe(args.callback, Promise.reject(err));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (this.$refs._$refs[url.stripHash(args.path)]) {
|
|
137
|
-
this.$refs._$refs[url.stripHash(args.path)].addError(err);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return maybe(args.callback, Promise.resolve(null));
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Parses the given JSON schema and resolves any JSON references, including references in
|
|
146
|
-
* externally-referenced files.
|
|
147
|
-
*
|
|
148
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
149
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
150
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed and resolved
|
|
151
|
-
* @param {function} [callback]
|
|
152
|
-
* - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
|
|
153
|
-
*
|
|
154
|
-
* @returns {Promise}
|
|
155
|
-
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
|
|
156
|
-
*/
|
|
157
|
-
$RefParser.resolve = function resolve (path, schema, options, callback) {
|
|
158
|
-
let Class = this; // eslint-disable-line consistent-this
|
|
159
|
-
let instance = new Class();
|
|
160
|
-
return instance.resolve.apply(instance, arguments);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Parses the given JSON schema and resolves any JSON references, including references in
|
|
165
|
-
* externally-referenced files.
|
|
166
|
-
*
|
|
167
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
168
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
169
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed and resolved
|
|
170
|
-
* @param {function} [callback]
|
|
171
|
-
* - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
|
|
172
|
-
*
|
|
173
|
-
* @returns {Promise}
|
|
174
|
-
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
|
|
175
|
-
*/
|
|
176
|
-
$RefParser.prototype.resolve = async function resolve (path, schema, options, callback) {
|
|
177
|
-
let me = this;
|
|
178
|
-
let args = normalizeArgs(arguments);
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
await this.parse(args.path, args.schema, args.options);
|
|
182
|
-
await resolveExternal(me, args.options);
|
|
183
|
-
finalize(me);
|
|
184
|
-
return maybe(args.callback, Promise.resolve(me.$refs));
|
|
185
|
-
}
|
|
186
|
-
catch (err) {
|
|
187
|
-
return maybe(args.callback, Promise.reject(err));
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Parses the given JSON schema, resolves any JSON references, and bundles all external references
|
|
193
|
-
* into the main JSON schema. This produces a JSON schema that only has *internal* references,
|
|
194
|
-
* not any *external* references.
|
|
195
|
-
*
|
|
196
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
197
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
198
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed, resolved, and dereferenced
|
|
199
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the bundled JSON schema object
|
|
200
|
-
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
|
|
201
|
-
*/
|
|
202
|
-
$RefParser.bundle = function bundle (path, schema, options, callback) {
|
|
203
|
-
let Class = this; // eslint-disable-line consistent-this
|
|
204
|
-
let instance = new Class();
|
|
205
|
-
return instance.bundle.apply(instance, arguments);
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Parses the given JSON schema, resolves any JSON references, and bundles all external references
|
|
210
|
-
* into the main JSON schema. This produces a JSON schema that only has *internal* references,
|
|
211
|
-
* not any *external* references.
|
|
212
|
-
*
|
|
213
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
214
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
215
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed, resolved, and dereferenced
|
|
216
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the bundled JSON schema object
|
|
217
|
-
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
|
|
218
|
-
*/
|
|
219
|
-
$RefParser.prototype.bundle = async function bundle (path, schema, options, callback) {
|
|
220
|
-
let me = this;
|
|
221
|
-
let args = normalizeArgs(arguments);
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
await this.resolve(args.path, args.schema, args.options);
|
|
225
|
-
_bundle(me, args.options);
|
|
226
|
-
finalize(me);
|
|
227
|
-
return maybe(args.callback, Promise.resolve(me.schema));
|
|
228
|
-
}
|
|
229
|
-
catch (err) {
|
|
230
|
-
return maybe(args.callback, Promise.reject(err));
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
|
|
236
|
-
* That is, all JSON references are replaced with their resolved values.
|
|
237
|
-
*
|
|
238
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
239
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
240
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed, resolved, and dereferenced
|
|
241
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
|
|
242
|
-
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
|
|
243
|
-
*/
|
|
244
|
-
$RefParser.dereference = function dereference (path, schema, options, callback) {
|
|
245
|
-
let Class = this; // eslint-disable-line consistent-this
|
|
246
|
-
let instance = new Class();
|
|
247
|
-
return instance.dereference.apply(instance, arguments);
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
|
|
252
|
-
* That is, all JSON references are replaced with their resolved values.
|
|
253
|
-
*
|
|
254
|
-
* @param {string} [path] - The file path or URL of the JSON schema
|
|
255
|
-
* @param {object} [schema] - A JSON schema object. This object will be used instead of reading from `path`.
|
|
256
|
-
* @param {$RefParserOptions} [options] - Options that determine how the schema is parsed, resolved, and dereferenced
|
|
257
|
-
* @param {function} [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
|
|
258
|
-
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
|
|
259
|
-
*/
|
|
260
|
-
$RefParser.prototype.dereference = async function dereference (path, schema, options, callback) {
|
|
261
|
-
let me = this;
|
|
262
|
-
let args = normalizeArgs(arguments);
|
|
263
|
-
|
|
264
|
-
try {
|
|
265
|
-
await this.resolve(args.path, args.schema, args.options);
|
|
266
|
-
_dereference(me, args.options);
|
|
267
|
-
finalize(me);
|
|
268
|
-
return maybe(args.callback, Promise.resolve(me.schema));
|
|
269
|
-
}
|
|
270
|
-
catch (err) {
|
|
271
|
-
return maybe(args.callback, Promise.reject(err));
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
function finalize (parser) {
|
|
276
|
-
const errors = JSONParserErrorGroup.getParserErrors(parser);
|
|
277
|
-
if (errors.length > 0) {
|
|
278
|
-
throw new JSONParserErrorGroup(parser);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export const parse = $RefParser.parse.bind($RefParser);
|
|
283
|
-
export const resolve = $RefParser.resolve.bind($RefParser);
|
|
284
|
-
export const bundle = $RefParser.bundle.bind($RefParser);
|
|
285
|
-
export const dereference = $RefParser.dereference.bind($RefParser);
|
|
286
|
-
|
|
287
|
-
// CommonJS default export hack
|
|
288
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
289
|
-
module.exports = Object.assign(module.exports.default, module.exports);
|
|
290
|
-
}
|
package/lib/options.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/* eslint lines-around-comment: [2, {beforeBlockComment: false}] */
|
|
2
|
-
import jsonParser from "./parsers/json.js";
|
|
3
|
-
import yamlParser from "./parsers/yaml.js";
|
|
4
|
-
import textParser from "./parsers/text.js";
|
|
5
|
-
import binaryParser from "./parsers/binary.js";
|
|
6
|
-
import fileResolver from "./resolvers/file.js";
|
|
7
|
-
import httpResolver from "./resolvers/http.js";
|
|
8
|
-
|
|
9
|
-
export default $RefParserOptions;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
|
|
13
|
-
*
|
|
14
|
-
* @param {object|$RefParserOptions} [options] - Overridden options
|
|
15
|
-
* @constructor
|
|
16
|
-
*/
|
|
17
|
-
function $RefParserOptions (options) {
|
|
18
|
-
merge(this, $RefParserOptions.defaults);
|
|
19
|
-
merge(this, options);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
$RefParserOptions.defaults = {
|
|
23
|
-
/**
|
|
24
|
-
* Determines how different types of files will be parsed.
|
|
25
|
-
*
|
|
26
|
-
* You can add additional parsers of your own, replace an existing one with
|
|
27
|
-
* your own implementation, or disable any parser by setting it to false.
|
|
28
|
-
*/
|
|
29
|
-
parse: {
|
|
30
|
-
json: jsonParser,
|
|
31
|
-
yaml: yamlParser,
|
|
32
|
-
text: textParser,
|
|
33
|
-
binary: binaryParser,
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Determines how JSON References will be resolved.
|
|
38
|
-
*
|
|
39
|
-
* You can add additional resolvers of your own, replace an existing one with
|
|
40
|
-
* your own implementation, or disable any resolver by setting it to false.
|
|
41
|
-
*/
|
|
42
|
-
resolve: {
|
|
43
|
-
file: fileResolver,
|
|
44
|
-
http: httpResolver,
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Determines whether external $ref pointers will be resolved.
|
|
48
|
-
* If this option is disabled, then none of above resolvers will be called.
|
|
49
|
-
* Instead, external $ref pointers will simply be ignored.
|
|
50
|
-
*
|
|
51
|
-
* @type {boolean}
|
|
52
|
-
*/
|
|
53
|
-
external: true,
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
|
|
58
|
-
* causes it to keep processing as much as possible and then throw a single error that contains all errors
|
|
59
|
-
* that were encountered.
|
|
60
|
-
*/
|
|
61
|
-
continueOnError: false,
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Determines the types of JSON references that are allowed.
|
|
65
|
-
*/
|
|
66
|
-
dereference: {
|
|
67
|
-
/**
|
|
68
|
-
* Dereference circular (recursive) JSON references?
|
|
69
|
-
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
|
|
70
|
-
* If "ignore", then circular references will not be dereferenced.
|
|
71
|
-
*
|
|
72
|
-
* @type {boolean|string}
|
|
73
|
-
*/
|
|
74
|
-
circular: true,
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* A function, called for each path, which can return true to stop this path and all
|
|
78
|
-
* subpaths from being dereferenced further. This is useful in schemas where some
|
|
79
|
-
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
80
|
-
*
|
|
81
|
-
* @type {function}
|
|
82
|
-
*/
|
|
83
|
-
excludedPathMatcher: () => false
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Merges the properties of the source object into the target object.
|
|
89
|
-
*
|
|
90
|
-
* @param {object} target - The object that we're populating
|
|
91
|
-
* @param {?object} source - The options that are being merged
|
|
92
|
-
* @returns {object}
|
|
93
|
-
*/
|
|
94
|
-
function merge (target, source) {
|
|
95
|
-
if (isMergeable(source)) {
|
|
96
|
-
let keys = Object.keys(source);
|
|
97
|
-
for (let i = 0; i < keys.length; i++) {
|
|
98
|
-
let key = keys[i];
|
|
99
|
-
let sourceSetting = source[key];
|
|
100
|
-
let targetSetting = target[key];
|
|
101
|
-
|
|
102
|
-
if (isMergeable(sourceSetting)) {
|
|
103
|
-
// It's a nested object, so merge it recursively
|
|
104
|
-
target[key] = merge(targetSetting || {}, sourceSetting);
|
|
105
|
-
}
|
|
106
|
-
else if (sourceSetting !== undefined) {
|
|
107
|
-
// It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
|
|
108
|
-
target[key] = sourceSetting;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return target;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Determines whether the given value can be merged,
|
|
117
|
-
* or if it is a scalar value that should just override the target value.
|
|
118
|
-
*
|
|
119
|
-
* @param {*} val
|
|
120
|
-
* @returns {Boolean}
|
|
121
|
-
*/
|
|
122
|
-
function isMergeable (val) {
|
|
123
|
-
return val &&
|
|
124
|
-
(typeof val === "object") &&
|
|
125
|
-
!Array.isArray(val) &&
|
|
126
|
-
!(val instanceof RegExp) &&
|
|
127
|
-
!(val instanceof Date);
|
|
128
|
-
}
|
package/lib/parse.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { ono } from "@jsdevtools/ono";
|
|
2
|
-
import * as url from "./util/url.js";
|
|
3
|
-
import * as plugins from "./util/plugins.js";
|
|
4
|
-
import { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError } from "./util/errors.js";
|
|
5
|
-
|
|
6
|
-
export default parse;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Reads and parses the specified file path or URL.
|
|
10
|
-
*
|
|
11
|
-
* @param {string} path - This path MUST already be resolved, since `read` doesn't know the resolution context
|
|
12
|
-
* @param {$Refs} $refs
|
|
13
|
-
* @param {$RefParserOptions} options
|
|
14
|
-
*
|
|
15
|
-
* @returns {Promise}
|
|
16
|
-
* The promise resolves with the parsed file contents, NOT the raw (Buffer) contents.
|
|
17
|
-
*/
|
|
18
|
-
async function parse (path, $refs, options) {
|
|
19
|
-
// Remove the URL fragment, if any
|
|
20
|
-
path = url.stripHash(path);
|
|
21
|
-
|
|
22
|
-
// Add a new $Ref for this file, even though we don't have the value yet.
|
|
23
|
-
// This ensures that we don't simultaneously read & parse the same file multiple times
|
|
24
|
-
let $ref = $refs._add(path);
|
|
25
|
-
|
|
26
|
-
// This "file object" will be passed to all resolvers and parsers.
|
|
27
|
-
let file = {
|
|
28
|
-
url: path,
|
|
29
|
-
extension: url.getExtension(path),
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// Read the file and then parse the data
|
|
33
|
-
try {
|
|
34
|
-
const resolver = await readFile(file, options, $refs);
|
|
35
|
-
$ref.pathType = resolver.plugin.name;
|
|
36
|
-
file.data = resolver.result;
|
|
37
|
-
|
|
38
|
-
const parser = await parseFile(file, options, $refs);
|
|
39
|
-
$ref.value = parser.result;
|
|
40
|
-
|
|
41
|
-
return parser.result;
|
|
42
|
-
}
|
|
43
|
-
catch (err) {
|
|
44
|
-
if (isHandledError(err)) {
|
|
45
|
-
$ref.value = err;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
throw err;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Reads the given file, using the configured resolver plugins
|
|
54
|
-
*
|
|
55
|
-
* @param {object} file - An object containing information about the referenced file
|
|
56
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
57
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
58
|
-
* @param {$RefParserOptions} options
|
|
59
|
-
*
|
|
60
|
-
* @returns {Promise}
|
|
61
|
-
* The promise resolves with the raw file contents and the resolver that was used.
|
|
62
|
-
*/
|
|
63
|
-
function readFile (file, options, $refs) {
|
|
64
|
-
return new Promise(((resolve, reject) => {
|
|
65
|
-
// console.log('Reading %s', file.url);
|
|
66
|
-
|
|
67
|
-
// Find the resolvers that can read this file
|
|
68
|
-
let resolvers = plugins.all(options.resolve);
|
|
69
|
-
resolvers = plugins.filter(resolvers, "canRead", file);
|
|
70
|
-
|
|
71
|
-
// Run the resolvers, in order, until one of them succeeds
|
|
72
|
-
plugins.sort(resolvers);
|
|
73
|
-
plugins.run(resolvers, "read", file, $refs)
|
|
74
|
-
.then(resolve, onError);
|
|
75
|
-
|
|
76
|
-
function onError (err) {
|
|
77
|
-
if (!err && options.continueOnError) {
|
|
78
|
-
// No resolver could be matched
|
|
79
|
-
reject(new UnmatchedResolverError(file.url));
|
|
80
|
-
}
|
|
81
|
-
else if (!err || !("error" in err)) {
|
|
82
|
-
// Throw a generic, friendly error.
|
|
83
|
-
reject(ono.syntax(`Unable to resolve $ref pointer "${file.url}"`));
|
|
84
|
-
}
|
|
85
|
-
// Throw the original error, if it's one of our own (user-friendly) errors.
|
|
86
|
-
else if (err.error instanceof ResolverError) {
|
|
87
|
-
reject(err.error);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
reject(new ResolverError(err, file.url));
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Parses the given file's contents, using the configured parser plugins.
|
|
98
|
-
*
|
|
99
|
-
* @param {object} file - An object containing information about the referenced file
|
|
100
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
101
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
102
|
-
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
103
|
-
* @param {$RefParserOptions} options
|
|
104
|
-
*
|
|
105
|
-
* @returns {Promise}
|
|
106
|
-
* The promise resolves with the parsed file contents and the parser that was used.
|
|
107
|
-
*/
|
|
108
|
-
function parseFile (file, options, $refs) {
|
|
109
|
-
return new Promise(((resolve, reject) => {
|
|
110
|
-
// console.log('Parsing %s', file.url);
|
|
111
|
-
|
|
112
|
-
// Find the parsers that can read this file type.
|
|
113
|
-
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
|
|
114
|
-
// This handles situations where the file IS a supported type, just with an unknown extension.
|
|
115
|
-
let allParsers = plugins.all(options.parse);
|
|
116
|
-
let filteredParsers = plugins.filter(allParsers, "canParse", file);
|
|
117
|
-
let parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
|
|
118
|
-
|
|
119
|
-
// Run the parsers, in order, until one of them succeeds
|
|
120
|
-
plugins.sort(parsers);
|
|
121
|
-
plugins.run(parsers, "parse", file, $refs)
|
|
122
|
-
.then(onParsed, onError);
|
|
123
|
-
|
|
124
|
-
function onParsed (parser) {
|
|
125
|
-
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
|
|
126
|
-
reject(ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`));
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
resolve(parser);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function onError (err) {
|
|
134
|
-
if (!err && options.continueOnError) {
|
|
135
|
-
// No resolver could be matched
|
|
136
|
-
reject(new UnmatchedParserError(file.url));
|
|
137
|
-
}
|
|
138
|
-
else if (!err || !("error" in err)) {
|
|
139
|
-
reject(ono.syntax(`Unable to parse ${file.url}`));
|
|
140
|
-
}
|
|
141
|
-
else if (err.error instanceof ParserError) {
|
|
142
|
-
reject(err.error);
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
reject(new ParserError(err.error.message, file.url));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Determines whether the parsed value is "empty".
|
|
153
|
-
*
|
|
154
|
-
* @param {*} value
|
|
155
|
-
* @returns {boolean}
|
|
156
|
-
*/
|
|
157
|
-
function isEmpty (value) {
|
|
158
|
-
return value === undefined ||
|
|
159
|
-
(typeof value === "object" && Object.keys(value).length === 0) ||
|
|
160
|
-
(typeof value === "string" && value.trim().length === 0) ||
|
|
161
|
-
(Buffer.isBuffer(value) && value.length === 0);
|
|
162
|
-
}
|
package/lib/parsers/binary.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
let BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
/**
|
|
5
|
-
* The order that this parser will run, in relation to other parsers.
|
|
6
|
-
*
|
|
7
|
-
* @type {number}
|
|
8
|
-
*/
|
|
9
|
-
order: 400,
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Whether to allow "empty" files (zero bytes).
|
|
13
|
-
*
|
|
14
|
-
* @type {boolean}
|
|
15
|
-
*/
|
|
16
|
-
allowEmpty: true,
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Determines whether this parser can parse a given file reference.
|
|
20
|
-
* Parsers that return true will be tried, in order, until one successfully parses the file.
|
|
21
|
-
* Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
|
|
22
|
-
* every parser will be tried.
|
|
23
|
-
*
|
|
24
|
-
* @param {object} file - An object containing information about the referenced file
|
|
25
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
26
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
27
|
-
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
28
|
-
* @returns {boolean}
|
|
29
|
-
*/
|
|
30
|
-
canParse (file) {
|
|
31
|
-
// Use this parser if the file is a Buffer, and has a known binary extension
|
|
32
|
-
return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url);
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Parses the given data as a Buffer (byte array).
|
|
37
|
-
*
|
|
38
|
-
* @param {object} file - An object containing information about the referenced file
|
|
39
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
40
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
41
|
-
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
42
|
-
* @returns {Buffer}
|
|
43
|
-
*/
|
|
44
|
-
parse (file) {
|
|
45
|
-
if (Buffer.isBuffer(file.data)) {
|
|
46
|
-
return file.data;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
// This will reject if data is anything other than a string or typed array
|
|
50
|
-
return Buffer.from(file.data);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
package/lib/parsers/text.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { ParserError } from "../util/errors.js";
|
|
2
|
-
|
|
3
|
-
let TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
|
|
4
|
-
|
|
5
|
-
export default {
|
|
6
|
-
/**
|
|
7
|
-
* The order that this parser will run, in relation to other parsers.
|
|
8
|
-
*
|
|
9
|
-
* @type {number}
|
|
10
|
-
*/
|
|
11
|
-
order: 300,
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Whether to allow "empty" files (zero bytes).
|
|
15
|
-
*
|
|
16
|
-
* @type {boolean}
|
|
17
|
-
*/
|
|
18
|
-
allowEmpty: true,
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The encoding that the text is expected to be in.
|
|
22
|
-
*
|
|
23
|
-
* @type {string}
|
|
24
|
-
*/
|
|
25
|
-
encoding: "utf8",
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Determines whether this parser can parse a given file reference.
|
|
29
|
-
* Parsers that return true will be tried, in order, until one successfully parses the file.
|
|
30
|
-
* Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
|
|
31
|
-
* every parser will be tried.
|
|
32
|
-
*
|
|
33
|
-
* @param {object} file - An object containing information about the referenced file
|
|
34
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
35
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
36
|
-
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
37
|
-
* @returns {boolean}
|
|
38
|
-
*/
|
|
39
|
-
canParse (file) {
|
|
40
|
-
// Use this parser if the file is a string or Buffer, and has a known text-based extension
|
|
41
|
-
return (typeof file.data === "string" || Buffer.isBuffer(file.data)) && TEXT_REGEXP.test(file.url);
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Parses the given file as text
|
|
46
|
-
*
|
|
47
|
-
* @param {object} file - An object containing information about the referenced file
|
|
48
|
-
* @param {string} file.url - The full URL of the referenced file
|
|
49
|
-
* @param {string} file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
50
|
-
* @param {*} file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
51
|
-
* @returns {string}
|
|
52
|
-
*/
|
|
53
|
-
parse (file) {
|
|
54
|
-
if (typeof file.data === "string") {
|
|
55
|
-
return file.data;
|
|
56
|
-
}
|
|
57
|
-
else if (Buffer.isBuffer(file.data)) {
|
|
58
|
-
return file.data.toString(this.encoding);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
throw new ParserError("data is not text", file.url);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
};
|