@swaggerexpert/jsonpath 2.3.0 → 2.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
@@ -41,6 +41,7 @@ The development of this library contributed to the identification and formal sub
41
41
  - [XML](#xml-translator)
42
42
  - [Statistics](#statistics)
43
43
  - [Tracing](#tracing)
44
+ - [Validation](#validation)
44
45
  - [Errors](#errors)
45
46
  - [Grammar](#grammar)
46
47
  - [More about JSONPath](#more-about-jsonpath)
@@ -201,6 +202,26 @@ By combining information from `result` and `trace`, it is possible to analyze th
201
202
  and generate a messages like this: `'Syntax error at position 1, expected "[", ".", ".."'`. Please see this
202
203
  [test file](https://github.com/swaggerexpert/jsonpath/blob/main/test/parse/trace.js) for more information how to achieve that.
203
204
 
205
+ #### Validation
206
+
207
+ `@swaggerexpert/jsonpath` provides a `test` function to validate JSONPath expressions.
208
+
209
+ ```js
210
+ import { test } from '@swaggerexpert/jsonpath';
211
+
212
+ test('$.store.book[0].title'); // => true
213
+ test('$$'); // => false
214
+ ```
215
+
216
+ Normalized paths can be validated by setting `normalized` option to `true`.
217
+
218
+ ```js
219
+ import { test } from '@swaggerexpert/jsonpath';
220
+
221
+ test("$['a']", { normalized: true }); // => true
222
+ test('$.store.book[0].title', { normalized: true }); // => false
223
+ ```
224
+
204
225
  #### Errors
205
226
 
206
227
  `@swaggerexpert/jsonpath` provides a structured error class hierarchy,
package/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.parse = exports.XMLTranslator = exports.Grammar = exports.CSTTranslator = exports.CSTOptimizedTranslator = void 0;
4
+ exports.test = exports.parse = exports.XMLTranslator = exports.Grammar = exports.CSTTranslator = exports.CSTOptimizedTranslator = void 0;
5
5
  var _grammar = _interopRequireDefault(require("./grammar.cjs"));
6
6
  exports.Grammar = _grammar.default;
7
7
  var _index = _interopRequireDefault(require("./parse/index.cjs"));
@@ -12,4 +12,6 @@ var _CSTOptimizedTranslator = _interopRequireDefault(require("./parse/translator
12
12
  exports.CSTOptimizedTranslator = _CSTOptimizedTranslator.default;
13
13
  var _XMLTranslator = _interopRequireDefault(require("./parse/translators/XMLTranslator.cjs"));
14
14
  exports.XMLTranslator = _XMLTranslator.default;
15
+ var _index2 = _interopRequireDefault(require("./test/index.cjs"));
16
+ exports.test = _index2.default;
15
17
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -12,8 +12,7 @@ const parse = (jsonPath, {
12
12
  normalized = false,
13
13
  stats = false,
14
14
  trace = false,
15
- translator = new _CSTTranslator.default(),
16
- test = 3
15
+ translator = new _CSTTranslator.default()
17
16
  } = {}) => {
18
17
  if (typeof jsonPath !== 'string') {
19
18
  throw new TypeError('JSONPath must be a string');
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _index = _interopRequireDefault(require("../parse/index.cjs"));
6
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
7
+ const test = (jsonPath, {
8
+ normalized = false
9
+ } = {}) => {
10
+ if (typeof jsonPath !== 'string') return false;
11
+ try {
12
+ const {
13
+ result
14
+ } = (0, _index.default)(jsonPath, {
15
+ normalized,
16
+ stats: false,
17
+ trace: false,
18
+ translator: null
19
+ });
20
+ return result.success;
21
+ } catch {
22
+ return false;
23
+ }
24
+ };
25
+ var _default = exports.default = test;
package/es/index.mjs CHANGED
@@ -2,4 +2,5 @@ export { default as Grammar } from "./grammar.mjs";
2
2
  export { default as parse } from "./parse/index.mjs";
3
3
  export { default as CSTTranslator } from "./parse/translators/CSTTranslator.mjs";
4
4
  export { default as CSTOptimizedTranslator } from "./parse/translators/CSTOptimizedTranslator.mjs";
5
- export { default as XMLTranslator } from "./parse/translators/XMLTranslator.mjs";
5
+ export { default as XMLTranslator } from "./parse/translators/XMLTranslator.mjs";
6
+ export { default as test } from "./test/index.mjs";
@@ -7,8 +7,7 @@ const parse = (jsonPath, {
7
7
  normalized = false,
8
8
  stats = false,
9
9
  trace = false,
10
- translator = new CSTTranslator(),
11
- test = 3
10
+ translator = new CSTTranslator()
12
11
  } = {}) => {
13
12
  if (typeof jsonPath !== 'string') {
14
13
  throw new TypeError('JSONPath must be a string');
@@ -0,0 +1,20 @@
1
+ import parse from "../parse/index.mjs";
2
+ const test = (jsonPath, {
3
+ normalized = false
4
+ } = {}) => {
5
+ if (typeof jsonPath !== 'string') return false;
6
+ try {
7
+ const {
8
+ result
9
+ } = parse(jsonPath, {
10
+ normalized,
11
+ stats: false,
12
+ trace: false,
13
+ translator: null
14
+ });
15
+ return result.success;
16
+ } catch {
17
+ return false;
18
+ }
19
+ };
20
+ export default test;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "2.3.0",
6
+ "version": "2.4.0",
7
7
  "description": "RCF 9535 implementation of JSONPath",
8
8
  "main": "./cjs/index.cjs",
9
9
  "types": "./types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -63,6 +63,15 @@ export interface Trace {
63
63
  displayTrace(): string;
64
64
  }
65
65
 
66
+ /**
67
+ * Validation
68
+ */
69
+ export function test(jsonpath: string, options?: TestOptions): boolean;
70
+
71
+ export interface TestOptions {
72
+ readonly normalized?: boolean;
73
+ }
74
+
66
75
  /**
67
76
  * Grammar
68
77
  */