@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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getNewOptions = void 0;
|
|
7
|
+
const json_js_1 = __importDefault(require("./parsers/json.js"));
|
|
8
|
+
const yaml_js_1 = __importDefault(require("./parsers/yaml.js"));
|
|
9
|
+
const text_js_1 = __importDefault(require("./parsers/text.js"));
|
|
10
|
+
const binary_js_1 = __importDefault(require("./parsers/binary.js"));
|
|
11
|
+
const file_js_1 = __importDefault(require("./resolvers/file.js"));
|
|
12
|
+
const http_js_1 = __importDefault(require("./resolvers/http.js"));
|
|
13
|
+
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
14
|
+
const getDefaults = () => {
|
|
15
|
+
const defaults = {
|
|
16
|
+
/**
|
|
17
|
+
* Determines how different types of files will be parsed.
|
|
18
|
+
*
|
|
19
|
+
* You can add additional parsers of your own, replace an existing one with
|
|
20
|
+
* your own implementation, or disable any parser by setting it to false.
|
|
21
|
+
*/
|
|
22
|
+
parse: {
|
|
23
|
+
json: json_js_1.default,
|
|
24
|
+
yaml: yaml_js_1.default,
|
|
25
|
+
text: text_js_1.default,
|
|
26
|
+
binary: binary_js_1.default,
|
|
27
|
+
},
|
|
28
|
+
/**
|
|
29
|
+
* Determines how JSON References will be resolved.
|
|
30
|
+
*
|
|
31
|
+
* You can add additional resolvers of your own, replace an existing one with
|
|
32
|
+
* your own implementation, or disable any resolver by setting it to false.
|
|
33
|
+
*/
|
|
34
|
+
resolve: {
|
|
35
|
+
file: file_js_1.default,
|
|
36
|
+
http: http_js_1.default,
|
|
37
|
+
/**
|
|
38
|
+
* Determines whether external $ref pointers will be resolved.
|
|
39
|
+
* If this option is disabled, then none of above resolvers will be called.
|
|
40
|
+
* Instead, external $ref pointers will simply be ignored.
|
|
41
|
+
*
|
|
42
|
+
* @type {boolean}
|
|
43
|
+
*/
|
|
44
|
+
external: true,
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
|
|
48
|
+
* causes it to keep processing as much as possible and then throw a single error that contains all errors
|
|
49
|
+
* that were encountered.
|
|
50
|
+
*/
|
|
51
|
+
continueOnError: false,
|
|
52
|
+
/**
|
|
53
|
+
* Determines the types of JSON references that are allowed.
|
|
54
|
+
*/
|
|
55
|
+
dereference: {
|
|
56
|
+
/**
|
|
57
|
+
* Dereference circular (recursive) JSON references?
|
|
58
|
+
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
|
|
59
|
+
* If "ignore", then circular references will not be dereferenced.
|
|
60
|
+
*
|
|
61
|
+
* @type {boolean|string}
|
|
62
|
+
*/
|
|
63
|
+
circular: true,
|
|
64
|
+
/**
|
|
65
|
+
* A function, called for each path, which can return true to stop this path and all
|
|
66
|
+
* subpaths from being dereferenced further. This is useful in schemas where some
|
|
67
|
+
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
68
|
+
*
|
|
69
|
+
* @type {function}
|
|
70
|
+
*/
|
|
71
|
+
excludedPathMatcher: () => false,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
return (0, lodash_clonedeep_1.default)(defaults);
|
|
75
|
+
};
|
|
76
|
+
const getNewOptions = (options) => {
|
|
77
|
+
const newOptions = getDefaults();
|
|
78
|
+
if (options) {
|
|
79
|
+
merge(newOptions, options);
|
|
80
|
+
}
|
|
81
|
+
return newOptions;
|
|
82
|
+
};
|
|
83
|
+
exports.getNewOptions = getNewOptions;
|
|
84
|
+
/**
|
|
85
|
+
* Merges the properties of the source object into the target object.
|
|
86
|
+
*
|
|
87
|
+
* @param target - The object that we're populating
|
|
88
|
+
* @param source - The options that are being merged
|
|
89
|
+
* @returns
|
|
90
|
+
*/
|
|
91
|
+
function merge(target, source) {
|
|
92
|
+
if (isMergeable(source)) {
|
|
93
|
+
const keys = Object.keys(source);
|
|
94
|
+
for (let i = 0; i < keys.length; i++) {
|
|
95
|
+
const key = keys[i];
|
|
96
|
+
const sourceSetting = source[key];
|
|
97
|
+
const targetSetting = target[key];
|
|
98
|
+
if (isMergeable(sourceSetting)) {
|
|
99
|
+
// It's a nested object, so merge it recursively
|
|
100
|
+
target[key] = merge(targetSetting || {}, sourceSetting);
|
|
101
|
+
}
|
|
102
|
+
else if (sourceSetting !== undefined) {
|
|
103
|
+
// It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
|
|
104
|
+
target[key] = sourceSetting;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return target;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Determines whether the given value can be merged,
|
|
112
|
+
* or if it is a scalar value that should just override the target value.
|
|
113
|
+
*
|
|
114
|
+
* @param val
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
function isMergeable(val) {
|
|
118
|
+
return val && typeof val === "object" && !Array.isArray(val) && !(val instanceof RegExp) && !(val instanceof Date);
|
|
119
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type $Refs from "./refs.js";
|
|
3
|
+
import type { Options } from "./options.js";
|
|
4
|
+
export default parse;
|
|
5
|
+
/**
|
|
6
|
+
* Reads and parses the specified file path or URL.
|
|
7
|
+
*/
|
|
8
|
+
declare function parse(path: string, $refs: $Refs, options: Options): Promise<string | import("./types/index.js").JSONSchema | Buffer | undefined>;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
const ono_1 = require("@jsdevtools/ono");
|
|
36
|
+
const url = __importStar(require("./util/url.js"));
|
|
37
|
+
const plugins = __importStar(require("./util/plugins.js"));
|
|
38
|
+
const errors_js_1 = require("./util/errors.js");
|
|
39
|
+
exports.default = parse;
|
|
40
|
+
/**
|
|
41
|
+
* Reads and parses the specified file path or URL.
|
|
42
|
+
*/
|
|
43
|
+
function parse(path, $refs, options) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
// Remove the URL fragment, if any
|
|
46
|
+
path = url.stripHash(path);
|
|
47
|
+
// Add a new $Ref for this file, even though we don't have the value yet.
|
|
48
|
+
// This ensures that we don't simultaneously read & parse the same file multiple times
|
|
49
|
+
const $ref = $refs._add(path);
|
|
50
|
+
// This "file object" will be passed to all resolvers and parsers.
|
|
51
|
+
const file = {
|
|
52
|
+
url: path,
|
|
53
|
+
extension: url.getExtension(path),
|
|
54
|
+
};
|
|
55
|
+
// Read the file and then parse the data
|
|
56
|
+
try {
|
|
57
|
+
const resolver = yield readFile(file, options, $refs);
|
|
58
|
+
$ref.pathType = resolver.plugin.name;
|
|
59
|
+
file.data = resolver.result;
|
|
60
|
+
const parser = yield parseFile(file, options, $refs);
|
|
61
|
+
$ref.value = parser.result;
|
|
62
|
+
return parser.result;
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
if ((0, errors_js_1.isHandledError)(err)) {
|
|
66
|
+
$ref.value = err;
|
|
67
|
+
}
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Reads the given file, using the configured resolver plugins
|
|
74
|
+
*
|
|
75
|
+
* @param file - An object containing information about the referenced file
|
|
76
|
+
* @param file.url - The full URL of the referenced file
|
|
77
|
+
* @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
78
|
+
* @param options
|
|
79
|
+
*
|
|
80
|
+
* @returns
|
|
81
|
+
* The promise resolves with the raw file contents and the resolver that was used.
|
|
82
|
+
*/
|
|
83
|
+
function readFile(file, options, $refs) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
// console.log('Reading %s', file.url);
|
|
86
|
+
// Find the resolvers that can read this file
|
|
87
|
+
let resolvers = plugins.all(options.resolve);
|
|
88
|
+
resolvers = plugins.filter(resolvers, "canRead", file);
|
|
89
|
+
// Run the resolvers, in order, until one of them succeeds
|
|
90
|
+
plugins.sort(resolvers);
|
|
91
|
+
try {
|
|
92
|
+
const data = yield plugins.run(resolvers, "read", file, $refs);
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
if (!err && options.continueOnError) {
|
|
97
|
+
// No resolver could be matched
|
|
98
|
+
throw new errors_js_1.UnmatchedResolverError(file.url);
|
|
99
|
+
}
|
|
100
|
+
else if (!err || !("error" in err)) {
|
|
101
|
+
// Throw a generic, friendly error.
|
|
102
|
+
throw ono_1.ono.syntax(`Unable to resolve $ref pointer "${file.url}"`);
|
|
103
|
+
}
|
|
104
|
+
// Throw the original error, if it's one of our own (user-friendly) errors.
|
|
105
|
+
else if (err.error instanceof errors_js_1.ResolverError) {
|
|
106
|
+
throw err.error;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
throw new errors_js_1.ResolverError(err, file.url);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Parses the given file's contents, using the configured parser plugins.
|
|
116
|
+
*
|
|
117
|
+
* @param file - An object containing information about the referenced file
|
|
118
|
+
* @param file.url - The full URL of the referenced file
|
|
119
|
+
* @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
120
|
+
* @param file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
121
|
+
* @param options
|
|
122
|
+
*
|
|
123
|
+
* @returns
|
|
124
|
+
* The promise resolves with the parsed file contents and the parser that was used.
|
|
125
|
+
*/
|
|
126
|
+
function parseFile(file, options, $refs) {
|
|
127
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
+
// console.log('Parsing %s', file.url);
|
|
129
|
+
// Find the parsers that can read this file type.
|
|
130
|
+
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
|
|
131
|
+
// This handles situations where the file IS a supported type, just with an unknown extension.
|
|
132
|
+
const allParsers = plugins.all(options.parse);
|
|
133
|
+
const filteredParsers = plugins.filter(allParsers, "canParse", file);
|
|
134
|
+
const parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
|
|
135
|
+
// Run the parsers, in order, until one of them succeeds
|
|
136
|
+
plugins.sort(parsers);
|
|
137
|
+
try {
|
|
138
|
+
const parser = yield plugins.run(parsers, "parse", file, $refs);
|
|
139
|
+
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
|
|
140
|
+
throw ono_1.ono.syntax(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
return parser;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
if (!err && options.continueOnError) {
|
|
148
|
+
// No resolver could be matched
|
|
149
|
+
throw new errors_js_1.UnmatchedParserError(file.url);
|
|
150
|
+
}
|
|
151
|
+
else if (err && err.message && err.message.startsWith("Error parsing")) {
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
else if (!err || !("error" in err)) {
|
|
155
|
+
throw ono_1.ono.syntax(`Unable to parse ${file.url}`);
|
|
156
|
+
}
|
|
157
|
+
else if (err.error instanceof errors_js_1.ParserError) {
|
|
158
|
+
throw err.error;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
throw new errors_js_1.ParserError(err.error.message, file.url);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Determines whether the parsed value is "empty".
|
|
168
|
+
*
|
|
169
|
+
* @param value
|
|
170
|
+
* @returns
|
|
171
|
+
*/
|
|
172
|
+
function isEmpty(value) {
|
|
173
|
+
return (value === undefined ||
|
|
174
|
+
(typeof value === "object" && Object.keys(value).length === 0) ||
|
|
175
|
+
(typeof value === "string" && value.trim().length === 0) ||
|
|
176
|
+
(Buffer.isBuffer(value) && value.length === 0));
|
|
177
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
|
|
4
|
+
exports.default = {
|
|
5
|
+
/**
|
|
6
|
+
* The order that this parser will run, in relation to other parsers.
|
|
7
|
+
*/
|
|
8
|
+
order: 400,
|
|
9
|
+
/**
|
|
10
|
+
* Whether to allow "empty" files (zero bytes).
|
|
11
|
+
*/
|
|
12
|
+
allowEmpty: true,
|
|
13
|
+
/**
|
|
14
|
+
* Determines whether this parser can parse a given file reference.
|
|
15
|
+
* Parsers that return true will be tried, in order, until one successfully parses the file.
|
|
16
|
+
* Parsers that return false will be skipped, UNLESS all parsers returned false, in which case
|
|
17
|
+
* every parser will be tried.
|
|
18
|
+
*/
|
|
19
|
+
canParse(file) {
|
|
20
|
+
// Use this parser if the file is a Buffer, and has a known binary extension
|
|
21
|
+
return Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url);
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* Parses the given data as a Buffer (byte array).
|
|
25
|
+
*/
|
|
26
|
+
parse(file) {
|
|
27
|
+
if (Buffer.isBuffer(file.data)) {
|
|
28
|
+
return file.data;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// This will reject if data is anything other than a string or typed array
|
|
32
|
+
return Buffer.from(file.data);
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const errors_js_1 = require("../util/errors.js");
|
|
13
|
+
exports.default = {
|
|
14
|
+
/**
|
|
15
|
+
* The order that this parser will run, in relation to other parsers.
|
|
16
|
+
*/
|
|
17
|
+
order: 100,
|
|
18
|
+
/**
|
|
19
|
+
* Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
|
|
20
|
+
*/
|
|
21
|
+
allowEmpty: true,
|
|
22
|
+
/**
|
|
23
|
+
* Determines whether this parser can parse a given file reference.
|
|
24
|
+
* Parsers that match will be tried, in order, until one successfully parses the file.
|
|
25
|
+
* Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
|
|
26
|
+
* every parser will be tried.
|
|
27
|
+
*/
|
|
28
|
+
canParse: ".json",
|
|
29
|
+
/**
|
|
30
|
+
* Parses the given file as JSON
|
|
31
|
+
*/
|
|
32
|
+
parse(file) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
let data = file.data;
|
|
35
|
+
if (Buffer.isBuffer(data)) {
|
|
36
|
+
data = data.toString();
|
|
37
|
+
}
|
|
38
|
+
if (typeof data === "string") {
|
|
39
|
+
if (data.trim().length === 0) {
|
|
40
|
+
return; // This mirrors the YAML behavior
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
try {
|
|
44
|
+
return JSON.parse(data);
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
throw new errors_js_1.ParserError(e.message, file.url);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const errors_js_1 = require("../util/errors.js");
|
|
4
|
+
const TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
|
|
5
|
+
exports.default = {
|
|
6
|
+
/**
|
|
7
|
+
* The order that this parser will run, in relation to other parsers.
|
|
8
|
+
*/
|
|
9
|
+
order: 300,
|
|
10
|
+
/**
|
|
11
|
+
* Whether to allow "empty" files (zero bytes).
|
|
12
|
+
*/
|
|
13
|
+
allowEmpty: true,
|
|
14
|
+
/**
|
|
15
|
+
* The encoding that the text is expected to be in.
|
|
16
|
+
*/
|
|
17
|
+
encoding: "utf8",
|
|
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
|
+
canParse(file) {
|
|
25
|
+
// Use this parser if the file is a string or Buffer, and has a known text-based extension
|
|
26
|
+
return (typeof file.data === "string" || Buffer.isBuffer(file.data)) && TEXT_REGEXP.test(file.url);
|
|
27
|
+
},
|
|
28
|
+
/**
|
|
29
|
+
* Parses the given file as text
|
|
30
|
+
*/
|
|
31
|
+
parse(file) {
|
|
32
|
+
if (typeof file.data === "string") {
|
|
33
|
+
return file.data;
|
|
34
|
+
}
|
|
35
|
+
else if (Buffer.isBuffer(file.data)) {
|
|
36
|
+
return file.data.toString(this.encoding);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new errors_js_1.ParserError("data is not text", file.url);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const errors_js_1 = require("../util/errors.js");
|
|
16
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
17
|
+
const js_yaml_2 = require("js-yaml");
|
|
18
|
+
exports.default = {
|
|
19
|
+
/**
|
|
20
|
+
* The order that this parser will run, in relation to other parsers.
|
|
21
|
+
*/
|
|
22
|
+
order: 200,
|
|
23
|
+
/**
|
|
24
|
+
* Whether to allow "empty" files. This includes zero-byte files, as well as empty JSON objects.
|
|
25
|
+
*/
|
|
26
|
+
allowEmpty: true,
|
|
27
|
+
/**
|
|
28
|
+
* Determines whether this parser can parse a given file reference.
|
|
29
|
+
* Parsers that match will be tried, in order, until one successfully parses the file.
|
|
30
|
+
* Parsers that don't match will be skipped, UNLESS none of the parsers match, in which case
|
|
31
|
+
* every parser will be tried.
|
|
32
|
+
*/
|
|
33
|
+
canParse: [".yaml", ".yml", ".json"],
|
|
34
|
+
/**
|
|
35
|
+
* Parses the given file as YAML
|
|
36
|
+
*
|
|
37
|
+
* @param file - An object containing information about the referenced file
|
|
38
|
+
* @param file.url - The full URL of the referenced file
|
|
39
|
+
* @param file.extension - The lowercased file extension (e.g. ".txt", ".html", etc.)
|
|
40
|
+
* @param file.data - The file contents. This will be whatever data type was returned by the resolver
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
parse(file) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
// eslint-disable-line require-await
|
|
46
|
+
let data = file.data;
|
|
47
|
+
if (Buffer.isBuffer(data)) {
|
|
48
|
+
data = data.toString();
|
|
49
|
+
}
|
|
50
|
+
if (typeof data === "string") {
|
|
51
|
+
try {
|
|
52
|
+
return js_yaml_1.default.load(data, { schema: js_yaml_2.JSON_SCHEMA });
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
// @ts-expect-error TS(2571): Object is of type 'unknown'.
|
|
56
|
+
throw new errors_js_1.ParserError(e.message, file.url);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// data is already a JavaScript value (object, array, number, null, NaN, etc.)
|
|
61
|
+
return data;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type $RefParserOptions from "./options.js";
|
|
2
|
+
import $Ref from "./ref.js";
|
|
3
|
+
/**
|
|
4
|
+
* This class represents a single JSON pointer and its resolved value.
|
|
5
|
+
*
|
|
6
|
+
* @param $ref
|
|
7
|
+
* @param path
|
|
8
|
+
* @param [friendlyPath] - The original user-specified path (used for error messages)
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
declare class Pointer {
|
|
12
|
+
/**
|
|
13
|
+
* The {@link $Ref} object that contains this {@link Pointer} object.
|
|
14
|
+
*/
|
|
15
|
+
$ref: $Ref;
|
|
16
|
+
/**
|
|
17
|
+
* The file path or URL, containing the JSON pointer in the hash.
|
|
18
|
+
* This path is relative to the path of the main JSON schema file.
|
|
19
|
+
*/
|
|
20
|
+
path: string;
|
|
21
|
+
/**
|
|
22
|
+
* The original path or URL, used for error messages.
|
|
23
|
+
*/
|
|
24
|
+
originalPath: string;
|
|
25
|
+
/**
|
|
26
|
+
* The value of the JSON pointer.
|
|
27
|
+
* Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
|
|
28
|
+
*/
|
|
29
|
+
value: any;
|
|
30
|
+
/**
|
|
31
|
+
* Indicates whether the pointer references itself.
|
|
32
|
+
*/
|
|
33
|
+
circular: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* The number of indirect references that were traversed to resolve the value.
|
|
36
|
+
* Resolving a single pointer may require resolving multiple $Refs.
|
|
37
|
+
*/
|
|
38
|
+
indirections: number;
|
|
39
|
+
constructor($ref: any, path: any, friendlyPath: any);
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the value of a nested property within the given object.
|
|
42
|
+
*
|
|
43
|
+
* @param obj - The object that will be crawled
|
|
44
|
+
* @param options
|
|
45
|
+
* @param pathFromRoot - the path of place that initiated resolving
|
|
46
|
+
*
|
|
47
|
+
* @returns
|
|
48
|
+
* Returns a JSON pointer whose {@link Pointer#value} is the resolved value.
|
|
49
|
+
* If resolving this value required resolving other JSON references, then
|
|
50
|
+
* the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
|
|
51
|
+
* of the resolved value.
|
|
52
|
+
*/
|
|
53
|
+
resolve(obj: any, options: any, pathFromRoot: any): this;
|
|
54
|
+
/**
|
|
55
|
+
* Sets the value of a nested property within the given object.
|
|
56
|
+
*
|
|
57
|
+
* @param obj - The object that will be crawled
|
|
58
|
+
* @param value - the value to assign
|
|
59
|
+
* @param options
|
|
60
|
+
*
|
|
61
|
+
* @returns
|
|
62
|
+
* Returns the modified object, or an entirely new object if the entire object is overwritten.
|
|
63
|
+
*/
|
|
64
|
+
set(obj: any, value: any, options?: $RefParserOptions): any;
|
|
65
|
+
/**
|
|
66
|
+
* Parses a JSON pointer (or a path containing a JSON pointer in the hash)
|
|
67
|
+
* and returns an array of the pointer's tokens.
|
|
68
|
+
* (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
|
|
69
|
+
*
|
|
70
|
+
* The pointer is parsed according to RFC 6901
|
|
71
|
+
* {@link https://tools.ietf.org/html/rfc6901#section-3}
|
|
72
|
+
*
|
|
73
|
+
* @param path
|
|
74
|
+
* @param [originalPath]
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
static parse(path: string, originalPath?: string): any;
|
|
78
|
+
/**
|
|
79
|
+
* Creates a JSON pointer path, by joining one or more tokens to a base path.
|
|
80
|
+
*
|
|
81
|
+
* @param base - The base path (e.g. "schema.json#/definitions/person")
|
|
82
|
+
* @param tokens - The token(s) to append (e.g. ["name", "first"])
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
static join(base: any, tokens: any): any;
|
|
86
|
+
}
|
|
87
|
+
export default Pointer;
|