TomFrost / Jexl

Javascript Expression Language: Powerful context-based expression parser and evaluator
MIT License
563 stars 92 forks source link

Errors for bad identifiers #117

Open abierbaum opened 2 years ago

abierbaum commented 2 years ago

Is there any way to get jexl to throw if the expression being evaluated uses an identifier that isn't found in the context? It seems that missing identifiers are just used as undefined in the expression. I would like to provide users feedback that they made a mistake in the expression input.

const context = {
  age: 36
}

const result = jexl.evalSync('age_wrong * (3 - 1)', context)

For example above will come back as NaN instead of throwing an evaluation exception. I don't see a way to detect when the expression simply has a bad variable. Am I missing any options?

chetbox commented 2 years ago

This would be really helpful for us too. At the moment we extract identifiers from the AST then validate identifiers against the context or a "context validator".

It looks something like this.


function getIdentifiers(ast: any): string[][] {
  if (!ast) {
    return [];
  }
  switch (ast.type) {
    case 'UnaryExpression':
      return getIdentifiers(ast.right);
    case 'BinaryExpression':
      return [...getIdentifiers(ast.left), ...getIdentifiers(ast.right)];
    case 'ConditionalExpression':
      return [
        ...getIdentifiers(ast.test),
        ...getIdentifiers(ast.consequent),
        ...getIdentifiers(ast.alternate)
      ];
    case 'FilterExpression':
      if (!ast.relative && ast.expr.type === 'Literal') {
        return [[...getIdentifiers(ast.subject)[0], ast.expr.value]];
      } else {
        return [...getIdentifiers(ast.subject), ...getIdentifiers(ast.expr)];
      }
    case 'ArrayLiteral':
      return ast.value.flatMap(value => getIdentifiers(value));
    case 'ObjectLiteral':
      return [...getIdentifiers(ast.left), ...getIdentifiers(ast.right)];
    case 'Transform':
      return [
        ...getIdentifiers(ast.subject),
        ...(ast.args?.flatMap(value => getIdentifiers(value)) ?? [])
      ];
    case 'Identifier':
      return [[...(ast.from ? getIdentifiers(ast.from)[0] : []), ast.value]];
  }
  return [];
}

function validateIdentifierInContext(
    context: object | unknown[] | string | undefined,
    keys: string[],
    progress: string[] = []
  ) {
    if (keys.length === 0) {
      return;
    }
    if (obj === undefined) {
      return;
    }

    if (typeof obj === 'string' || Array.isArray(obj)) {
      if (typeof keys[0] === 'number') {
        return;
      } else {
        throw error(`Cannot index into ${progress.join('.')} with ${JSON.stringify(keys[0])}`);
      }
    }

    if (keys[0] in obj) {
      return validateIdentifierInContext(obj[keys[0]], keys.slice(1), [...progress, keys[0]]);
    } else {
      const errorDescription =
        progress.length === 0
          ? 'not a valid identifier'
          : `not a valid key of "${progress.join('.')}"`;
      const helpText = `Valid keys: ${Object.keys(obj)
        .map(key => `"${key}"`)
        .join(', ')}`;
      throw error(`"${keys[0]}" is ${errorDescription}\n${helpText}`);
    }
  }

getIdentifiers(compiledExpression._getAst()).forEach(identifier => validateIdentifierInContext(context, identifier))