@hey-api/json-schema-ref-parser 1.3.1 → 1.4.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/README.md +130 -1
- package/dist/index.d.mts +33 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +78 -78
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -8
- package/src/bundle.ts +8 -8
- package/src/dereference.ts +1 -1
- package/src/index.ts +22 -8
- package/src/parsers/yaml.ts +2 -4
- package/src/pointer.ts +5 -5
- package/src/ref.ts +2 -2
- package/src/refs.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hey-api/json-schema-ref-parser",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"$ref",
|
|
@@ -45,19 +45,17 @@
|
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@jsdevtools/ono": "7.1.3",
|
|
47
47
|
"@types/json-schema": "7.0.15",
|
|
48
|
-
"
|
|
48
|
+
"yaml": "2.8.3"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"
|
|
52
|
-
"typescript": "5.9.3"
|
|
51
|
+
"typescript": "6.0.2"
|
|
53
52
|
},
|
|
54
53
|
"engines": {
|
|
55
|
-
"node": ">=
|
|
54
|
+
"node": ">=22.13.0"
|
|
56
55
|
},
|
|
57
56
|
"scripts": {
|
|
58
|
-
"build": "tsdown
|
|
59
|
-
"check-exports": "attw --pack . --profile esm-only --ignore-rules cjs-resolves-to-esm",
|
|
57
|
+
"build": "tsdown",
|
|
60
58
|
"dev": "tsdown --watch",
|
|
61
|
-
"typecheck": "
|
|
59
|
+
"typecheck": "tsgo --noEmit"
|
|
62
60
|
}
|
|
63
61
|
}
|
package/src/bundle.ts
CHANGED
|
@@ -92,7 +92,7 @@ const getContainerTypeFromPath = (
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
|
-
* Inventories the given JSON Reference (i.e
|
|
95
|
+
* Inventories the given JSON Reference (i.e., records detailed information about it so we can
|
|
96
96
|
* optimize all $refs in the schema), and then crawls the resolved value.
|
|
97
97
|
*/
|
|
98
98
|
const inventory$Ref = <S extends object = JSONSchema>({
|
|
@@ -213,10 +213,10 @@ const inventory$Ref = <S extends object = JSONSchema>({
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
const newEntry: InventoryEntry = {
|
|
216
|
-
$ref, // The JSON Reference (e.g
|
|
217
|
-
circular: pointer.circular, // Is this $ref pointer DIRECTLY circular? (i.e
|
|
216
|
+
$ref, // The JSON Reference (e.g., {$ref: string})
|
|
217
|
+
circular: pointer.circular, // Is this $ref pointer DIRECTLY circular? (i.e., it references itself)
|
|
218
218
|
depth, // How far from the JSON Schema root is this $ref pointer?
|
|
219
|
-
extended, // Does this $ref extend its resolved value? (i.e
|
|
219
|
+
extended, // Does this $ref extend its resolved value? (i.e., it has extra properties, in addition to "$ref")
|
|
220
220
|
external, // Does this $ref pointer point to a file other than the main JSON Schema file?
|
|
221
221
|
file, // The file that the $ref pointer resolves to
|
|
222
222
|
hash, // The hash within `file` that the $ref pointer resolves to
|
|
@@ -240,7 +240,7 @@ const inventory$Ref = <S extends object = JSONSchema>({
|
|
|
240
240
|
// Recursively crawl the resolved value.
|
|
241
241
|
// When the resolution followed a $ref chain to a different file,
|
|
242
242
|
// use the resolved file as the base path so that local $ref values
|
|
243
|
-
// (e.g
|
|
243
|
+
// (e.g., #/components/schemas/SiblingSchema) inside the resolved
|
|
244
244
|
// value resolve against the correct file.
|
|
245
245
|
if (!existingEntry || external) {
|
|
246
246
|
let crawlPath = pointer.path;
|
|
@@ -445,8 +445,8 @@ function remap(parser: $RefParser, inventory: Array<InventoryEntry>) {
|
|
|
445
445
|
const ensureContainer = (
|
|
446
446
|
type: 'schemas' | 'parameters' | 'requestBodies' | 'responses' | 'headers',
|
|
447
447
|
) => {
|
|
448
|
-
const isOas3 =
|
|
449
|
-
const isOas2 =
|
|
448
|
+
const isOas3 = Boolean(root && typeof root === 'object' && typeof root.openapi === 'string');
|
|
449
|
+
const isOas2 = Boolean(root && typeof root === 'object' && typeof root.swagger === 'string');
|
|
450
450
|
|
|
451
451
|
if (isOas3) {
|
|
452
452
|
if (!root.components || typeof root.components !== 'object') {
|
|
@@ -570,7 +570,7 @@ function remap(parser: $RefParser, inventory: Array<InventoryEntry>) {
|
|
|
570
570
|
}
|
|
571
571
|
|
|
572
572
|
// Keep internal refs internal. However, if the $ref extends the resolved value
|
|
573
|
-
// (i.e
|
|
573
|
+
// (i.e., it has additional properties in addition to "$ref"), then we must
|
|
574
574
|
// preserve the original $ref rather than rewriting it to the resolved hash.
|
|
575
575
|
if (!entry.external) {
|
|
576
576
|
if (!entry.extended && entry.$ref && typeof entry.$ref === 'object') {
|
package/src/dereference.ts
CHANGED
|
@@ -226,7 +226,7 @@ function dereference$Ref<S extends object = JSONSchema>(
|
|
|
226
226
|
}
|
|
227
227
|
|
|
228
228
|
if (directCircular) {
|
|
229
|
-
// The pointer is a DIRECT circular reference (i.e
|
|
229
|
+
// The pointer is a DIRECT circular reference (i.e., it references itself).
|
|
230
230
|
// So replace the $ref path with the absolute path from the JSON Schema root
|
|
231
231
|
dereferencedValue.$ref = pathFromRoot;
|
|
232
232
|
}
|
package/src/index.ts
CHANGED
|
@@ -117,12 +117,12 @@ export class $RefParser {
|
|
|
117
117
|
|
|
118
118
|
await resolveExternal(this, this.options);
|
|
119
119
|
const errors = JSONParserErrorGroup.getParserErrors(this);
|
|
120
|
-
if (errors.length
|
|
120
|
+
if (errors.length) {
|
|
121
121
|
throw new JSONParserErrorGroup(this);
|
|
122
122
|
}
|
|
123
123
|
_bundle(this, this.options);
|
|
124
124
|
const errors2 = JSONParserErrorGroup.getParserErrors(this);
|
|
125
|
-
if (errors2.length
|
|
125
|
+
if (errors2.length) {
|
|
126
126
|
throw new JSONParserErrorGroup(this);
|
|
127
127
|
}
|
|
128
128
|
return this.schema!;
|
|
@@ -148,14 +148,14 @@ export class $RefParser {
|
|
|
148
148
|
|
|
149
149
|
await resolveExternal(this, this.options);
|
|
150
150
|
const errors = JSONParserErrorGroup.getParserErrors(this);
|
|
151
|
-
if (errors.length
|
|
151
|
+
if (errors.length) {
|
|
152
152
|
throw new JSONParserErrorGroup(this);
|
|
153
153
|
}
|
|
154
154
|
_bundle(this, this.options);
|
|
155
155
|
// Merged root is ready for bundling
|
|
156
156
|
|
|
157
157
|
const errors2 = JSONParserErrorGroup.getParserErrors(this);
|
|
158
|
-
if (errors2.length
|
|
158
|
+
if (errors2.length) {
|
|
159
159
|
throw new JSONParserErrorGroup(this);
|
|
160
160
|
}
|
|
161
161
|
return this.schema!;
|
|
@@ -297,7 +297,7 @@ export class $RefParser {
|
|
|
297
297
|
|
|
298
298
|
public mergeMany(): JSONSchema {
|
|
299
299
|
const schemas = this.schemaMany || [];
|
|
300
|
-
if (schemas.length
|
|
300
|
+
if (!schemas.length) {
|
|
301
301
|
throw ono('mergeMany called with no schemas. Did you run parseMany?');
|
|
302
302
|
}
|
|
303
303
|
|
|
@@ -335,7 +335,7 @@ export class $RefParser {
|
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
}
|
|
338
|
-
if (Object.keys(infoAccumulator).length
|
|
338
|
+
if (Object.keys(infoAccumulator).length) {
|
|
339
339
|
merged.info = infoAccumulator;
|
|
340
340
|
}
|
|
341
341
|
|
|
@@ -356,7 +356,7 @@ export class $RefParser {
|
|
|
356
356
|
}
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
|
-
if (servers.length
|
|
359
|
+
if (servers.length) {
|
|
360
360
|
merged.servers = servers;
|
|
361
361
|
}
|
|
362
362
|
|
|
@@ -568,7 +568,7 @@ export class $RefParser {
|
|
|
568
568
|
}
|
|
569
569
|
}
|
|
570
570
|
|
|
571
|
-
if (tags.length
|
|
571
|
+
if (tags.length) {
|
|
572
572
|
merged.tags = tags;
|
|
573
573
|
}
|
|
574
574
|
|
|
@@ -585,3 +585,17 @@ export class $RefParser {
|
|
|
585
585
|
|
|
586
586
|
export { sendRequest } from './resolvers/url';
|
|
587
587
|
export type { JSONSchema } from './types';
|
|
588
|
+
export type { JSONParserErrorType } from './util/errors';
|
|
589
|
+
export {
|
|
590
|
+
InvalidPointerError,
|
|
591
|
+
isHandledError,
|
|
592
|
+
JSONParserError,
|
|
593
|
+
JSONParserErrorGroup,
|
|
594
|
+
MissingPointerError,
|
|
595
|
+
normalizeError,
|
|
596
|
+
ParserError,
|
|
597
|
+
ResolverError,
|
|
598
|
+
TimeoutError,
|
|
599
|
+
UnmatchedParserError,
|
|
600
|
+
UnmatchedResolverError,
|
|
601
|
+
} from './util/errors';
|
package/src/parsers/yaml.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { JSON_SCHEMA } from 'js-yaml';
|
|
1
|
+
import { parse } from 'yaml';
|
|
3
2
|
|
|
4
3
|
import type { FileInfo, JSONSchema, Plugin } from '../types';
|
|
5
4
|
import { ParserError } from '../util/errors';
|
|
@@ -16,8 +15,7 @@ export const yamlParser: Plugin = {
|
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
try {
|
|
19
|
-
|
|
20
|
-
return yamlSchema;
|
|
18
|
+
return parse(data) as JSONSchema;
|
|
21
19
|
} catch (error: any) {
|
|
22
20
|
throw new ParserError(error?.message || 'Parser Error', file.url);
|
|
23
21
|
}
|
package/src/pointer.ts
CHANGED
|
@@ -140,7 +140,7 @@ class Pointer<S extends object = JSONSchema> {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
if (errors.length
|
|
143
|
+
if (errors.length) {
|
|
144
144
|
throw errors.length === 1
|
|
145
145
|
? errors[0]
|
|
146
146
|
: new AggregateError(errors, 'Multiple missing pointer errors');
|
|
@@ -171,7 +171,7 @@ class Pointer<S extends object = JSONSchema> {
|
|
|
171
171
|
const tokens = Pointer.parse(this.path);
|
|
172
172
|
let token;
|
|
173
173
|
|
|
174
|
-
if (tokens.length
|
|
174
|
+
if (!tokens.length) {
|
|
175
175
|
// There are no tokens, replace the entire object with the new value
|
|
176
176
|
this.value = value;
|
|
177
177
|
return value;
|
|
@@ -205,7 +205,7 @@ class Pointer<S extends object = JSONSchema> {
|
|
|
205
205
|
/**
|
|
206
206
|
* Parses a JSON pointer (or a path containing a JSON pointer in the hash)
|
|
207
207
|
* and returns an array of the pointer's tokens.
|
|
208
|
-
* (e.g
|
|
208
|
+
* (e.g., "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
|
|
209
209
|
*
|
|
210
210
|
* The pointer is parsed according to RFC 6901
|
|
211
211
|
* {@link https://tools.ietf.org/html/rfc6901#section-3}
|
|
@@ -244,8 +244,8 @@ class Pointer<S extends object = JSONSchema> {
|
|
|
244
244
|
/**
|
|
245
245
|
* Creates a JSON pointer path, by joining one or more tokens to a base path.
|
|
246
246
|
*
|
|
247
|
-
* @param base - The base path (e.g
|
|
248
|
-
* @param tokens - The token(s) to append (e.g
|
|
247
|
+
* @param base - The base path (e.g., "schema.json#/definitions/person")
|
|
248
|
+
* @param tokens - The token(s) to append (e.g., ["name", "first"])
|
|
249
249
|
* @returns
|
|
250
250
|
*/
|
|
251
251
|
static join(base: string, tokens: string | string[]) {
|
package/src/ref.ts
CHANGED
|
@@ -46,7 +46,7 @@ class $Ref<S extends object = JSONSchema> {
|
|
|
46
46
|
$refs: $Refs<S>;
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
|
-
* Indicates the type of {@link $Ref#path} (e.g
|
|
49
|
+
* Indicates the type of {@link $Ref#path} (e.g., "file", "http", etc.)
|
|
50
50
|
*/
|
|
51
51
|
pathType: string | unknown;
|
|
52
52
|
|
|
@@ -152,7 +152,7 @@ class $Ref<S extends object = JSONSchema> {
|
|
|
152
152
|
value !== null &&
|
|
153
153
|
'$ref' in value &&
|
|
154
154
|
typeof value.$ref === 'string' &&
|
|
155
|
-
value.$ref.length
|
|
155
|
+
Boolean(value.$ref.length)
|
|
156
156
|
);
|
|
157
157
|
}
|
|
158
158
|
|
package/src/refs.ts
CHANGED
|
@@ -224,7 +224,7 @@ function getPaths<S extends object = JSONSchema>($refs: $RefsMap<S>, types: stri
|
|
|
224
224
|
|
|
225
225
|
// Filter the paths by type
|
|
226
226
|
types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
|
|
227
|
-
if (types.length
|
|
227
|
+
if (types.length && types[0]) {
|
|
228
228
|
paths = paths.filter((key) => types.includes($refs[key]!.pathType as string));
|
|
229
229
|
}
|
|
230
230
|
|