Skip to content

Commit 19cb462

Browse files
committed
- Fix: Ensure throwing with a bad result type
- Enhancement: Allow path as array in non-object signature - Testing; Improve coverage
1 parent 108f9aa commit 19cb462

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## 3.0.0 (2020-01-13)
44

55
- Fix: wrap: false returning inconsistent data types (@CacheControl)
6+
- Fix: Ensure throwing with a bad result type
7+
- Enhancement: Allow path as array in non-object signature
68
- Linting (ESLint): As per latest ash-nazg
79
- Linting (ESLint): Remove redundant "use strict" with switch to ESM
810
- Maintenance: 2 sp. for package.json

src/jsonpath.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ const supportsNodeVM = function () {
1010
return false;
1111
}
1212
};
13-
const allowedResultTypes = [
14-
'value', 'path', 'pointer', 'parent', 'parentProperty', 'all'
15-
];
13+
1614
const {hasOwnProperty: hasOwnProp} = Object.prototype;
1715

1816
/**
@@ -274,7 +272,7 @@ JSONPath.prototype.evaluate = function (
274272

275273
json = json || this.json;
276274
expr = expr || this.path;
277-
if (expr && typeof expr === 'object') {
275+
if (expr && typeof expr === 'object' && !Array.isArray(expr)) {
278276
if (!expr.path) {
279277
throw new TypeError(
280278
'You must supply a "path" property when providing an object ' +
@@ -315,7 +313,7 @@ JSONPath.prototype.evaluate = function (
315313
if (Array.isArray(expr)) {
316314
expr = JSONPath.toPathString(expr);
317315
}
318-
if (!expr || !json || !allowedResultTypes.includes(this.currResultType)) {
316+
if (!expr || !json) {
319317
return undefined;
320318
}
321319
this._obj = json;

test/test.api.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ describe('JSONPath - API', function () {
4343
result = jsonpath({json, path: 'store.book[*].author'});
4444
assert.deepEqual(expected, result);
4545
});
46+
47+
it('should test array path of constructor', () => {
48+
const books = json.store.book;
49+
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
50+
let result = jsonpath({path: ['$', 'store', 'book', '*', 'author'], json});
51+
assert.deepEqual(expected, result);
52+
result = jsonpath({json, path: 'store.book[*].author'});
53+
assert.deepEqual(expected, result);
54+
});
4655
});

test/test.errors.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,48 @@ describe('JSONPath - Errors', function () {
1818
jsonpath({json: {book: []}, path: '$..[?(@.category === category)]'});
1919
}).to.throw(Error, 'jsonPath: category is not defined: _$_v.category === category');
2020
});
21+
22+
it('should throw with a bad result type', () => {
23+
expect(() => {
24+
jsonpath({
25+
json: {children: [5]},
26+
path: '$..children',
27+
resultType: 'badType'
28+
});
29+
}).to.throw(TypeError, 'Unknown result type');
30+
});
31+
32+
it('should throw with `preventEval` and [?()] filtering expression', () => {
33+
expect(() => {
34+
const json = {
35+
datafield: [
36+
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
37+
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
38+
{"@tag": "045", "045": "secret"}
39+
]
40+
};
41+
jsonpath({
42+
json,
43+
path: "$.datafield[?(@.tag=='035')]",
44+
preventEval: true
45+
});
46+
}).to.throw(Error, 'Eval [?(expr)] prevented in JSONPath expression.');
47+
});
48+
49+
it('should throw with `preventEval` and [?()] filtering expression', () => {
50+
expect(() => {
51+
const json = {
52+
datafield: [
53+
{"tag": "035", "subfield": {"@code": "a", "#text": "1879"}},
54+
{"@tag": "042", "subfield": {"@code": "a", "#text": "5555"}},
55+
{"@tag": "045", "045": "secret"}
56+
]
57+
};
58+
jsonpath({
59+
json,
60+
path: '$..datafield[(@.length-1)]',
61+
preventEval: true
62+
});
63+
}).to.throw(Error, 'Eval [(expr)] prevented in JSONPath expression.');
64+
});
2165
});

0 commit comments

Comments
 (0)