Skip to content

Commit

Permalink
- Change: Throw TypeError instead of Error for missing `otherType…
Browse files Browse the repository at this point in the history
…Callback` 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`
  • Loading branch information
brettz9 committed Nov 23, 2019
1 parent b155046 commit 355b3f4
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 29 deletions.
27 changes: 19 additions & 8 deletions dist/index-es.js
Expand Up @@ -374,11 +374,11 @@ function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
callback = obj;
obj = expr;
expr = opts;
opts = {};
opts = null;
}

var optObj = opts && _typeof(opts) === 'object';
opts = opts || {};
var objArgs = hasOwnProp.call(opts, 'json') && hasOwnProp.call(opts, 'path');
this.json = opts.json || obj;
this.path = opts.path || expr;
this.resultType = opts.resultType && opts.resultType.toLowerCase() || 'value';
Expand All @@ -391,14 +391,21 @@ function JSONPath(opts, expr, obj, callback, otherTypeCallback) {
this.callback = opts.callback || callback || null;

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

if (opts.autostart !== false) {
var ret = this.evaluate({
path: objArgs ? opts.path : expr,
json: objArgs ? opts.json : obj
});
var args = {
path: optObj ? opts.path : expr
};

if (!optObj) {
args.json = obj;
} else if ('json' in opts) {
args.json = opts.json;
}

var ret = this.evaluate(args);

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

if (expr && _typeof(expr) === 'object') {
if (!expr.path) {
throw new Error('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
throw new TypeError('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
}

if (!('json' in expr)) {
throw new TypeError('You must supply a "json" property when providing an object ' + 'argument to JSONPath.evaluate().');
}

json = hasOwnProp.call(expr, 'json') ? expr.json : json;
Expand Down
2 changes: 1 addition & 1 deletion dist/index-es.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-es.min.js.map

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions dist/index-umd.js
Expand Up @@ -380,11 +380,11 @@
callback = obj;
obj = expr;
expr = opts;
opts = {};
opts = null;
}

var optObj = opts && _typeof(opts) === 'object';
opts = opts || {};
var objArgs = hasOwnProp.call(opts, 'json') && hasOwnProp.call(opts, 'path');
this.json = opts.json || obj;
this.path = opts.path || expr;
this.resultType = opts.resultType && opts.resultType.toLowerCase() || 'value';
Expand All @@ -397,14 +397,21 @@
this.callback = opts.callback || callback || null;

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

if (opts.autostart !== false) {
var ret = this.evaluate({
path: objArgs ? opts.path : expr,
json: objArgs ? opts.json : obj
});
var args = {
path: optObj ? opts.path : expr
};

if (!optObj) {
args.json = obj;
} else if ('json' in opts) {
args.json = opts.json;
}

var ret = this.evaluate(args);

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

if (expr && _typeof(expr) === 'object') {
if (!expr.path) {
throw new Error('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
throw new TypeError('You must supply a "path" property when providing an object ' + 'argument to JSONPath.evaluate().');
}

if (!('json' in expr)) {
throw new TypeError('You must supply a "json" property when providing an object ' + 'argument to JSONPath.evaluate().');
}

json = hasOwnProp.call(expr, 'json') ? expr.json : json;
Expand Down
2 changes: 1 addition & 1 deletion dist/index-umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-umd.min.js.map

Large diffs are not rendered by default.

28 changes: 19 additions & 9 deletions src/jsonpath.js
Expand Up @@ -216,11 +216,10 @@ function JSONPath (opts, expr, obj, callback, otherTypeCallback) {
callback = obj;
obj = expr;
expr = opts;
opts = {};
opts = null;
}
const optObj = opts && typeof opts === 'object';
opts = opts || {};
const objArgs = hasOwnProp.call(opts, 'json') &&
hasOwnProp.call(opts, 'path');
this.json = opts.json || obj;
this.path = opts.path || expr;
this.resultType = (opts.resultType && opts.resultType.toLowerCase()) ||
Expand All @@ -235,17 +234,22 @@ function JSONPath (opts, expr, obj, callback, otherTypeCallback) {
this.otherTypeCallback = opts.otherTypeCallback ||
otherTypeCallback ||
function () {
throw new Error(
throw new TypeError(
'You must supply an otherTypeCallback callback option ' +
'with the @other() operator.'
);
};

if (opts.autostart !== false) {
const ret = this.evaluate({
path: (objArgs ? opts.path : expr),
json: (objArgs ? opts.json : obj)
});
const args = {
path: (optObj ? opts.path : expr)
};
if (!optObj) {
args.json = obj;
} else if ('json' in opts) {
args.json = opts.json;
}
const ret = this.evaluate(args);
if (!ret || typeof ret !== 'object') {
throw new NewError(ret);
}
Expand All @@ -272,11 +276,17 @@ JSONPath.prototype.evaluate = function (
expr = expr || this.path;
if (expr && typeof expr === 'object') {
if (!expr.path) {
throw new Error(
throw new TypeError(
'You must supply a "path" property when providing an object ' +
'argument to JSONPath.evaluate().'
);
}
if (!('json' in expr)) {
throw new TypeError(
'You must supply a "json" property when providing an object ' +
'argument to JSONPath.evaluate().'
);
}
json = hasOwnProp.call(expr, 'json') ? expr.json : json;
flatten = hasOwnProp.call(expr, 'flatten') ? expr.flatten : flatten;
this.currResultType = hasOwnProp.call(expr, 'resultType')
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Expand Up @@ -32,6 +32,7 @@ <h1>JSONPath Tests</h1>
<script src="test.at_and_dollar.js"></script>
<script src="test.callback.js"></script>
<script src="test.custom-properties.js"></script>
<script src="test.errors.js"></script>
<script src="test.escaping.js"></script>
<script src="test.eval.js"></script>
<script src="test.examples.js"></script>
Expand Down
16 changes: 16 additions & 0 deletions test/test.errors.js
@@ -0,0 +1,16 @@
'use strict';

describe('JSONPath - Errors', function () {
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().');
});
});

0 comments on commit 355b3f4

Please sign in to comment.