@swagger-api/apidom-json-pointer-relative 0.75.0 → 0.76.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/es/compile.js CHANGED
@@ -1,22 +1,29 @@
1
1
  import { compile as compileJsonPointer } from '@swagger-api/apidom-json-pointer';
2
- // compile :: RelativeJSONPointer -> String
2
+ import CompilationRelativeJsonPointerError from "./errors/CompilationRelativeJsonPointerError.js"; // compile :: RelativeJSONPointer -> String
3
3
  const compile = relativeJsonPointer => {
4
- let relativePointer = '';
4
+ try {
5
+ let relativePointer = '';
5
6
 
6
- // non-negative-integer
7
- relativePointer += String(relativeJsonPointer.nonNegativeIntegerPrefix);
7
+ // non-negative-integer
8
+ relativePointer += String(relativeJsonPointer.nonNegativeIntegerPrefix);
8
9
 
9
- // index-manipulation
10
- if (typeof relativeJsonPointer.indexManipulation === 'number') {
11
- relativePointer += String(relativeJsonPointer.indexManipulation);
10
+ // index-manipulation
11
+ if (typeof relativeJsonPointer.indexManipulation === 'number') {
12
+ relativePointer += String(relativeJsonPointer.indexManipulation);
13
+ }
14
+ if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
15
+ // <json-pointer>
16
+ relativePointer += compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
17
+ } else if (relativeJsonPointer.hashCharacter) {
18
+ // "#"
19
+ relativePointer += '#';
20
+ }
21
+ return relativePointer;
22
+ } catch (error) {
23
+ throw new CompilationRelativeJsonPointerError('Relative JSON Pointer compilation encountered an error.', {
24
+ relativePointer: relativeJsonPointer,
25
+ cause: error
26
+ });
12
27
  }
13
- if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
14
- // <json-pointer>
15
- relativePointer += compileJsonPointer(relativeJsonPointer.jsonPointerTokens);
16
- } else if (relativeJsonPointer.hashCharacter) {
17
- // "#"
18
- relativePointer += '#';
19
- }
20
- return relativePointer;
21
28
  };
22
29
  export default compile;
@@ -0,0 +1,20 @@
1
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
2
+ import RelativeJsonPointerError from "./RelativeJsonPointerError.js";
3
+ class CompilationRelativeJsonPointerError extends RelativeJsonPointerError {
4
+ constructor(message, structuredOptions) {
5
+ super(message, structuredOptions);
6
+ _defineProperty(this, "nonNegativeIntegerPrefix", void 0);
7
+ _defineProperty(this, "indexManipulation", void 0);
8
+ _defineProperty(this, "jsonPointerTokens", void 0);
9
+ _defineProperty(this, "hashCharacter", void 0);
10
+ if (typeof structuredOptions !== 'undefined') {
11
+ this.nonNegativeIntegerPrefix = structuredOptions.relativePointer.nonNegativeIntegerPrefix;
12
+ this.indexManipulation = structuredOptions.relativePointer.indexManipulation;
13
+ this.hashCharacter = structuredOptions.relativePointer.hashCharacter;
14
+ if (Array.isArray(structuredOptions.relativePointer.jsonPointerTokens)) {
15
+ this.jsonPointerTokens = [...structuredOptions.relativePointer.jsonPointerTokens];
16
+ }
17
+ }
18
+ }
19
+ }
20
+ export default CompilationRelativeJsonPointerError;
@@ -1,12 +1,33 @@
1
- export default class EvaluationRelativeJsonPointerError extends Error {
2
- constructor(message) {
3
- super(message);
4
- this.name = this.constructor.name;
5
- this.message = message;
6
- if (typeof Error.captureStackTrace === 'function') {
7
- Error.captureStackTrace(this, this.constructor);
8
- } else {
9
- this.stack = new Error(message).stack;
1
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
2
+ import { hasElementSourceMap, toValue } from '@swagger-api/apidom-core';
3
+ import RelativeJsonPointerError from "./RelativeJsonPointerError.js";
4
+ class EvaluationRelativeJsonPointerError extends RelativeJsonPointerError {
5
+ constructor(message, structuredOptions) {
6
+ super(message, structuredOptions);
7
+ _defineProperty(this, "relativePointer", void 0);
8
+ _defineProperty(this, "currentElement", void 0);
9
+ _defineProperty(this, "currentElementSourceMap", void 0);
10
+ _defineProperty(this, "rootElement", void 0);
11
+ _defineProperty(this, "rootElementSourceMap", void 0);
12
+ _defineProperty(this, "cursorElement", void 0);
13
+ _defineProperty(this, "cursorElementSourceMap", void 0);
14
+ if (typeof structuredOptions !== 'undefined') {
15
+ this.relativePointer = structuredOptions.relativePointer;
16
+ this.currentElement = structuredOptions.currentElement.element;
17
+ if (hasElementSourceMap(structuredOptions.currentElement)) {
18
+ this.currentElementSourceMap = toValue(structuredOptions.currentElement.getMetaProperty('sourceMap'));
19
+ }
20
+ this.rootElement = structuredOptions.rootElement.element;
21
+ if (hasElementSourceMap(structuredOptions.rootElement)) {
22
+ this.rootElementSourceMap = toValue(structuredOptions.rootElement.getMetaProperty('sourceMap'));
23
+ }
24
+ if (typeof structuredOptions.cursorElement !== 'undefined') {
25
+ this.cursorElement = structuredOptions.cursorElement.element;
26
+ if (hasElementSourceMap(structuredOptions.cursorElement)) {
27
+ this.cursorElementSourceMap = toValue(structuredOptions.cursorElement.getMetaProperty('sourceMap'));
28
+ }
29
+ }
10
30
  }
11
31
  }
12
- }
32
+ }
33
+ export default EvaluationRelativeJsonPointerError;
@@ -1,12 +1,11 @@
1
- export default class InvalidRelativeJsonPointerError extends Error {
2
- constructor(relativePointer) {
3
- super(`Invalid Relative JSON Pointer "${relativePointer}".`);
4
- this.name = this.constructor.name;
5
- this.message = `Invalid Relative JSON Pointer "${relativePointer}".`;
6
- if (typeof Error.captureStackTrace === 'function') {
7
- Error.captureStackTrace(this, this.constructor);
8
- } else {
9
- this.stack = new Error(`Invalid Relative JSON Pointer "${relativePointer}".`).stack;
1
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
2
+ import RelativeJsonPointerError from "./RelativeJsonPointerError.js";
3
+ export default class InvalidRelativeJsonPointerError extends RelativeJsonPointerError {
4
+ constructor(message, structuredOptions) {
5
+ super(message, structuredOptions);
6
+ _defineProperty(this, "relativePointer", void 0);
7
+ if (typeof structuredOptions !== 'undefined') {
8
+ this.relativePointer = structuredOptions.relativePointer;
10
9
  }
11
10
  }
12
11
  }
@@ -0,0 +1,3 @@
1
+ import { ApiDOMStructuredError } from '@swagger-api/apidom-error';
2
+ class RelativeJsonPointerError extends ApiDOMStructuredError {}
3
+ export default RelativeJsonPointerError;
package/es/evaluate.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { visit, BREAK, isElement, isMemberElement, isArrayElement, NumberElement } from '@swagger-api/apidom-core';
2
2
  import { compile as compileJsonPointer, evaluate as evaluateJsonPointer } from '@swagger-api/apidom-json-pointer';
3
3
  import { last } from 'ramda';
4
- import { EvaluationRelativeJsonPointerError } from "./errors/index.js";
5
- import parse from "./parse.js"; // evaluates Relative JSON Pointer against ApiDOM fragment
4
+ import EvaluationRelativeJsonPointerError from "./errors/EvaluationRelativeJsonPointerError.js";
5
+ import parse from "./parse.js";
6
+ // evaluates Relative JSON Pointer against ApiDOM fragment
6
7
  const evaluate = (relativePointer, currentElement, rootElement) => {
7
8
  let ancestorLineage = [];
8
9
  let cursor = currentElement;
@@ -16,12 +17,33 @@ const evaluate = (relativePointer, currentElement, rootElement) => {
16
17
  }
17
18
  });
18
19
  if (ancestorLineage.length === 0) {
19
- throw new EvaluationRelativeJsonPointerError('Current element not found inside the root element');
20
+ throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element not found inside the root element', {
21
+ relativePointer,
22
+ currentElement,
23
+ rootElement,
24
+ cursorElement: cursor
25
+ });
20
26
  }
21
27
  if (last(ancestorLineage) === rootElement) {
22
- throw new EvaluationRelativeJsonPointerError('Current element cannot be the root element');
28
+ throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element cannot be the root element', {
29
+ relativePointer,
30
+ currentElement,
31
+ rootElement,
32
+ cursorElement: cursor
33
+ });
34
+ }
35
+ let relativeJsonPointer;
36
+ try {
37
+ relativeJsonPointer = parse(relativePointer);
38
+ } catch (error) {
39
+ throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed while parsing the pointer.', {
40
+ relativePointer,
41
+ currentElement,
42
+ rootElement,
43
+ cursorElement: cursor,
44
+ cause: error
45
+ });
23
46
  }
24
- const relativeJsonPointer = parse(relativePointer);
25
47
 
26
48
  // non-negative-integer
27
49
  if (relativeJsonPointer.nonNegativeIntegerPrefix > 0) {
@@ -35,7 +57,12 @@ const evaluate = (relativePointer, currentElement, rootElement) => {
35
57
  }
36
58
  }
37
59
  if (typeof cursor === 'undefined') {
38
- throw new EvaluationRelativeJsonPointerError(`Evaluation failed on non-negative-integer prefix of "${relativeJsonPointer.nonNegativeIntegerPrefix}"`);
60
+ throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed on non-negative-integer prefix of "${relativeJsonPointer.nonNegativeIntegerPrefix}"`, {
61
+ relativePointer,
62
+ currentElement,
63
+ rootElement,
64
+ cursorElement: cursor
65
+ });
39
66
  }
40
67
  ancestorLineage = ancestorLineageCopy;
41
68
  }
@@ -44,13 +71,23 @@ const evaluate = (relativePointer, currentElement, rootElement) => {
44
71
  if (typeof relativeJsonPointer.indexManipulation === 'number') {
45
72
  const containedArray = last(ancestorLineage);
46
73
  if (typeof containedArray === 'undefined' || !isArrayElement(containedArray)) {
47
- throw new EvaluationRelativeJsonPointerError(`Evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`);
74
+ throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
75
+ relativePointer,
76
+ currentElement,
77
+ rootElement,
78
+ cursorElement: cursor
79
+ });
48
80
  }
49
81
  const currentCursorIndex = containedArray.content.indexOf(cursor);
50
82
  const newCursorIndex = currentCursorIndex + relativeJsonPointer.indexManipulation;
51
83
  cursor = containedArray.content[newCursorIndex];
52
84
  if (typeof cursor === 'undefined') {
53
- throw new EvaluationRelativeJsonPointerError(`Evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`);
85
+ throw new EvaluationRelativeJsonPointerError(`Relative JSON Pointer evaluation failed on index-manipulation "${relativeJsonPointer.indexManipulation}"`, {
86
+ relativePointer,
87
+ currentElement,
88
+ rootElement,
89
+ cursorElement: cursor
90
+ });
54
91
  }
55
92
  }
56
93
  if (Array.isArray(relativeJsonPointer.jsonPointerTokens)) {
@@ -60,7 +97,12 @@ const evaluate = (relativePointer, currentElement, rootElement) => {
60
97
  } else if (relativeJsonPointer.hashCharacter) {
61
98
  // "#"
62
99
  if (cursor === rootElement) {
63
- throw new EvaluationRelativeJsonPointerError('Current element cannot be the root element to apply "#"');
100
+ throw new EvaluationRelativeJsonPointerError('Relative JSON Pointer evaluation failed. Current element cannot be the root element to apply "#"', {
101
+ relativePointer,
102
+ currentElement,
103
+ rootElement,
104
+ cursorElement: cursor
105
+ });
64
106
  }
65
107
  const parentElement = last(ancestorLineage);
66
108
  if (typeof parentElement !== 'undefined') {
package/es/index.js CHANGED
@@ -1,4 +1,7 @@
1
+ export { default as RelativeJsonPointerError } from "./errors/RelativeJsonPointerError.js";
2
+ export { default as InvalidRelativeJsonPointerError } from "./errors/InvalidRelativeJsonPointerError.js";
3
+ export { default as EvaluationRelativeJsonPointerError } from "./errors/EvaluationRelativeJsonPointerError.js";
4
+ export { default as CompilationRelativeJsonPointerError } from "./errors/CompilationRelativeJsonPointerError.js";
1
5
  export { default as parse, isRelativeJsonPointer } from "./parse.js";
2
6
  export { default as compile } from "./compile.js";
3
- export { default as evaluate } from "./evaluate.js";
4
- export { InvalidRelativeJsonPointerError, EvaluationRelativeJsonPointerError } from "./errors/index.js";
7
+ export { default as evaluate } from "./evaluate.js";
package/es/parse.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { parse as parseJsonPointer } from '@swagger-api/apidom-json-pointer';
2
- import { InvalidRelativeJsonPointerError } from "./errors/index.js";
2
+ import InvalidRelativeJsonPointerError from "./errors/InvalidRelativeJsonPointerError.js";
3
3
  const nonNegativeIntegerPrefixRegExp = '(?<nonNegativeIntegerPrefix>[1-9]\\d*|0)';
4
4
  const indexManipulationRegExp = '(?<indexManipulation>[+-][1-9]\\d*|0)';
5
5
  const hashCharacterRegExp = '(?<hashCharacter>#)';
@@ -13,22 +13,30 @@ export const isRelativeJsonPointer = value => {
13
13
  const parse = relativePointer => {
14
14
  const match = relativePointer.match(relativeJsonPointerRegExp);
15
15
  if (match === null || typeof match.groups === 'undefined') {
16
- throw new InvalidRelativeJsonPointerError(relativePointer);
16
+ throw new InvalidRelativeJsonPointerError(`Invalid Relative JSON Pointer "${relativePointer}".`, {
17
+ relativePointer
18
+ });
19
+ }
20
+ try {
21
+ // non-negative-integer
22
+ const nonNegativeIntegerPrefix = parseInt(match.groups.nonNegativeIntegerPrefix, 10);
23
+ // index-manipulation
24
+ const indexManipulation = typeof match.groups.indexManipulation === 'string' ? parseInt(match.groups.indexManipulation, 10) : undefined;
25
+ // <json-pointer>
26
+ const jsonPointerTokens = typeof match.groups.jsonPointer === 'string' ? parseJsonPointer(match.groups.jsonPointer) : undefined;
27
+ // "#"
28
+ const hashCharacter = typeof match.groups.hashCharacter === 'string';
29
+ return {
30
+ nonNegativeIntegerPrefix,
31
+ indexManipulation,
32
+ jsonPointerTokens,
33
+ hashCharacter
34
+ };
35
+ } catch (error) {
36
+ throw new InvalidRelativeJsonPointerError(`Relative JSON Pointer parsing of "${relativePointer}" encountered an error.`, {
37
+ relativePointer,
38
+ cause: error
39
+ });
17
40
  }
18
-
19
- // non-negative-integer
20
- const nonNegativeIntegerPrefix = parseInt(match.groups.nonNegativeIntegerPrefix, 10);
21
- // index-manipulation
22
- const indexManipulation = typeof match.groups.indexManipulation === 'string' ? parseInt(match.groups.indexManipulation, 10) : undefined;
23
- // <json-pointer>
24
- const jsonPointerTokens = typeof match.groups.jsonPointer === 'string' ? parseJsonPointer(match.groups.jsonPointer) : undefined;
25
- // "#"
26
- const hashCharacter = typeof match.groups.hashCharacter === 'string';
27
- return {
28
- nonNegativeIntegerPrefix,
29
- indexManipulation,
30
- jsonPointerTokens,
31
- hashCharacter
32
- };
33
41
  };
34
42
  export default parse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-json-pointer-relative",
3
- "version": "0.75.0",
3
+ "version": "0.76.0",
4
4
  "description": "Evaluate Relative JSON Pointer expressions against ApiDOM.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -38,8 +38,9 @@
38
38
  "license": "Apache-2.0",
39
39
  "dependencies": {
40
40
  "@babel/runtime-corejs3": "^7.20.7",
41
- "@swagger-api/apidom-core": "^0.75.0",
42
- "@swagger-api/apidom-json-pointer": "^0.75.0",
41
+ "@swagger-api/apidom-core": "^0.76.0",
42
+ "@swagger-api/apidom-error": "^0.76.0",
43
+ "@swagger-api/apidom-json-pointer": "^0.76.0",
43
44
  "@types/ramda": "~0.29.3",
44
45
  "ramda": "~0.29.0",
45
46
  "ramda-adjunct": "^4.0.0"
@@ -54,5 +55,5 @@
54
55
  "README.md",
55
56
  "CHANGELOG.md"
56
57
  ],
57
- "gitHead": "1be099d8de39f3fb703cbb5e3f59076e1110fd9b"
58
+ "gitHead": "ce497d1083df34162b9cf2047d6cbd8843af3314"
58
59
  }
package/types/dist.d.ts CHANGED
@@ -1,5 +1,34 @@
1
+ import { ApiDOMStructuredError, ApiDOMErrorOptions } from '@swagger-api/apidom-error';
1
2
  import { Element } from '@swagger-api/apidom-core';
2
3
 
4
+ declare class RelativeJsonPointerError extends ApiDOMStructuredError {
5
+ }
6
+
7
+ interface InvalidRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
8
+ readonly relativePointer: string;
9
+ }
10
+ declare class InvalidRelativeJsonPointerError extends RelativeJsonPointerError {
11
+ readonly relativePointer: string;
12
+ constructor(message?: string, structuredOptions?: InvalidRelativeJsonPointerErrorOptions);
13
+ }
14
+
15
+ interface EvaluationRelativeJsonPointerErrorOptions<T extends Element, U extends Element, V extends Element> extends ApiDOMErrorOptions {
16
+ readonly relativePointer: string;
17
+ readonly currentElement: T;
18
+ readonly rootElement: U;
19
+ readonly cursorElement?: V;
20
+ }
21
+ declare class EvaluationRelativeJsonPointerError<T extends Element, U extends Element, V extends Element> extends RelativeJsonPointerError {
22
+ readonly relativePointer: string;
23
+ readonly currentElement: string;
24
+ readonly currentElementSourceMap?: [[number, number, number], [number, number, number]];
25
+ readonly rootElement: string;
26
+ readonly rootElementSourceMap?: [[number, number, number], [number, number, number]];
27
+ readonly cursorElement?: string;
28
+ readonly cursorElementSourceMap?: [[number, number, number], [number, number, number]];
29
+ constructor(message?: string, structuredOptions?: EvaluationRelativeJsonPointerErrorOptions<T, U, V>);
30
+ }
31
+
3
32
  type RelativeJsonPointer = {
4
33
  readonly nonNegativeIntegerPrefix: number;
5
34
  readonly indexManipulation?: number;
@@ -7,6 +36,17 @@ type RelativeJsonPointer = {
7
36
  readonly hashCharacter?: boolean;
8
37
  };
9
38
 
39
+ interface CompilationRelativeJsonPointerErrorOptions extends ApiDOMErrorOptions {
40
+ readonly relativePointer: RelativeJsonPointer;
41
+ }
42
+ declare class CompilationRelativeJsonPointerError extends RelativeJsonPointerError {
43
+ readonly nonNegativeIntegerPrefix: number;
44
+ readonly indexManipulation?: number;
45
+ readonly jsonPointerTokens?: string[];
46
+ readonly hashCharacter?: boolean;
47
+ constructor(message?: string, structuredOptions?: CompilationRelativeJsonPointerErrorOptions);
48
+ }
49
+
10
50
  declare const isRelativeJsonPointer: (value: any) => boolean;
11
51
  declare const parse: (relativePointer: string) => RelativeJsonPointer;
12
52
 
@@ -14,11 +54,4 @@ declare const compile: (relativeJsonPointer: RelativeJsonPointer) => string;
14
54
 
15
55
  declare const evaluate: <T extends Element, U extends Element>(relativePointer: string, currentElement: T, rootElement: U) => Element;
16
56
 
17
- declare class EvaluationRelativeJsonPointerError extends Error {
18
- }
19
-
20
- declare class InvalidRelativeJsonPointerError extends Error {
21
- constructor(relativePointer: string);
22
- }
23
-
24
- export { EvaluationRelativeJsonPointerError, InvalidRelativeJsonPointerError, compile, evaluate, isRelativeJsonPointer, parse };
57
+ export { CompilationRelativeJsonPointerError, CompilationRelativeJsonPointerErrorOptions, EvaluationRelativeJsonPointerError, EvaluationRelativeJsonPointerErrorOptions, InvalidRelativeJsonPointerError, InvalidRelativeJsonPointerErrorOptions, RelativeJsonPointer, RelativeJsonPointerError, compile, evaluate, isRelativeJsonPointer, parse };
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
4
- exports.__esModule = true;
5
- exports.InvalidRelativeJsonPointerError = exports.EvaluationRelativeJsonPointerError = void 0;
6
- var _EvaluationRelativeJsonPointerError = _interopRequireDefault(require("./EvaluationRelativeJsonPointerError.cjs"));
7
- exports.EvaluationRelativeJsonPointerError = _EvaluationRelativeJsonPointerError.default;
8
- var _InvalidRelativeJsonPointerError = _interopRequireDefault(require("./InvalidRelativeJsonPointerError.cjs"));
9
- exports.InvalidRelativeJsonPointerError = _InvalidRelativeJsonPointerError.default;
@@ -1,2 +0,0 @@
1
- export { default as EvaluationRelativeJsonPointerError } from "./EvaluationRelativeJsonPointerError.js";
2
- export { default as InvalidRelativeJsonPointerError } from "./InvalidRelativeJsonPointerError.js";