Skip to content

Commit

Permalink
Add mode to throw on invalid property access
Browse files Browse the repository at this point in the history
  • Loading branch information
dmose committed Jun 28, 2022
1 parent 3d09092 commit c25502a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Jexl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var Evaluator = require("./evaluator/Evaluator"),
* xpath-like drilldown into native Javascript objects.
* @constructor
*/
function Jexl() {
function Jexl(throwOnMissingProp = false) {
this._customGrammar = null;
this._lexer = null;
this._transforms = {};
this._throwOnMissingProp = throwOnMissingProp;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/evaluator/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ exports.FilterExpression = function(ast) {
exports.Identifier = function(ast) {
if (ast.from) {
return this.eval(ast.from).then(function(context) {
if (
this._throwOnMissingProp &&
!Object.hasOwnProperty(context, ast.value)
) {
throw new Error(
`property does not have an identifier named ${ast.value}`
);
}
if (Array.isArray(context)) context = context[0];
if (context === undefined) return undefined;
return context[ast.value];
Expand Down

0 comments on commit c25502a

Please sign in to comment.