@hey-api/json-schema-ref-parser 1.2.4 → 1.3.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.
Files changed (92) hide show
  1. package/README.md +9 -84
  2. package/dist/index.d.mts +629 -0
  3. package/dist/index.d.mts.map +1 -0
  4. package/dist/index.mjs +1887 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/package.json +42 -78
  7. package/src/__tests__/bundle.test.ts +59 -0
  8. package/src/__tests__/index.test.ts +43 -0
  9. package/src/__tests__/pointer.test.ts +34 -0
  10. package/src/__tests__/utils.ts +3 -0
  11. package/{lib → src}/bundle.ts +143 -229
  12. package/{lib → src}/dereference.ts +20 -43
  13. package/{lib → src}/index.ts +103 -125
  14. package/{lib → src}/options.ts +13 -9
  15. package/{lib → src}/parse.ts +19 -15
  16. package/src/parsers/binary.ts +13 -0
  17. package/{lib → src}/parsers/json.ts +5 -6
  18. package/src/parsers/text.ts +21 -0
  19. package/{lib → src}/parsers/yaml.ts +9 -9
  20. package/{lib → src}/pointer.ts +42 -23
  21. package/{lib → src}/ref.ts +25 -21
  22. package/{lib → src}/refs.ts +23 -26
  23. package/{lib → src}/resolve-external.ts +91 -60
  24. package/{lib → src}/resolvers/file.ts +7 -10
  25. package/{lib → src}/resolvers/url.ts +12 -8
  26. package/{lib → src}/types/index.ts +9 -2
  27. package/src/util/convert-path-to-posix.ts +8 -0
  28. package/{lib → src}/util/errors.ts +38 -36
  29. package/{lib → src}/util/is-windows.ts +1 -1
  30. package/{lib → src}/util/plugins.ts +7 -8
  31. package/{lib → src}/util/url.ts +41 -42
  32. package/dist/lib/__tests__/bundle.test.d.ts +0 -1
  33. package/dist/lib/__tests__/bundle.test.js +0 -50
  34. package/dist/lib/__tests__/index.test.d.ts +0 -1
  35. package/dist/lib/__tests__/index.test.js +0 -43
  36. package/dist/lib/__tests__/pointer.test.d.ts +0 -1
  37. package/dist/lib/__tests__/pointer.test.js +0 -27
  38. package/dist/lib/bundle.d.ts +0 -26
  39. package/dist/lib/bundle.js +0 -600
  40. package/dist/lib/dereference.d.ts +0 -11
  41. package/dist/lib/dereference.js +0 -226
  42. package/dist/lib/index.d.ts +0 -92
  43. package/dist/lib/index.js +0 -525
  44. package/dist/lib/options.d.ts +0 -61
  45. package/dist/lib/options.js +0 -45
  46. package/dist/lib/parse.d.ts +0 -13
  47. package/dist/lib/parse.js +0 -87
  48. package/dist/lib/parsers/binary.d.ts +0 -2
  49. package/dist/lib/parsers/binary.js +0 -12
  50. package/dist/lib/parsers/json.d.ts +0 -2
  51. package/dist/lib/parsers/json.js +0 -38
  52. package/dist/lib/parsers/text.d.ts +0 -2
  53. package/dist/lib/parsers/text.js +0 -18
  54. package/dist/lib/parsers/yaml.d.ts +0 -2
  55. package/dist/lib/parsers/yaml.js +0 -28
  56. package/dist/lib/pointer.d.ts +0 -88
  57. package/dist/lib/pointer.js +0 -297
  58. package/dist/lib/ref.d.ts +0 -180
  59. package/dist/lib/ref.js +0 -226
  60. package/dist/lib/refs.d.ts +0 -127
  61. package/dist/lib/refs.js +0 -232
  62. package/dist/lib/resolve-external.d.ts +0 -13
  63. package/dist/lib/resolve-external.js +0 -151
  64. package/dist/lib/resolvers/file.d.ts +0 -6
  65. package/dist/lib/resolvers/file.js +0 -61
  66. package/dist/lib/resolvers/url.d.ts +0 -17
  67. package/dist/lib/resolvers/url.js +0 -62
  68. package/dist/lib/types/index.d.ts +0 -43
  69. package/dist/lib/types/index.js +0 -2
  70. package/dist/lib/util/convert-path-to-posix.d.ts +0 -1
  71. package/dist/lib/util/convert-path-to-posix.js +0 -14
  72. package/dist/lib/util/errors.d.ts +0 -56
  73. package/dist/lib/util/errors.js +0 -112
  74. package/dist/lib/util/is-windows.d.ts +0 -1
  75. package/dist/lib/util/is-windows.js +0 -6
  76. package/dist/lib/util/plugins.d.ts +0 -16
  77. package/dist/lib/util/plugins.js +0 -45
  78. package/dist/lib/util/url.d.ts +0 -79
  79. package/dist/lib/util/url.js +0 -285
  80. package/dist/vite.config.d.ts +0 -2
  81. package/dist/vite.config.js +0 -19
  82. package/lib/__tests__/bundle.test.ts +0 -52
  83. package/lib/__tests__/index.test.ts +0 -45
  84. package/lib/__tests__/pointer.test.ts +0 -26
  85. package/lib/__tests__/spec/circular-ref-with-description.json +0 -11
  86. package/lib/__tests__/spec/multiple-refs.json +0 -34
  87. package/lib/__tests__/spec/openapi-paths-ref.json +0 -46
  88. package/lib/__tests__/spec/path-parameter.json +0 -16
  89. package/lib/parsers/binary.ts +0 -13
  90. package/lib/parsers/text.ts +0 -21
  91. package/lib/util/convert-path-to-posix.ts +0 -11
  92. /package/{LICENSE → LICENSE.md} +0 -0
package/dist/lib/parse.js DELETED
@@ -1,87 +0,0 @@
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.parseFile = void 0;
37
- exports.newFile = newFile;
38
- const ono_1 = require("@jsdevtools/ono");
39
- const url_js_1 = require("./util/url.js");
40
- const plugins = __importStar(require("./util/plugins.js"));
41
- const errors_js_1 = require("./util/errors.js");
42
- /**
43
- * Prepares the file object so we can populate it with data and other values
44
- * when it's read and parsed. This "file object" will be passed to all
45
- * resolvers and parsers.
46
- */
47
- function newFile(path) {
48
- let url = path;
49
- // Remove the URL fragment, if any
50
- const hashIndex = url.indexOf("#");
51
- let hash = "";
52
- if (hashIndex > -1) {
53
- hash = url.substring(hashIndex);
54
- url = url.substring(0, hashIndex);
55
- }
56
- return {
57
- extension: (0, url_js_1.getExtension)(url),
58
- hash,
59
- url,
60
- };
61
- }
62
- /**
63
- * Parses the given file's contents, using the configured parser plugins.
64
- */
65
- const parseFile = async (file, options) => {
66
- try {
67
- // If none of the parsers are a match for this file, try all of them. This
68
- // handles situations where the file is a supported type, just with an
69
- // unknown extension.
70
- const parsers = [options.parse.json, options.parse.yaml, options.parse.text, options.parse.binary];
71
- const filtered = parsers.filter((plugin) => plugin.canHandle(file));
72
- return await plugins.run(filtered.length ? filtered : parsers, file);
73
- }
74
- catch (error) {
75
- if (error && error.message && error.message.startsWith("Error parsing")) {
76
- throw error;
77
- }
78
- if (!error || !("error" in error)) {
79
- throw ono_1.ono.syntax(`Unable to parse ${file.url}`);
80
- }
81
- if (error.error instanceof errors_js_1.ParserError) {
82
- throw error.error;
83
- }
84
- throw new errors_js_1.ParserError(error.error.message, file.url);
85
- }
86
- };
87
- exports.parseFile = parseFile;
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "../types/index.js";
2
- export declare const binaryParser: Plugin;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.binaryParser = void 0;
4
- const BINARY_REGEXP = /\.(jpeg|jpg|gif|png|bmp|ico)$/i;
5
- exports.binaryParser = {
6
- canHandle: (file) => Buffer.isBuffer(file.data) && BINARY_REGEXP.test(file.url),
7
- handler: (file) => Buffer.isBuffer(file.data)
8
- ? file.data
9
- // This will reject if data is anything other than a string or typed array
10
- : Buffer.from(file.data),
11
- name: 'binary',
12
- };
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "../types/index.js";
2
- export declare const jsonParser: Plugin;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonParser = void 0;
4
- const errors_js_1 = require("../util/errors.js");
5
- exports.jsonParser = {
6
- canHandle: (file) => file.extension === '.json',
7
- async handler(file) {
8
- let data = file.data;
9
- if (Buffer.isBuffer(data)) {
10
- data = data.toString();
11
- }
12
- if (typeof data !== "string") {
13
- // data is already a JavaScript value (object, array, number, null, NaN, etc.)
14
- return data;
15
- }
16
- if (!data.trim().length) {
17
- // this mirrors the YAML behavior
18
- return;
19
- }
20
- try {
21
- return JSON.parse(data);
22
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
23
- }
24
- catch (error) {
25
- try {
26
- // find the first curly brace
27
- const firstCurlyBrace = data.indexOf("{");
28
- // remove any characters before the first curly brace
29
- data = data.slice(firstCurlyBrace);
30
- return JSON.parse(data);
31
- }
32
- catch (error) {
33
- throw new errors_js_1.ParserError(error.message, file.url);
34
- }
35
- }
36
- },
37
- name: 'json',
38
- };
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "../types/index.js";
2
- export declare const textParser: Plugin;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.textParser = void 0;
4
- const errors_js_1 = require("../util/errors.js");
5
- const TEXT_REGEXP = /\.(txt|htm|html|md|xml|js|min|map|css|scss|less|svg)$/i;
6
- exports.textParser = {
7
- canHandle: (file) => (typeof file.data === "string" || Buffer.isBuffer(file.data)) && TEXT_REGEXP.test(file.url),
8
- handler(file) {
9
- if (typeof file.data === "string") {
10
- return file.data;
11
- }
12
- if (!Buffer.isBuffer(file.data)) {
13
- throw new errors_js_1.ParserError("data is not text", file.url);
14
- }
15
- return file.data.toString('utf-8');
16
- },
17
- name: 'text',
18
- };
@@ -1,2 +0,0 @@
1
- import type { Plugin } from "../types/index.js";
2
- export declare const yamlParser: Plugin;
@@ -1,28 +0,0 @@
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.yamlParser = void 0;
7
- const errors_js_1 = require("../util/errors.js");
8
- const js_yaml_1 = __importDefault(require("js-yaml"));
9
- const js_yaml_2 = require("js-yaml");
10
- exports.yamlParser = {
11
- // JSON is valid YAML
12
- canHandle: (file) => [".yaml", ".yml", ".json"].includes(file.extension),
13
- handler: async (file) => {
14
- const data = Buffer.isBuffer(file.data) ? file.data.toString() : file.data;
15
- if (typeof data !== "string") {
16
- // data is already a JavaScript value (object, array, number, null, NaN, etc.)
17
- return data;
18
- }
19
- try {
20
- const yamlSchema = js_yaml_1.default.load(data, { schema: js_yaml_2.JSON_SCHEMA });
21
- return yamlSchema;
22
- }
23
- catch (error) {
24
- throw new errors_js_1.ParserError(error?.message || "Parser Error", file.url);
25
- }
26
- },
27
- name: 'yaml',
28
- };
@@ -1,88 +0,0 @@
1
- import type { ParserOptions } from "./options.js";
2
- import $Ref from "./ref.js";
3
- import type { JSONSchema } from "./types";
4
- /**
5
- * This class represents a single JSON pointer and its resolved value.
6
- *
7
- * @param $ref
8
- * @param path
9
- * @param [friendlyPath] - The original user-specified path (used for error messages)
10
- * @class
11
- */
12
- declare class Pointer<S extends object = JSONSchema> {
13
- /**
14
- * The {@link $Ref} object that contains this {@link Pointer} object.
15
- */
16
- $ref: $Ref<S>;
17
- /**
18
- * The file path or URL, containing the JSON pointer in the hash.
19
- * This path is relative to the path of the main JSON schema file.
20
- */
21
- path: string;
22
- /**
23
- * The original path or URL, used for error messages.
24
- */
25
- originalPath: string;
26
- /**
27
- * The value of the JSON pointer.
28
- * Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
29
- */
30
- value: any;
31
- /**
32
- * Indicates whether the pointer references itself.
33
- */
34
- circular: boolean;
35
- /**
36
- * The number of indirect references that were traversed to resolve the value.
37
- * Resolving a single pointer may require resolving multiple $Refs.
38
- */
39
- indirections: number;
40
- constructor($ref: $Ref<S>, path: string, friendlyPath?: string);
41
- /**
42
- * Resolves the value of a nested property within the given object.
43
- *
44
- * @param obj - The object that will be crawled
45
- * @param options
46
- * @param pathFromRoot - the path of place that initiated resolving
47
- *
48
- * @returns
49
- * Returns a JSON pointer whose {@link Pointer#value} is the resolved value.
50
- * If resolving this value required resolving other JSON references, then
51
- * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
52
- * of the resolved value.
53
- */
54
- resolve(obj: S, options?: ParserOptions, pathFromRoot?: string): this;
55
- /**
56
- * Sets the value of a nested property within the given object.
57
- *
58
- * @param obj - The object that will be crawled
59
- * @param value - the value to assign
60
- * @param options
61
- *
62
- * @returns
63
- * Returns the modified object, or an entirely new object if the entire object is overwritten.
64
- */
65
- set(obj: S, value: any, options?: ParserOptions): any;
66
- /**
67
- * Parses a JSON pointer (or a path containing a JSON pointer in the hash)
68
- * and returns an array of the pointer's tokens.
69
- * (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
70
- *
71
- * The pointer is parsed according to RFC 6901
72
- * {@link https://tools.ietf.org/html/rfc6901#section-3}
73
- *
74
- * @param path
75
- * @param [originalPath]
76
- * @returns
77
- */
78
- static parse(path: string, originalPath?: string): string[];
79
- /**
80
- * Creates a JSON pointer path, by joining one or more tokens to a base path.
81
- *
82
- * @param base - The base path (e.g. "schema.json#/definitions/person")
83
- * @param tokens - The token(s) to append (e.g. ["name", "first"])
84
- * @returns
85
- */
86
- static join(base: string, tokens: string | string[]): string;
87
- }
88
- export default Pointer;
@@ -1,297 +0,0 @@
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 () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- const ref_js_1 = __importDefault(require("./ref.js"));
40
- const url = __importStar(require("./util/url.js"));
41
- const errors_js_1 = require("./util/errors.js");
42
- const slashes = /\//g;
43
- const tildes = /~/g;
44
- const escapedSlash = /~1/g;
45
- const escapedTilde = /~0/g;
46
- const safeDecodeURIComponent = (encodedURIComponent) => {
47
- try {
48
- return decodeURIComponent(encodedURIComponent);
49
- }
50
- catch {
51
- return encodedURIComponent;
52
- }
53
- };
54
- /**
55
- * This class represents a single JSON pointer and its resolved value.
56
- *
57
- * @param $ref
58
- * @param path
59
- * @param [friendlyPath] - The original user-specified path (used for error messages)
60
- * @class
61
- */
62
- class Pointer {
63
- constructor($ref, path, friendlyPath) {
64
- this.$ref = $ref;
65
- this.path = path;
66
- this.originalPath = friendlyPath || path;
67
- this.value = undefined;
68
- this.circular = false;
69
- this.indirections = 0;
70
- }
71
- /**
72
- * Resolves the value of a nested property within the given object.
73
- *
74
- * @param obj - The object that will be crawled
75
- * @param options
76
- * @param pathFromRoot - the path of place that initiated resolving
77
- *
78
- * @returns
79
- * Returns a JSON pointer whose {@link Pointer#value} is the resolved value.
80
- * If resolving this value required resolving other JSON references, then
81
- * the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
82
- * of the resolved value.
83
- */
84
- resolve(obj, options, pathFromRoot) {
85
- const tokens = Pointer.parse(this.path, this.originalPath);
86
- // Crawl the object, one token at a time
87
- this.value = unwrapOrThrow(obj);
88
- const errors = [];
89
- for (let i = 0; i < tokens.length; i++) {
90
- if (resolveIf$Ref(this, options, pathFromRoot)) {
91
- // The $ref path has changed, so append the remaining tokens to the path
92
- this.path = Pointer.join(this.path, tokens.slice(i));
93
- }
94
- if (typeof this.value === "object" && this.value !== null && !isRootPath(pathFromRoot) && "$ref" in this.value) {
95
- return this;
96
- }
97
- const token = tokens[i];
98
- if (this.value[token] === undefined || (this.value[token] === null && i === tokens.length - 1)) {
99
- // 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
100
- let didFindSubstringSlashMatch = false;
101
- for (let j = tokens.length - 1; j > i; j--) {
102
- const joinedToken = tokens.slice(i, j + 1).join("/");
103
- if (this.value[joinedToken] !== undefined) {
104
- this.value = this.value[joinedToken];
105
- i = j;
106
- didFindSubstringSlashMatch = true;
107
- break;
108
- }
109
- }
110
- if (didFindSubstringSlashMatch) {
111
- continue;
112
- }
113
- this.value = null;
114
- errors.push(new errors_js_1.MissingPointerError(token, decodeURI(this.originalPath)));
115
- }
116
- else {
117
- this.value = this.value[token];
118
- }
119
- }
120
- if (errors.length > 0) {
121
- throw errors.length === 1 ? errors[0] : new AggregateError(errors, "Multiple missing pointer errors");
122
- }
123
- // Resolve the final value
124
- if (!this.value || (this.value.$ref && url.resolve(this.path, this.value.$ref) !== pathFromRoot)) {
125
- resolveIf$Ref(this, options, pathFromRoot);
126
- }
127
- return this;
128
- }
129
- /**
130
- * Sets the value of a nested property within the given object.
131
- *
132
- * @param obj - The object that will be crawled
133
- * @param value - the value to assign
134
- * @param options
135
- *
136
- * @returns
137
- * Returns the modified object, or an entirely new object if the entire object is overwritten.
138
- */
139
- set(obj, value, options) {
140
- const tokens = Pointer.parse(this.path);
141
- let token;
142
- if (tokens.length === 0) {
143
- // There are no tokens, replace the entire object with the new value
144
- this.value = value;
145
- return value;
146
- }
147
- // Crawl the object, one token at a time
148
- this.value = unwrapOrThrow(obj);
149
- for (let i = 0; i < tokens.length - 1; i++) {
150
- resolveIf$Ref(this, options);
151
- token = tokens[i];
152
- if (this.value && this.value[token] !== undefined) {
153
- // The token exists
154
- this.value = this.value[token];
155
- }
156
- else {
157
- // The token doesn't exist, so create it
158
- this.value = setValue(this, token, {});
159
- }
160
- }
161
- // Set the value of the final token
162
- resolveIf$Ref(this, options);
163
- token = tokens[tokens.length - 1];
164
- setValue(this, token, value);
165
- // Return the updated object
166
- return obj;
167
- }
168
- /**
169
- * Parses a JSON pointer (or a path containing a JSON pointer in the hash)
170
- * and returns an array of the pointer's tokens.
171
- * (e.g. "schema.json#/definitions/person/name" => ["definitions", "person", "name"])
172
- *
173
- * The pointer is parsed according to RFC 6901
174
- * {@link https://tools.ietf.org/html/rfc6901#section-3}
175
- *
176
- * @param path
177
- * @param [originalPath]
178
- * @returns
179
- */
180
- static parse(path, originalPath) {
181
- // Get the JSON pointer from the path's hash
182
- const pointer = url.getHash(path).substring(1);
183
- // If there's no pointer, then there are no tokens,
184
- // so return an empty array
185
- if (!pointer) {
186
- return [];
187
- }
188
- // Split into an array
189
- const split = pointer.split("/");
190
- // Decode each part, according to RFC 6901
191
- for (let i = 0; i < split.length; i++) {
192
- split[i] = safeDecodeURIComponent(split[i].replace(escapedSlash, "/").replace(escapedTilde, "~"));
193
- }
194
- if (split[0] !== "") {
195
- throw new errors_js_1.InvalidPointerError(pointer, originalPath === undefined ? path : originalPath);
196
- }
197
- return split.slice(1);
198
- }
199
- /**
200
- * Creates a JSON pointer path, by joining one or more tokens to a base path.
201
- *
202
- * @param base - The base path (e.g. "schema.json#/definitions/person")
203
- * @param tokens - The token(s) to append (e.g. ["name", "first"])
204
- * @returns
205
- */
206
- static join(base, tokens) {
207
- // Ensure that the base path contains a hash
208
- if (base.indexOf("#") === -1) {
209
- base += "#";
210
- }
211
- // Append each token to the base path
212
- tokens = Array.isArray(tokens) ? tokens : [tokens];
213
- for (let i = 0; i < tokens.length; i++) {
214
- const token = tokens[i];
215
- // Encode the token, according to RFC 6901
216
- base += "/" + encodeURIComponent(token.replace(tildes, "~0").replace(slashes, "~1"));
217
- }
218
- return base;
219
- }
220
- }
221
- /**
222
- * If the given pointer's {@link Pointer#value} is a JSON reference,
223
- * then the reference is resolved and {@link Pointer#value} is replaced with the resolved value.
224
- * In addition, {@link Pointer#path} and {@link Pointer#$ref} are updated to reflect the
225
- * resolution path of the new value.
226
- *
227
- * @param pointer
228
- * @param options
229
- * @param [pathFromRoot] - the path of place that initiated resolving
230
- * @returns - Returns `true` if the resolution path changed
231
- */
232
- function resolveIf$Ref(pointer, options, pathFromRoot) {
233
- // Is the value a JSON reference? (and allowed?)
234
- if (ref_js_1.default.isAllowed$Ref(pointer.value)) {
235
- const $refPath = url.resolve(pointer.path, pointer.value.$ref);
236
- if ($refPath === pointer.path && !isRootPath(pathFromRoot)) {
237
- // The value is a reference to itself, so there's nothing to do.
238
- pointer.circular = true;
239
- }
240
- else {
241
- const resolved = pointer.$ref.$refs._resolve($refPath, pointer.path, options);
242
- if (resolved === null) {
243
- return false;
244
- }
245
- pointer.indirections += resolved.indirections + 1;
246
- if (ref_js_1.default.isExtended$Ref(pointer.value)) {
247
- // This JSON reference "extends" the resolved value, rather than simply pointing to it.
248
- // So the resolved path does NOT change. Just the value does.
249
- pointer.value = ref_js_1.default.dereference(pointer.value, resolved.value);
250
- return false;
251
- }
252
- else {
253
- // Resolve the reference
254
- pointer.$ref = resolved.$ref;
255
- pointer.path = resolved.path;
256
- pointer.value = resolved.value;
257
- }
258
- return true;
259
- }
260
- }
261
- return undefined;
262
- }
263
- exports.default = Pointer;
264
- /**
265
- * Sets the specified token value of the {@link Pointer#value}.
266
- *
267
- * The token is evaluated according to RFC 6901.
268
- * {@link https://tools.ietf.org/html/rfc6901#section-4}
269
- *
270
- * @param pointer - The JSON Pointer whose value will be modified
271
- * @param token - A JSON Pointer token that indicates how to modify `obj`
272
- * @param value - The value to assign
273
- * @returns - Returns the assigned value
274
- */
275
- function setValue(pointer, token, value) {
276
- if (pointer.value && typeof pointer.value === "object") {
277
- if (token === "-" && Array.isArray(pointer.value)) {
278
- pointer.value.push(value);
279
- }
280
- else {
281
- pointer.value[token] = value;
282
- }
283
- }
284
- else {
285
- throw new errors_js_1.JSONParserError(`Error assigning $ref pointer "${pointer.path}". \nCannot set "${token}" of a non-object.`);
286
- }
287
- return value;
288
- }
289
- function unwrapOrThrow(value) {
290
- if ((0, errors_js_1.isHandledError)(value)) {
291
- throw value;
292
- }
293
- return value;
294
- }
295
- function isRootPath(pathFromRoot) {
296
- return typeof pathFromRoot == "string" && Pointer.parse(pathFromRoot).length == 0;
297
- }