I have a workflow which gets as input equality/inequality expressions which i want to act based upon. I wish to evaluate the value of the expression inside the ‘if’ node and act accordingly. I have seen that theres a utility function $evaluteExpression - but it does not work when i attempt to evalute the whole expression as a string.
For example:
“if {{ $evaluteExpression(1==0) }}” will result in
“if false”
however, the following expression:
“if {{ $evaluteExpression(“1==0” }}” (notice the quotes) will result in
“if 1==0”
my expression is injected from a input parameter, therefore its necessarily a string.
I understand this is probably possible using the code block, but i would very much like to avoid it. Does anyone else have an idea?
Sorry if my previous explanation misled you, allow me to try to further clarify my original question.
I would like to receive from an input param an expression, such as:
1==0
1 > 10
1 >= 9
then, i would like to have an if statement that its true and false paths are determined based on the result of the expression.
Since i get the expression from a parameter, it is necessary a string. Now, while inserting 1==0 inside the if field will work, when done dynamically (and therefore is a string), the $evaluateExpression function will output the string ‘as-is’ as its result.
a close equivalent of the situation is to hardcodedly put an expression inside the if node, but surround it with quotations.
{{ $evaluateExpression(1==0) }} → false
{{ $evaluteExpression(“1==0” }} → “1==0”
{{ $evaluteExpression($json.expression}} → “1==0” (when the value of that param is “1==0”)
And this is a more comprehensive case that handles multiple operators:
{{ (function() {
const ex = $json.ex;
const operators = ['==', '>=', '<=', '>', '<', '!='];
for (const op of operators) {
if (ex.includes(op)) {
const [left, right] = ex.split(op).map(part => Number(part.trim()));
switch(op) {
case '==': return left == right;
case '>=': return left >= right;
case '<=': return left <= right;
case '>': return left > right;
case '<': return left < right;
case '!=': return left != right;
}
}
}
return ex;
})() }}