-
Notifications
You must be signed in to change notification settings - Fork 96
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
Comments
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
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.
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?
The text was updated successfully, but these errors were encountered: