@swaggerexpert/jsonpath 2.2.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 +25 -4
- package/cjs/index.cjs +3 -1
- package/cjs/parse/index.cjs +1 -2
- package/cjs/parse/translators/CSTOptimizedTranslator.cjs +1 -1
- package/cjs/test/index.cjs +25 -0
- package/es/index.mjs +2 -1
- package/es/parse/index.mjs +1 -2
- package/es/parse/translators/CSTOptimizedTranslator.mjs +1 -1
- package/es/test/index.mjs +20 -0
- package/package.json +1 -1
- package/types/index.d.ts +9 -0
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)
|
|
@@ -152,7 +153,7 @@ interface CSTNode {
|
|
|
152
153
|
###### CST Optimized translator
|
|
153
154
|
|
|
154
155
|
Same as CST, but optimizes the tree for more optimized representation. By default, it collapses
|
|
155
|
-
fragmented `single-quoted` or `
|
|
156
|
+
fragmented `single-quoted`, `double-quoted` or `normal-single-quoted` nodes into a single node.
|
|
156
157
|
|
|
157
158
|
```js
|
|
158
159
|
import { parse, CSTOptimizedTranslator } from '@swaggerexpert/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,
|
|
@@ -360,7 +381,7 @@ literal = number / string-literal /
|
|
|
360
381
|
comparable = singular-query / ; singular query value
|
|
361
382
|
function-expr / ; ValueType
|
|
362
383
|
literal
|
|
363
|
-
; MODIFICATION: https://www.rfc-editor.org/errata/
|
|
384
|
+
; MODIFICATION: https://www.rfc-editor.org/errata/eid8352
|
|
364
385
|
comparison-op = "==" / "!=" /
|
|
365
386
|
"<=" / ">=" /
|
|
366
387
|
"<" / ">"
|
|
@@ -424,9 +445,9 @@ descendant-segment = double-dot-prefix (bracketed-selection / ; MODIFICATION: s
|
|
|
424
445
|
|
|
425
446
|
; https://www.rfc-editor.org/rfc/rfc9535#name-normalized-paths
|
|
426
447
|
normalized-path = root-identifier *(normal-index-segment)
|
|
427
|
-
normal-index-segment =
|
|
448
|
+
normal-index-segment = left-bracket normal-selector right-bracket ; MODIFICATION: surrogate text rule used
|
|
428
449
|
normal-selector = normal-name-selector / normal-index-selector
|
|
429
|
-
normal-name-selector =
|
|
450
|
+
normal-name-selector = squote *normal-single-quoted squote ; 'string', MODIFICATION: surrogate text rule used
|
|
430
451
|
normal-single-quoted = normal-unescaped /
|
|
431
452
|
ESC normal-escapable
|
|
432
453
|
normal-unescaped = ; omit %x0-1F control codes
|
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 }; }
|
package/cjs/parse/index.cjs
CHANGED
|
@@ -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');
|
|
@@ -5,7 +5,7 @@ exports.default = void 0;
|
|
|
5
5
|
var _CSTTranslator = _interopRequireDefault(require("./CSTTranslator.cjs"));
|
|
6
6
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
7
|
class CSTOptimizedTranslator extends _CSTTranslator.default {
|
|
8
|
-
collapsibleTypes = ['single-quoted', 'double-quoted'];
|
|
8
|
+
collapsibleTypes = ['single-quoted', 'double-quoted', 'normal-single-quoted'];
|
|
9
9
|
constructor({
|
|
10
10
|
collapsibleTypes
|
|
11
11
|
} = {}) {
|
|
@@ -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";
|
package/es/parse/index.mjs
CHANGED
|
@@ -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');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import CSTTranslator from "./CSTTranslator.mjs";
|
|
2
2
|
class CSTOptimizedTranslator extends CSTTranslator {
|
|
3
|
-
collapsibleTypes = ['single-quoted', 'double-quoted'];
|
|
3
|
+
collapsibleTypes = ['single-quoted', 'double-quoted', 'normal-single-quoted'];
|
|
4
4
|
constructor({
|
|
5
5
|
collapsibleTypes
|
|
6
6
|
} = {}) {
|
|
@@ -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
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
|
*/
|