-
-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathtest.errors.js
More file actions
109 lines (100 loc) · 4.24 KB
/
Copy pathtest.errors.js
File metadata and controls
109 lines (100 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';
checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Error (${vmType})`, function () {
before(setBuiltInState);
it('should throw with missing `path`', function () {
assert.throws(() => {
jsonpath({json: []});
}, TypeError, 'You must supply a "path" property when providing an object ' +
'argument to JSONPath.evaluate().');
});
it('should throw with missing `json`', function () {
assert.throws(() => {
jsonpath({path: '$'});
}, TypeError, 'You must supply a "json" property when providing an object ' +
'argument to JSONPath.evaluate().');
});
it('should throw with a bad filter', () => {
expect(() => {
jsonpath({json: {book: []}, path: '$..[?(@.category === category)]'});
}).to.throw(Error, 'jsonPath: category is not defined: @.category === category');
});
it('should throw with a bad result type', () => {
expect(() => {
// @ts-ignore -- Deliberately invalid result type.
jsonpath({
json: {children: [5]},
path: '$..children',
// @ts-ignore -- Deliberately invalid result type.
resultType: 'badType'
});
}).to.throw(TypeError, 'Unknown result type');
});
it('should throw with `eval: false` and [?()] filtering expression', () => {
expect(() => {
const json = {
datafield: [
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
{"@tag": "045", "045": "secret"}
]
};
jsonpath({
json,
path: "$.datafield[?(@.tag=='035')]",
eval: false
});
}).to.throw(Error, 'Eval [?(expr)] prevented in JSONPath expression.');
});
it('should throw with `eval: false` and [?()] filtering expression (@.length)', () => {
expect(() => {
const json = {
datafield: [
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
{"@tag": "045", "045": "secret"}
]
};
jsonpath({
json,
path: '$..datafield[(@.length-1)]',
eval: false
});
}).to.throw(Error, 'Eval [(expr)] prevented in JSONPath expression.');
});
it('Syntax error in safe mode script', () => {
expect(() => {
const json = {tag: 10};
jsonpath({
json,
path: '$..[?(this)]',
wrap: false,
eval: 'safe'
});
}).to.throw(Error, 'jsonPath: Unexpected expression: this');
});
it('Invalid assignment in safe mode script', () => {
expect(() => {
const json = {tag: 10};
jsonpath({
json,
path: '$..[?(2 = 8)]',
wrap: false,
eval: 'safe'
});
}).to.throw(Error, 'jsonPath: Invalid left-hand side in assignment: 2 = 8');
});
it('should throw when `new JSONPath` would unwrap a scalar result', () => {
expect(() => {
// @ts-expect-error - Confirm constructor misuse is rejected
// eslint-disable-next-line no-new -- Testing
new /** @type {new (opts: object) => object} */ (
JSONPath
)({json: {a: 1}, path: '$.a', wrap: false});
}).to.throw(
Error,
'JSONPath should not be called with "new" (it prevents return of (unwrapped) scalar values)'
);
});
});
});