Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors for bad identifiers #117

Open
abierbaum opened this issue Feb 14, 2022 · 1 comment
Open

Errors for bad identifiers #117

abierbaum opened this issue Feb 14, 2022 · 1 comment

Comments

@abierbaum
Copy link

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
Copy link

chetbox commented Mar 1, 2022

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants