@swaggerexpert/jsonpath 4.0.0 → 4.0.2

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/SECURITY.md CHANGED
@@ -8,7 +8,8 @@ If you believe you've found an exploitable security issue in @swaggerexpert/json
8
8
  |---------|--------------------|
9
9
  | ^1.0.0 | :x: |
10
10
  | ^2.0.0 | :x: |
11
- | ^3.0.0 | :white_check_mark: |
11
+ | ^3.0.0 | :x: |
12
+ | ^4.0.0 | :white_check_mark: |
12
13
 
13
14
  ## Reporting a Vulnerability
14
15
 
@@ -6,6 +6,7 @@ var _index = _interopRequireDefault(require("../parse/index.cjs"));
6
6
  var NormalizedPath = _interopRequireWildcard(require("../normalized-path.cjs"));
7
7
  var _segment = _interopRequireDefault(require("./visitors/segment.cjs"));
8
8
  var _index2 = _interopRequireDefault(require("./realms/json/index.cjs"));
9
+ var _JSONPathParseError = _interopRequireDefault(require("../errors/JSONPathParseError.cjs"));
9
10
  var _JSONPathEvaluateError = _interopRequireDefault(require("../errors/JSONPathEvaluateError.cjs"));
10
11
  var defaultFunctions = _interopRequireWildcard(require("./functions/index.cjs"));
11
12
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
@@ -29,6 +30,8 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
29
30
  * Default is JSONEvaluationRealm for plain objects/arrays.
30
31
  * @property {Object} [functions] - Optional custom function registry.
31
32
  * Can extend or override built-in functions (length, count, match, search, value).
33
+ * @property {boolean} [trace=true] - Enable parser tracing for detailed error messages.
34
+ * When true, syntax errors include position and expected tokens.
32
35
  */
33
36
 
34
37
  /**
@@ -38,7 +41,8 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
38
41
  * @param {string} expression - JSONPath expression
39
42
  * @param {EvaluateOptions} [options] - Evaluation options
40
43
  * @returns {unknown[]} - Array of matched values
41
- * @throws {JSONPathEvaluateError} If the expression is invalid
44
+ * @throws {JSONPathParseError} If the expression is invalid (syntax or semantic error)
45
+ * @throws {JSONPathEvaluateError} If an unexpected error occurs during evaluation
42
46
  *
43
47
  * @example
44
48
  * // Simple query
@@ -60,21 +64,29 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
60
64
  const evaluate = (value, expression, {
61
65
  callback,
62
66
  realm = new _index2.default(),
63
- functions = defaultFunctions
67
+ functions = defaultFunctions,
68
+ trace = true
64
69
  } = {}) => {
65
- // Parse the expression
66
- const parseResult = (0, _index.default)(expression);
67
- if (!parseResult.result.success) {
68
- throw new _JSONPathEvaluateError.default(`Invalid JSONPath expression: ${expression}`, {
69
- expression
70
+ // Parse the expression with trace enabled for better error messages
71
+ const {
72
+ result,
73
+ trace: parseTrace,
74
+ tree
75
+ } = (0, _index.default)(expression, {
76
+ trace: !!trace
77
+ });
78
+ if (!result.success) {
79
+ let message = `Invalid JSONPath expression: "${expression}". Syntax error at position ${result.maxMatched}`;
80
+ message += parseTrace ? `, expected ${parseTrace.inferExpectations()}` : '';
81
+ throw new _JSONPathParseError.default(message, {
82
+ jsonPath: expression
70
83
  });
71
84
  }
72
85
  try {
73
86
  // The tree is the AST root directly (JsonPathQuery node)
74
- const ast = parseResult.tree;
75
87
  const {
76
88
  segments
77
- } = ast;
89
+ } = tree;
78
90
  const results = [];
79
91
 
80
92
  // Handle empty query ($ with no segments)
@@ -12,6 +12,7 @@ import parse from "../parse/index.mjs";
12
12
  import * as NormalizedPath from "../normalized-path.mjs";
13
13
  import visitSegment from "./visitors/segment.mjs";
14
14
  import JSONEvaluationRealm from "./realms/json/index.mjs";
15
+ import JSONPathParseError from "../errors/JSONPathParseError.mjs";
15
16
  import JSONPathEvaluateError from "../errors/JSONPathEvaluateError.mjs";
16
17
  import * as defaultFunctions from "./functions/index.mjs";
17
18
  /**
@@ -22,6 +23,8 @@ import * as defaultFunctions from "./functions/index.mjs";
22
23
  * Default is JSONEvaluationRealm for plain objects/arrays.
23
24
  * @property {Object} [functions] - Optional custom function registry.
24
25
  * Can extend or override built-in functions (length, count, match, search, value).
26
+ * @property {boolean} [trace=true] - Enable parser tracing for detailed error messages.
27
+ * When true, syntax errors include position and expected tokens.
25
28
  */
26
29
  /**
27
30
  * Evaluate a JSONPath expression against a value.
@@ -30,7 +33,8 @@ import * as defaultFunctions from "./functions/index.mjs";
30
33
  * @param {string} expression - JSONPath expression
31
34
  * @param {EvaluateOptions} [options] - Evaluation options
32
35
  * @returns {unknown[]} - Array of matched values
33
- * @throws {JSONPathEvaluateError} If the expression is invalid
36
+ * @throws {JSONPathParseError} If the expression is invalid (syntax or semantic error)
37
+ * @throws {JSONPathEvaluateError} If an unexpected error occurs during evaluation
34
38
  *
35
39
  * @example
36
40
  * // Simple query
@@ -52,21 +56,29 @@ import * as defaultFunctions from "./functions/index.mjs";
52
56
  const evaluate = (value, expression, {
53
57
  callback,
54
58
  realm = new JSONEvaluationRealm(),
55
- functions = defaultFunctions
59
+ functions = defaultFunctions,
60
+ trace = true
56
61
  } = {}) => {
57
- // Parse the expression
58
- const parseResult = parse(expression);
59
- if (!parseResult.result.success) {
60
- throw new JSONPathEvaluateError(`Invalid JSONPath expression: ${expression}`, {
61
- expression
62
+ // Parse the expression with trace enabled for better error messages
63
+ const {
64
+ result,
65
+ trace: parseTrace,
66
+ tree
67
+ } = parse(expression, {
68
+ trace: !!trace
69
+ });
70
+ if (!result.success) {
71
+ let message = `Invalid JSONPath expression: "${expression}". Syntax error at position ${result.maxMatched}`;
72
+ message += parseTrace ? `, expected ${parseTrace.inferExpectations()}` : '';
73
+ throw new JSONPathParseError(message, {
74
+ jsonPath: expression
62
75
  });
63
76
  }
64
77
  try {
65
78
  // The tree is the AST root directly (JsonPathQuery node)
66
- const ast = parseResult.tree;
67
79
  const {
68
80
  segments
69
- } = ast;
81
+ } = tree;
70
82
  const results = [];
71
83
 
72
84
  // Handle empty query ($ with no segments)
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "registry": "https://registry.npmjs.org",
6
6
  "provenance": true
7
7
  },
8
- "version": "4.0.0",
8
+ "version": "4.0.2",
9
9
  "description": "RFC 9535 implementation of JSONPath",
10
10
  "main": "./cjs/index.cjs",
11
11
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -216,6 +216,11 @@ export interface Stats {
216
216
 
217
217
  export interface Trace {
218
218
  displayTrace(): string;
219
+ inferExpectations(): Expectations;
220
+ }
221
+
222
+ export interface Expectations extends Array<string> {
223
+ toString(): string;
219
224
  }
220
225
 
221
226
  /**
@@ -274,6 +279,12 @@ export interface EvaluateOptions {
274
279
  * Can extend or override built-in functions.
275
280
  */
276
281
  readonly functions?: Record<string, Function>;
282
+ /**
283
+ * Enable parser tracing for detailed error messages.
284
+ * When true, syntax errors include position and expected tokens.
285
+ * @default true
286
+ */
287
+ readonly trace?: boolean;
277
288
  }
278
289
 
279
290
  /**