Skip to content

Commit 355b3f4

Browse files
committed
- Change: Throw TypeError instead of Error for missing otherTypeCallback when using @other
- Change: Throw `TypeError` instead of `Error` for missing `path` - Enhancement: Throw `TypeError` for missing `json` (fixes #110) - Testing: Add test for missing `path` or `json`
1 parent b155046 commit 355b3f4

File tree

9 files changed

+78
-29
lines changed

9 files changed

+78
-29
lines changed

dist/index-es.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,11 @@ function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
374374
callback = obj;
375375
obj = expr;
376376
expr = opts;
377-
opts = {};
377+
opts = null;
378378
}
379379

380+
var optObj = opts && _typeof(opts) === 'object';
380381
opts = opts || {};
381-
var objArgs = hasOwnProp.call(opts, 'json') && hasOwnProp.call(opts, 'path');
382382
this.json = opts.json || obj;
383383
this.path = opts.path || expr;
384384
this.resultType = opts.resultType && opts.resultType.toLowerCase() || 'value';
@@ -391,14 +391,21 @@ function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
391391
this.callback = opts.callback || callback || null;
392392

393393
this.otherTypeCallback = opts.otherTypeCallback || otherTypeCallback || function () {
394-
throw new Error('You must supply an otherTypeCallback callback option ' + 'with the @other() operator.');
394+
throw new TypeError('You must supply an otherTypeCallback callback option ' + 'with the @other() operator.');
395395
};
396396

397397
if (opts.autostart !== false) {
398-
var ret = this.evaluate({
399-
path: objArgs ? opts.path : expr,
400-
json: objArgs ? opts.json : obj
401-
});
398+
var args = {
399+
path: optObj ? opts.path : expr
400+
};
401+
402+
if (!optObj) {
403+
args.json = obj;
404+
} else if ('json' in opts) {
405+
args.json = opts.json;
406+
}
407+
408+
var ret = this.evaluate(args);
402409

403410
if (!ret || _typeof(ret) !== 'object') {
404411
throw new NewError(ret);
@@ -425,7 +432,11 @@ JSONPath.prototype.evaluate = function (expr, json, callback, otherTypeCallback)
425432

426433
if (expr && _typeof(expr) === 'object') {
427434
if (!expr.path) {
428-
throw new Error('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
435+
throw new TypeError('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
436+
}
437+
438+
if (!('json' in expr)) {
439+
throw new TypeError('You must supply a "json" property when providing an object ' + 'argument to JSONPath.evaluate().');
429440
}
430441

431442
json = hasOwnProp.call(expr, 'json') ? expr.json : json;

dist/index-es.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-es.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-umd.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,11 @@
380380
callback = obj;
381381
obj = expr;
382382
expr = opts;
383-
opts = {};
383+
opts = null;
384384
}
385385

386+
var optObj = opts && _typeof(opts) === 'object';
386387
opts = opts || {};
387-
var objArgs = hasOwnProp.call(opts, 'json') && hasOwnProp.call(opts, 'path');
388388
this.json = opts.json || obj;
389389
this.path = opts.path || expr;
390390
this.resultType = opts.resultType && opts.resultType.toLowerCase() || 'value';
@@ -397,14 +397,21 @@
397397
this.callback = opts.callback || callback || null;
398398

399399
this.otherTypeCallback = opts.otherTypeCallback || otherTypeCallback || function () {
400-
throw new Error('You must supply an otherTypeCallback callback option ' + 'with the @other() operator.');
400+
throw new TypeError('You must supply an otherTypeCallback callback option ' + 'with the @other() operator.');
401401
};
402402

403403
if (opts.autostart !== false) {
404-
var ret = this.evaluate({
405-
path: objArgs ? opts.path : expr,
406-
json: objArgs ? opts.json : obj
407-
});
404+
var args = {
405+
path: optObj ? opts.path : expr
406+
};
407+
408+
if (!optObj) {
409+
args.json = obj;
410+
} else if ('json' in opts) {
411+
args.json = opts.json;
412+
}
413+
414+
var ret = this.evaluate(args);
408415

409416
if (!ret || _typeof(ret) !== 'object') {
410417
throw new NewError(ret);
@@ -431,7 +438,11 @@
431438

432439
if (expr && _typeof(expr) === 'object') {
433440
if (!expr.path) {
434-
throw new Error('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
441+
throw new TypeError('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
442+
}
443+
444+
if (!('json' in expr)) {
445+
throw new TypeError('You must supply a "json" property when providing an object ' + 'argument to JSONPath.evaluate().');
435446
}
436447

437448
json = hasOwnProp.call(expr, 'json') ? expr.json : json;

dist/index-umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index-umd.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jsonpath.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,10 @@ function JSONPath (opts, expr, obj, callback, otherTypeCallback) {
216216
callback = obj;
217217
obj = expr;
218218
expr = opts;
219-
opts = {};
219+
opts = null;
220220
}
221+
const optObj = opts && typeof opts === 'object';
221222
opts = opts || {};
222-
const objArgs = hasOwnProp.call(opts, 'json') &&
223-
hasOwnProp.call(opts, 'path');
224223
this.json = opts.json || obj;
225224
this.path = opts.path || expr;
226225
this.resultType = (opts.resultType && opts.resultType.toLowerCase()) ||
@@ -235,17 +234,22 @@ function JSONPath (opts, expr, obj, callback, otherTypeCallback) {
235234
this.otherTypeCallback = opts.otherTypeCallback ||
236235
otherTypeCallback ||
237236
function () {
238-
throw new Error(
237+
throw new TypeError(
239238
'You must supply an otherTypeCallback callback option ' +
240239
'with the @other() operator.'
241240
);
242241
};
243242

244243
if (opts.autostart !== false) {
245-
const ret = this.evaluate({
246-
path: (objArgs ? opts.path : expr),
247-
json: (objArgs ? opts.json : obj)
248-
});
244+
const args = {
245+
path: (optObj ? opts.path : expr)
246+
};
247+
if (!optObj) {
248+
args.json = obj;
249+
} else if ('json' in opts) {
250+
args.json = opts.json;
251+
}
252+
const ret = this.evaluate(args);
249253
if (!ret || typeof ret !== 'object') {
250254
throw new NewError(ret);
251255
}
@@ -272,11 +276,17 @@ JSONPath.prototype.evaluate = function (
272276
expr = expr || this.path;
273277
if (expr && typeof expr === 'object') {
274278
if (!expr.path) {
275-
throw new Error(
279+
throw new TypeError(
276280
'You must supply a "path" property when providing an object ' +
277281
'argument to JSONPath.evaluate().'
278282
);
279283
}
284+
if (!('json' in expr)) {
285+
throw new TypeError(
286+
'You must supply a "json" property when providing an object ' +
287+
'argument to JSONPath.evaluate().'
288+
);
289+
}
280290
json = hasOwnProp.call(expr, 'json') ? expr.json : json;
281291
flatten = hasOwnProp.call(expr, 'flatten') ? expr.flatten : flatten;
282292
this.currResultType = hasOwnProp.call(expr, 'resultType')

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ <h1>JSONPath Tests</h1>
3232
<script src="test.at_and_dollar.js"></script>
3333
<script src="test.callback.js"></script>
3434
<script src="test.custom-properties.js"></script>
35+
<script src="test.errors.js"></script>
3536
<script src="test.escaping.js"></script>
3637
<script src="test.eval.js"></script>
3738
<script src="test.examples.js"></script>

test/test.errors.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
describe('JSONPath - Errors', function () {
4+
it('should throw with missing `path`', function () {
5+
assert.throws(() => {
6+
jsonpath({json: []});
7+
}, TypeError, 'You must supply a "path" property when providing an object ' +
8+
'argument to JSONPath.evaluate().');
9+
});
10+
it('should throw with missing `json`', function () {
11+
assert.throws(() => {
12+
jsonpath({path: '$'});
13+
}, TypeError, 'You must supply a "json" property when providing an object ' +
14+
'argument to JSONPath.evaluate().');
15+
});
16+
});

0 commit comments

Comments
 (0)