@apidevtools/json-schema-ref-parser 11.2.4 → 11.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 CHANGED
@@ -9,11 +9,23 @@
9
9
  [![License](https://img.shields.io/npm/l/@apidevtools/json-schema-ref-parser.svg)](LICENSE)
10
10
  [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser)
11
11
 
12
+ Installation
13
+ --------------------------
14
+ Install using [npm](https://docs.npmjs.com/about-npm/):
15
+
16
+ ```bash
17
+ npm install @apidevtools/json-schema-ref-parser
18
+ yarn add @apidevtools/json-schema-ref-parser
19
+ bun add @apidevtools/json-schema-ref-parser
20
+ ```
21
+
12
22
  The Problem:
13
23
  --------------------------
14
- You'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.
24
+ You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead
25
+ 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
26
+ format. Maybe some of the files contain cross-references to each other.
15
27
 
16
- ```javascript
28
+ ```json
17
29
  {
18
30
  "definitions": {
19
31
  "person": {
@@ -36,44 +48,41 @@ You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe
36
48
  }
37
49
  ```
38
50
 
39
-
40
51
  The Solution:
41
52
  --------------------------
42
- JSON 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.
53
+ JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
54
+ and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most
55
+ complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward
56
+ JavaScript objects.
43
57
 
44
58
  - Use **JSON** or **YAML** schemas — or even a mix of both!
45
- - Supports `$ref` pointers to external files and URLs, as well as [custom sources](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) such as databases
46
- - Can [bundle](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) multiple files into a single schema that only has _internal_ `$ref` pointers
47
- - Can [dereference](https://apitools.dev/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
48
- - Supports [circular references](https://apitools.dev/json-schema-ref-parser/docs/#circular-refs), nested references, back-references, and cross-references between files
49
- - Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object instance
50
- - Tested in Node v10, v12, & v14, and all major web browsers on Windows, Mac, and Linux
51
-
59
+ - Supports `$ref` pointers to external files and URLs, as well
60
+ as [custom sources](https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html) such as databases
61
+ - Can [bundle](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundlepath-options-callback) multiple
62
+ files into a single schema that only has _internal_ `$ref` pointers
63
+ - Can [dereference](https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferencepath-options-callback)
64
+ your schema, producing a plain-old JavaScript object that's easy to work with
65
+ - Supports [circular references](https://apitools.dev/json-schema-ref-parser/docs/#circular-refs), nested references,
66
+ back-references, and cross-references between files
67
+ - Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object
68
+ instance
69
+ - Compatible with Node LTS and beyond, and all major web browsers on Windows, Mac, and Linux
52
70
 
53
71
  Example
54
72
  --------------------------
55
73
 
56
74
  ```javascript
57
- $RefParser.dereference(mySchema, (err, schema) => {
58
- if (err) {
59
- console.error(err);
60
- }
61
- else {
62
- // `schema` is just a normal JavaScript object that contains your entire JSON Schema,
63
- // including referenced files, combined into a single object
64
- console.log(schema.definitions.person.properties.firstName);
65
- }
66
- })
67
- ```
68
-
69
- Or use `async`/`await` syntax instead. The following example is the same as above:
75
+ import $RefParser from "@apidevtools/json-schema-ref-parser";
70
76
 
71
- ```javascript
72
77
  try {
73
- let schema = await $RefParser.dereference(mySchema);
74
- console.log(schema.definitions.person.properties.firstName);
75
- }
76
- catch(err) {
78
+ await $RefParser.dereference(mySchema);
79
+ // note - by default, mySchema is modified in place, and the returned value is a reference to the same object
80
+ console.log(mySchema.definitions.person.properties.firstName);
81
+
82
+ // if you want to avoid modifying the original schema, you can disable the `mutateInputSchema` option
83
+ let clonedSchema = await $RefParser.dereference(mySchema, { mutateInputSchema: false });
84
+ console.log(clonedSchema.definitions.person.properties.firstName);
85
+ } catch (err) {
77
86
  console.error(err);
78
87
  }
79
88
  ```
@@ -81,99 +90,80 @@ catch(err) {
81
90
  For more detailed examples, please see the [API Documentation](https://apitools.dev/json-schema-ref-parser/docs/)
82
91
 
83
92
 
84
-
85
- Installation
93
+ Polyfills
86
94
  --------------------------
87
- Install using [npm](https://docs.npmjs.com/about-npm/):
88
95
 
89
- ```bash
90
- npm install @apidevtools/json-schema-ref-parser
91
- ```
92
96
 
97
+ If you are using Node.js < 18, you'll need a polyfill for `fetch`,
98
+ like [node-fetch](https://github.com/node-fetch/node-fetch):
93
99
 
94
-
95
- Usage
96
- --------------------------
97
- When using JSON Schema $Ref Parser in Node.js apps, you'll probably want to use **CommonJS** syntax:
98
-
99
- ```javascript
100
- const $RefParser = require("@apidevtools/json-schema-ref-parser");
101
- ```
102
-
103
- When using a transpiler such as [Babel](https://babeljs.io/) or [TypeScript](https://www.typescriptlang.org/), or a bundler such as [Webpack](https://webpack.js.org/) or [Rollup](https://rollupjs.org/), you can use **ECMAScript modules** syntax instead:
104
-
105
- ```javascript
106
- import $RefParser from "@apidevtools/json-schema-ref-parser";
107
- ```
108
-
109
- If you are using Node.js < 18, you'll need a polyfill for `fetch`, like [node-fetch](https://github.com/node-fetch/node-fetch):
110
100
  ```javascript
111
101
  import fetch from "node-fetch";
112
102
 
113
103
  globalThis.fetch = fetch;
114
104
  ```
115
105
 
116
-
117
-
118
106
  Browser support
119
107
  --------------------------
120
- JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill).
121
-
122
- To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/), or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve).
108
+ JSON Schema $Ref Parser supports recent versions of every major web browser. Older browsers may
109
+ require [Babel](https://babeljs.io/) and/or [polyfills](https://babeljs.io/docs/en/next/babel-polyfill).
123
110
 
111
+ To use JSON Schema $Ref Parser in a browser, you'll need to use a bundling tool such
112
+ as [Webpack](https://webpack.js.org/), [Rollup](https://rollupjs.org/), [Parcel](https://parceljs.org/),
113
+ or [Browserify](http://browserify.org/). Some bundlers may require a bit of configuration, such as
114
+ setting `browser: true` in [rollup-plugin-resolve](https://github.com/rollup/rollup-plugin-node-resolve).
124
115
 
125
116
  #### Webpack 5
126
- Webpack 5 has dropped the default export of node core modules in favour of polyfills, you'll need to set them up yourself ( after npm-installing them )
117
+
118
+ Webpack 5 has dropped the default export of node core modules in favour of polyfills, you'll need to set them up
119
+ yourself ( after npm-installing them )
127
120
  Edit your `webpack.config.js` :
121
+
128
122
  ```js
129
- config.resolve.fallback = {
130
- "path": require.resolve("path-browserify"),
131
- 'util': require.resolve('util/'),
132
- 'fs': require.resolve('browserify-fs'),
133
- "buffer": require.resolve("buffer/"),
134
- "http": require.resolve("stream-http"),
135
- "https": require.resolve("https-browserify"),
136
- "url": require.resolve("url"),
137
- }
123
+ config.resolve.fallback = {
124
+ "path": require.resolve("path-browserify"),
125
+ 'fs': require.resolve('browserify-fs')
126
+ }
138
127
 
139
- config.plugins.push(
140
- new webpack.ProvidePlugin({
141
- Buffer: [ 'buffer', 'Buffer']
142
- })
143
- )
128
+ config.plugins.push(
129
+ new webpack.ProvidePlugin({
130
+ Buffer: ['buffer', 'Buffer']
131
+ })
132
+ )
144
133
 
145
134
  ```
146
135
 
147
-
148
136
  API Documentation
149
137
  --------------------------
150
138
  Full API documentation is available [right here](https://apitools.dev/json-schema-ref-parser/docs/)
151
139
 
152
140
 
153
-
154
141
  Contributing
155
142
  --------------------------
156
- I welcome any contributions, enhancements, and bug-fixes. [Open 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).
143
+ I welcome any contributions, enhancements, and
144
+ bug-fixes. [Open an issue](https://github.com/APIDevTools/json-schema-ref-parser/issues) on GitHub
145
+ and [submit a pull request](https://github.com/APIDevTools/json-schema-ref-parser/pulls).
157
146
 
158
147
  #### Building/Testing
148
+
159
149
  To build/test the project locally on your computer:
160
150
 
161
151
  1. __Clone this repo__<br>
162
- `git clone https://github.com/APIDevTools/json-schema-ref-parser.git`
152
+ `git clone https://github.com/APIDevTools/json-schema-ref-parser.git`
163
153
 
164
154
  2. __Install dependencies__<br>
165
- `npm install`
155
+ `yarn install`
166
156
 
167
157
  3. __Run the tests__<br>
168
- `npm test`
169
-
170
-
158
+ `yarn test`
171
159
 
172
160
  License
173
161
  --------------------------
174
162
  JSON Schema $Ref Parser is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.
175
163
 
176
- This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
164
+ This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a
165
+ tree**](https://plant.treeware.earth/APIDevTools/json-schema-ref-parser) to thank us for our work. By contributing to
166
+ the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
177
167
 
178
168
 
179
169
 
@@ -129,7 +129,6 @@ function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedC
129
129
  * @returns
130
130
  */
131
131
  function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options) {
132
- // console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
133
132
  const isExternalRef = ref_js_1.default.isExternal$Ref($ref);
134
133
  const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
135
134
  const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
@@ -1,9 +1,15 @@
1
1
  import $Refs from "./refs.js";
2
2
  import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors.js";
3
3
  import type { ParserOptions } from "./options.js";
4
- import type { Plugin, $RefsCallback, JSONSchema, SchemaCallback, HTTPResolverOptions, FileInfo, ResolverOptions, JSONSchemaObject } from "./types/index.js";
5
- export { JSONSchemaObject, ResolverOptions, ParserError, UnmatchedResolverError, ResolverError, HTTPResolverOptions, FileInfo, UnmatchedParserError, ParserOptions, MissingPointerError, InvalidPointerError, JSONParserError, Plugin, JSONSchema, $RefsCallback, SchemaCallback, };
6
- export type RefParserSchema = string | JSONSchema;
4
+ import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";
5
+ export { JSONParserError };
6
+ export { InvalidPointerError };
7
+ export { MissingPointerError };
8
+ export { ResolverError };
9
+ export { ParserError };
10
+ export { UnmatchedParserError };
11
+ export { UnmatchedResolverError };
12
+ type RefParserSchema = string | JSONSchema;
7
13
  /**
8
14
  * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
9
15
  * and provides methods for traversing, manipulating, and dereferencing those references.
package/dist/lib/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.dereference = exports.bundle = exports.resolve = exports.parse = exports.$RefParser = exports.JSONParserError = exports.InvalidPointerError = exports.MissingPointerError = exports.UnmatchedParserError = exports.ResolverError = exports.UnmatchedResolverError = exports.ParserError = void 0;
29
+ exports.dereference = exports.bundle = exports.resolve = exports.parse = exports.$RefParser = exports.UnmatchedResolverError = exports.UnmatchedParserError = exports.ParserError = exports.ResolverError = exports.MissingPointerError = exports.InvalidPointerError = exports.JSONParserError = void 0;
30
30
  const refs_js_1 = __importDefault(require("./refs.js"));
31
31
  const parse_js_1 = __importDefault(require("./parse.js"));
32
32
  const normalize_args_js_1 = __importDefault(require("./normalize-args.js"));
@@ -38,6 +38,10 @@ function normalizeArgs(_args) {
38
38
  catch (e) {
39
39
  console.log(e);
40
40
  }
41
+ if (!options.mutateInputSchema) {
42
+ // Make a deep clone of the schema, so that we don't alter the original object
43
+ schema = JSON.parse(JSON.stringify(schema));
44
+ }
41
45
  return {
42
46
  path,
43
47
  schema,
@@ -77,6 +77,12 @@ interface $RefParserOptions {
77
77
  * Default: `relative`
78
78
  */
79
79
  externalReferenceResolution?: "relative" | "root";
80
+ /**
81
+ * Whether to clone the schema before dereferencing it.
82
+ * This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
83
+ * Default: `true` due to mutating the input being the default behavior historically
84
+ */
85
+ mutateInputSchema?: boolean;
80
86
  };
81
87
  }
82
88
  export declare const getNewOptions: (options: DeepPartial<$RefParserOptions>) => $RefParserOptions;
@@ -70,6 +70,7 @@ const getDefaults = () => {
70
70
  excludedPathMatcher: () => false,
71
71
  referenceResolution: "relative",
72
72
  },
73
+ mutateInputSchema: true,
73
74
  };
74
75
  return defaults;
75
76
  };
package/dist/lib/parse.js CHANGED
@@ -33,13 +33,20 @@ exports.default = parse;
33
33
  */
34
34
  async function parse(path, $refs, options) {
35
35
  // Remove the URL fragment, if any
36
- path = url.stripHash(path);
36
+ const hashIndex = path.indexOf("#");
37
+ let hash = "";
38
+ if (hashIndex >= 0) {
39
+ hash = path.substring(hashIndex);
40
+ // Remove the URL fragment, if any
41
+ path = path.substring(0, hashIndex);
42
+ }
37
43
  // Add a new $Ref for this file, even though we don't have the value yet.
38
44
  // This ensures that we don't simultaneously read & parse the same file multiple times
39
45
  const $ref = $refs._add(path);
40
46
  // This "file object" will be passed to all resolvers and parsers.
41
47
  const file = {
42
48
  url: path,
49
+ hash,
43
50
  extension: url.getExtension(path),
44
51
  };
45
52
  // Read the file and then parse the data
@@ -111,7 +118,6 @@ async function readFile(file, options, $refs) {
111
118
  * The promise resolves with the parsed file contents and the parser that was used.
112
119
  */
113
120
  async function parseFile(file, options, $refs) {
114
- // console.log('Parsing %s', file.url);
115
121
  // Find the parsers that can read this file type.
116
122
  // If none of the parsers are an exact match for this file, then we'll try ALL of them.
117
123
  // This handles situations where the file IS a supported type, just with an unknown extension.
@@ -17,6 +17,10 @@ exports.default = {
17
17
  * every parser will be tried.
18
18
  */
19
19
  canParse: ".json",
20
+ /**
21
+ * Allow JSON files with byte order marks (BOM)
22
+ */
23
+ allowBOM: true,
20
24
  /**
21
25
  * Parses the given file as JSON
22
26
  */
@@ -34,6 +38,18 @@ exports.default = {
34
38
  return JSON.parse(data);
35
39
  }
36
40
  catch (e) {
41
+ if (this.allowBOM) {
42
+ try {
43
+ // find the first curly brace
44
+ const firstCurlyBrace = data.indexOf("{");
45
+ // remove any characters before the first curly brace
46
+ data = data.slice(firstCurlyBrace);
47
+ return JSON.parse(data);
48
+ }
49
+ catch (e) {
50
+ throw new errors_js_1.ParserError(e.message, file.url);
51
+ }
52
+ }
37
53
  throw new errors_js_1.ParserError(e.message, file.url);
38
54
  }
39
55
  }
@@ -36,7 +36,7 @@ declare class Pointer {
36
36
  * Resolving a single pointer may require resolving multiple $Refs.
37
37
  */
38
38
  indirections: number;
39
- constructor($ref: any, path: any, friendlyPath?: string);
39
+ constructor($ref: $Ref, path: string, friendlyPath?: string);
40
40
  /**
41
41
  * Resolves the value of a nested property within the given object.
42
42
  *
@@ -74,7 +74,7 @@ declare class Pointer {
74
74
  * @param [originalPath]
75
75
  * @returns
76
76
  */
77
- static parse(path: string, originalPath?: string): any;
77
+ static parse(path: string, originalPath?: string): string[];
78
78
  /**
79
79
  * Creates a JSON pointer path, by joining one or more tokens to a base path.
80
80
  *
@@ -85,6 +85,20 @@ class Pointer {
85
85
  }
86
86
  const token = tokens[i];
87
87
  if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
88
+ // one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again
89
+ let didFindSubstringSlashMatch = false;
90
+ for (let j = tokens.length - 1; j > i; j--) {
91
+ const joinedToken = tokens.slice(i, j + 1).join("/");
92
+ if (this.value[joinedToken] !== undefined) {
93
+ this.value = this.value[joinedToken];
94
+ i = j;
95
+ didFindSubstringSlashMatch = true;
96
+ break;
97
+ }
98
+ }
99
+ if (didFindSubstringSlashMatch) {
100
+ continue;
101
+ }
88
102
  this.value = null;
89
103
  throw new errors_js_1.MissingPointerError(token, decodeURI(this.originalPath));
90
104
  }
@@ -151,22 +165,22 @@ class Pointer {
151
165
  */
152
166
  static parse(path, originalPath) {
153
167
  // Get the JSON pointer from the path's hash
154
- let pointer = url.getHash(path).substr(1);
168
+ const pointer = url.getHash(path).substring(1);
155
169
  // If there's no pointer, then there are no tokens,
156
170
  // so return an empty array
157
171
  if (!pointer) {
158
172
  return [];
159
173
  }
160
174
  // Split into an array
161
- pointer = pointer.split("/");
175
+ const split = pointer.split("/");
162
176
  // Decode each part, according to RFC 6901
163
- for (let i = 0; i < pointer.length; i++) {
164
- pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
177
+ for (let i = 0; i < split.length; i++) {
178
+ split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
165
179
  }
166
- if (pointer[0] !== "") {
167
- throw new errors_js_1.InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
180
+ if (split[0] !== "") {
181
+ throw new errors_js_1.InvalidPointerError(split, originalPath === undefined ? path : originalPath);
168
182
  }
169
- return pointer.slice(1);
183
+ return split.slice(1);
170
184
  }
171
185
  /**
172
186
  * Creates a JSON pointer path, by joining one or more tokens to a base path.
@@ -64,6 +64,12 @@ export interface Plugin {
64
64
  * You can set `allowEmpty: false` on any parser, which will cause an error to be thrown if a file empty.
65
65
  */
66
66
  allowEmpty?: boolean;
67
+ /**
68
+ * Specifies whether a Byte Order Mark (BOM) is allowed or not. Only applies to JSON parsing
69
+ *
70
+ * @type {boolean} @default true
71
+ */
72
+ allowBOM?: boolean;
67
73
  /**
68
74
  * The encoding that the text is expected to be in.
69
75
  */
@@ -93,6 +99,10 @@ export interface FileInfo {
93
99
  * The full URL of the file. This could be any type of URL, including "http://", "https://", "file://", "ftp://", "mongodb://", or even a local filesystem path (when running in Node.js).
94
100
  */
95
101
  url: string;
102
+ /**
103
+ * The hash (URL fragment) of the file URL, including the # symbol. If the URL doesn't have a hash, then this will be an empty string.
104
+ */
105
+ hash: string;
96
106
  /**
97
107
  * The lowercase file extension, such as ".json", ".yaml", ".txt", etc.
98
108
  */
@@ -40,21 +40,21 @@ export declare function stripQuery(path: any): any;
40
40
  * @param path
41
41
  * @returns
42
42
  */
43
- export declare function getHash(path: any): any;
43
+ export declare function getHash(path: undefined | string): string;
44
44
  /**
45
45
  * Removes the hash (URL fragment), if any, from the given path.
46
46
  *
47
47
  * @param path
48
48
  * @returns
49
49
  */
50
- export declare function stripHash(path: any): any;
50
+ export declare function stripHash(path?: string | undefined): string;
51
51
  /**
52
52
  * Determines whether the given path is an HTTP(S) URL.
53
53
  *
54
54
  * @param path
55
55
  * @returns
56
56
  */
57
- export declare function isHttp(path: any): boolean;
57
+ export declare function isHttp(path: string): boolean;
58
58
  /**
59
59
  * Determines whether the given path is a filesystem path.
60
60
  * This includes "file://" URLs.
@@ -52,12 +52,13 @@ exports.parse = parse;
52
52
  function resolve(from, to) {
53
53
  const fromUrl = new URL((0, convert_path_to_posix_1.default)(from), "resolve://");
54
54
  const resolvedUrl = new URL((0, convert_path_to_posix_1.default)(to), fromUrl);
55
+ const endSpaces = to.match(/(\s*)$/)?.[1] || "";
55
56
  if (resolvedUrl.protocol === "resolve:") {
56
57
  // `from` is a relative URL.
57
58
  const { pathname, search, hash } = resolvedUrl;
58
- return pathname + search + hash;
59
+ return pathname + search + hash + endSpaces;
59
60
  }
60
- return resolvedUrl.toString();
61
+ return resolvedUrl.toString() + endSpaces;
61
62
  }
62
63
  exports.resolve = resolve;
63
64
  /**
@@ -129,9 +130,12 @@ exports.stripQuery = stripQuery;
129
130
  * @returns
130
131
  */
131
132
  function getHash(path) {
133
+ if (!path) {
134
+ return "#";
135
+ }
132
136
  const hashIndex = path.indexOf("#");
133
137
  if (hashIndex >= 0) {
134
- return path.substr(hashIndex);
138
+ return path.substring(hashIndex);
135
139
  }
136
140
  return "#";
137
141
  }
@@ -143,9 +147,12 @@ exports.getHash = getHash;
143
147
  * @returns
144
148
  */
145
149
  function stripHash(path) {
150
+ if (!path) {
151
+ return "";
152
+ }
146
153
  const hashIndex = path.indexOf("#");
147
154
  if (hashIndex >= 0) {
148
- path = path.substr(0, hashIndex);
155
+ path = path.substring(0, hashIndex);
149
156
  }
150
157
  return path;
151
158
  }
@@ -159,16 +159,14 @@ function crawl(
159
159
  */
160
160
  function dereference$Ref(
161
161
  $ref: any,
162
- path: any,
163
- pathFromRoot: any,
164
- parents: any,
162
+ path: string,
163
+ pathFromRoot: string,
164
+ parents: Set<any>,
165
165
  processedObjects: any,
166
166
  dereferencedCache: any,
167
- $refs: any,
168
- options: any,
167
+ $refs: $Refs,
168
+ options: $RefParserOptions,
169
169
  ) {
170
- // console.log('Dereferencing $ref pointer "%s" at %s', $ref.$ref, path);
171
-
172
170
  const isExternalRef = $Ref.isExternal$Ref($ref);
173
171
  const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
174
172
  const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
package/lib/index.ts CHANGED
@@ -19,37 +19,17 @@ import {
19
19
  import { ono } from "@jsdevtools/ono";
20
20
  import maybe from "./util/maybe.js";
21
21
  import type { ParserOptions } from "./options.js";
22
- import type {
23
- Plugin,
24
- $RefsCallback,
25
- JSONSchema,
26
- SchemaCallback,
27
- HTTPResolverOptions,
28
- FileInfo,
29
- ResolverOptions,
30
- JSONSchemaObject,
31
- } from "./types/index.js";
22
+ import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";
32
23
 
33
- export {
34
- JSONSchemaObject,
35
- ResolverOptions,
36
- ParserError,
37
- UnmatchedResolverError,
38
- ResolverError,
39
- HTTPResolverOptions,
40
- FileInfo,
41
- UnmatchedParserError,
42
- ParserOptions,
43
- MissingPointerError,
44
- InvalidPointerError,
45
- JSONParserError,
46
- Plugin,
47
- JSONSchema,
48
- $RefsCallback,
49
- SchemaCallback,
50
- };
24
+ export { JSONParserError };
25
+ export { InvalidPointerError };
26
+ export { MissingPointerError };
27
+ export { ResolverError };
28
+ export { ParserError };
29
+ export { UnmatchedParserError };
30
+ export { UnmatchedResolverError };
51
31
 
52
- export type RefParserSchema = string | JSONSchema;
32
+ type RefParserSchema = string | JSONSchema;
53
33
 
54
34
  /**
55
35
  * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
@@ -39,6 +39,11 @@ function normalizeArgs(_args: Partial<IArguments>) {
39
39
  console.log(e);
40
40
  }
41
41
 
42
+ if (!options.mutateInputSchema) {
43
+ // Make a deep clone of the schema, so that we don't alter the original object
44
+ schema = JSON.parse(JSON.stringify(schema));
45
+ }
46
+
42
47
  return {
43
48
  path,
44
49
  schema,
package/lib/options.ts CHANGED
@@ -91,6 +91,13 @@ interface $RefParserOptions {
91
91
  * Default: `relative`
92
92
  */
93
93
  externalReferenceResolution?: "relative" | "root";
94
+
95
+ /**
96
+ * Whether to clone the schema before dereferencing it.
97
+ * This is useful when you want to dereference the same schema multiple times, but you don't want to modify the original schema.
98
+ * Default: `true` due to mutating the input being the default behavior historically
99
+ */
100
+ mutateInputSchema?: boolean;
94
101
  };
95
102
  }
96
103
 
@@ -159,6 +166,8 @@ const getDefaults = () => {
159
166
  excludedPathMatcher: () => false,
160
167
  referenceResolution: "relative",
161
168
  },
169
+
170
+ mutateInputSchema: true,
162
171
  } as $RefParserOptions;
163
172
  return defaults;
164
173
  };
package/lib/parse.ts CHANGED
@@ -19,7 +19,13 @@ export default parse;
19
19
  */
20
20
  async function parse(path: string, $refs: $Refs, options: Options) {
21
21
  // Remove the URL fragment, if any
22
- path = url.stripHash(path);
22
+ const hashIndex = path.indexOf("#");
23
+ let hash = "";
24
+ if (hashIndex >= 0) {
25
+ hash = path.substring(hashIndex);
26
+ // Remove the URL fragment, if any
27
+ path = path.substring(0, hashIndex);
28
+ }
23
29
 
24
30
  // Add a new $Ref for this file, even though we don't have the value yet.
25
31
  // This ensures that we don't simultaneously read & parse the same file multiple times
@@ -28,6 +34,7 @@ async function parse(path: string, $refs: $Refs, options: Options) {
28
34
  // This "file object" will be passed to all resolvers and parsers.
29
35
  const file = {
30
36
  url: path,
37
+ hash,
31
38
  extension: url.getExtension(path),
32
39
  } as FileInfo;
33
40
 
@@ -103,8 +110,6 @@ async function readFile(file: FileInfo, options: Options, $refs: $Refs): Promise
103
110
  * The promise resolves with the parsed file contents and the parser that was used.
104
111
  */
105
112
  async function parseFile(file: FileInfo, options: Options, $refs: $Refs) {
106
- // console.log('Parsing %s', file.url);
107
-
108
113
  // Find the parsers that can read this file type.
109
114
  // If none of the parsers are an exact match for this file, then we'll try ALL of them.
110
115
  // This handles situations where the file IS a supported type, just with an unknown extension.
@@ -21,6 +21,11 @@ export default {
21
21
  */
22
22
  canParse: ".json",
23
23
 
24
+ /**
25
+ * Allow JSON files with byte order marks (BOM)
26
+ */
27
+ allowBOM: true,
28
+
24
29
  /**
25
30
  * Parses the given file as JSON
26
31
  */
@@ -37,6 +42,17 @@ export default {
37
42
  try {
38
43
  return JSON.parse(data);
39
44
  } catch (e: any) {
45
+ if (this.allowBOM) {
46
+ try {
47
+ // find the first curly brace
48
+ const firstCurlyBrace = data.indexOf("{");
49
+ // remove any characters before the first curly brace
50
+ data = data.slice(firstCurlyBrace);
51
+ return JSON.parse(data);
52
+ } catch (e: any) {
53
+ throw new ParserError(e.message, file.url);
54
+ }
55
+ }
40
56
  throw new ParserError(e.message, file.url);
41
57
  }
42
58
  }
package/lib/pointer.ts CHANGED
@@ -3,6 +3,7 @@ import type $RefParserOptions from "./options.js";
3
3
  import $Ref from "./ref.js";
4
4
  import * as url from "./util/url.js";
5
5
  import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
6
+
6
7
  const slashes = /\//g;
7
8
  const tildes = /~/g;
8
9
  const escapedSlash = /~1/g;
@@ -57,7 +58,7 @@ class Pointer {
57
58
  */
58
59
  indirections: number;
59
60
 
60
- constructor($ref: any, path: any, friendlyPath?: string) {
61
+ constructor($ref: $Ref, path: string, friendlyPath?: string) {
61
62
  this.$ref = $ref;
62
63
 
63
64
  this.path = path;
@@ -102,6 +103,21 @@ class Pointer {
102
103
 
103
104
  const token = tokens[i];
104
105
  if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
106
+ // one final case is if the entry itself includes slashes, and was parsed out as a token - we can join the remaining tokens and try again
107
+ let didFindSubstringSlashMatch = false;
108
+ for (let j = tokens.length - 1; j > i; j--) {
109
+ const joinedToken = tokens.slice(i, j + 1).join("/");
110
+ if (this.value[joinedToken] !== undefined) {
111
+ this.value = this.value[joinedToken];
112
+ i = j;
113
+ didFindSubstringSlashMatch = true;
114
+ break;
115
+ }
116
+ }
117
+ if (didFindSubstringSlashMatch) {
118
+ continue;
119
+ }
120
+
105
121
  this.value = null;
106
122
  throw new MissingPointerError(token, decodeURI(this.originalPath));
107
123
  } else {
@@ -174,9 +190,9 @@ class Pointer {
174
190
  * @param [originalPath]
175
191
  * @returns
176
192
  */
177
- static parse(path: string, originalPath?: string) {
193
+ static parse(path: string, originalPath?: string): string[] {
178
194
  // Get the JSON pointer from the path's hash
179
- let pointer = url.getHash(path).substr(1);
195
+ const pointer = url.getHash(path).substring(1);
180
196
 
181
197
  // If there's no pointer, then there are no tokens,
182
198
  // so return an empty array
@@ -185,18 +201,18 @@ class Pointer {
185
201
  }
186
202
 
187
203
  // Split into an array
188
- pointer = pointer.split("/");
204
+ const split = pointer.split("/");
189
205
 
190
206
  // Decode each part, according to RFC 6901
191
- for (let i = 0; i < pointer.length; i++) {
192
- pointer[i] = safeDecodeURIComponent(pointer[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
207
+ for (let i = 0; i < split.length; i++) {
208
+ split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
193
209
  }
194
210
 
195
- if (pointer[0] !== "") {
196
- throw new InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
211
+ if (split[0] !== "") {
212
+ throw new InvalidPointerError(split, originalPath === undefined ? path : originalPath);
197
213
  }
198
214
 
199
- return pointer.slice(1);
215
+ return split.slice(1);
200
216
  }
201
217
 
202
218
  /**
@@ -88,6 +88,13 @@ export interface Plugin {
88
88
  */
89
89
  allowEmpty?: boolean;
90
90
 
91
+ /**
92
+ * Specifies whether a Byte Order Mark (BOM) is allowed or not. Only applies to JSON parsing
93
+ *
94
+ * @type {boolean} @default true
95
+ */
96
+ allowBOM?: boolean;
97
+
91
98
  /**
92
99
  * The encoding that the text is expected to be in.
93
100
  */
@@ -123,6 +130,11 @@ export interface FileInfo {
123
130
  */
124
131
  url: string;
125
132
 
133
+ /**
134
+ * The hash (URL fragment) of the file URL, including the # symbol. If the URL doesn't have a hash, then this will be an empty string.
135
+ */
136
+ hash: string;
137
+
126
138
  /**
127
139
  * The lowercase file extension, such as ".json", ".yaml", ".txt", etc.
128
140
  */
package/lib/util/url.ts CHANGED
@@ -28,12 +28,13 @@ export const parse = (u: string | URL) => new URL(u);
28
28
  export function resolve(from: string, to: string) {
29
29
  const fromUrl = new URL(convertPathToPosix(from), "resolve://");
30
30
  const resolvedUrl = new URL(convertPathToPosix(to), fromUrl);
31
+ const endSpaces = to.match(/(\s*)$/)?.[1] || "";
31
32
  if (resolvedUrl.protocol === "resolve:") {
32
33
  // `from` is a relative URL.
33
34
  const { pathname, search, hash } = resolvedUrl;
34
- return pathname + search + hash;
35
+ return pathname + search + hash + endSpaces;
35
36
  }
36
- return resolvedUrl.toString();
37
+ return resolvedUrl.toString() + endSpaces;
37
38
  }
38
39
 
39
40
  /**
@@ -105,10 +106,13 @@ export function stripQuery(path: any) {
105
106
  * @param path
106
107
  * @returns
107
108
  */
108
- export function getHash(path: any) {
109
+ export function getHash(path: undefined | string) {
110
+ if (!path) {
111
+ return "#";
112
+ }
109
113
  const hashIndex = path.indexOf("#");
110
114
  if (hashIndex >= 0) {
111
- return path.substr(hashIndex);
115
+ return path.substring(hashIndex);
112
116
  }
113
117
  return "#";
114
118
  }
@@ -119,10 +123,13 @@ export function getHash(path: any) {
119
123
  * @param path
120
124
  * @returns
121
125
  */
122
- export function stripHash(path: any) {
126
+ export function stripHash(path?: string | undefined) {
127
+ if (!path) {
128
+ return "";
129
+ }
123
130
  const hashIndex = path.indexOf("#");
124
131
  if (hashIndex >= 0) {
125
- path = path.substr(0, hashIndex);
132
+ path = path.substring(0, hashIndex);
126
133
  }
127
134
  return path;
128
135
  }
@@ -133,7 +140,7 @@ export function stripHash(path: any) {
133
140
  * @param path
134
141
  * @returns
135
142
  */
136
- export function isHttp(path: any) {
143
+ export function isHttp(path: string) {
137
144
  const protocol = getProtocol(path);
138
145
  if (protocol === "http" || protocol === "https") {
139
146
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apidevtools/json-schema-ref-parser",
3
- "version": "11.2.4",
3
+ "version": "11.4.0",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "keywords": [
6
6
  "json",
package/lib/tsconfig.json DELETED
@@ -1,103 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Visit https://aka.ms/tsconfig to read more about this file */
4
-
5
- /* Projects */
6
- // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7
- // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
- // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
9
- // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
10
- // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
- // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
-
13
- /* Language and Environment */
14
- "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
- // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
- // "jsx": "preserve", /* Specify what JSX code is generated. */
17
- // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
- // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19
- // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
20
- // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
- // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
22
- // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
23
- // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
- // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
- // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
-
27
- /* Modules */
28
- "module": "commonjs", /* Specify what module code is generated. */
29
- // "rootDir": "./", /* Specify the root folder within your source files. */
30
- // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
31
- // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
32
- // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
33
- // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
34
- // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
35
- // "types": [], /* Specify type package names to be included without being referenced in a source file. */
36
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
37
- // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38
- // "resolveJsonModule": true, /* Enable importing .json files. */
39
- // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
40
-
41
- /* JavaScript Support */
42
- // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
43
- // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
44
- // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
45
-
46
- /* Emit */
47
- // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
48
- // "declarationMap": true, /* Create sourcemaps for d.ts files. */
49
- // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
50
- // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
51
- // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
52
- // "outDir": "./", /* Specify an output folder for all emitted files. */
53
- // "removeComments": true, /* Disable emitting comments. */
54
- // "noEmit": true, /* Disable emitting files from a compilation. */
55
- // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
56
- // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
57
- // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
58
- // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
59
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
60
- // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
61
- // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
62
- // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
63
- // "newLine": "crlf", /* Set the newline character for emitting files. */
64
- // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
65
- // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
66
- // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
67
- // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
68
- // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
69
- // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
70
-
71
- /* Interop Constraints */
72
- // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
73
- // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
74
- "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
75
- // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
76
- "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
77
-
78
- /* Type Checking */
79
- "strict": true, /* Enable all strict type-checking options. */
80
- // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
81
- // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
82
- // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
83
- // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
84
- // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
85
- // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
86
- // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
87
- // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
88
- // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
89
- // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
90
- // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
91
- // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
92
- // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
93
- // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
94
- // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
95
- // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
96
- // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
97
- // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
98
-
99
- /* Completeness */
100
- // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
101
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
102
- }
103
- }