feat: surface expression validation errors when creating or updating rules

This commit is contained in:
Michael Thomas
2024-03-07 12:23:32 -05:00
parent b572c1dcd3
commit 438f602961
7 changed files with 58 additions and 14 deletions

View File

@@ -31,9 +31,10 @@ class ActionExpressionEvaluator
{
private static array $NAMES = array("transaction");
private ExpressionLanguage $expressionLanguage;
private string $expr;
private bool $isExpression;
private ExpressionLanguage $expressionLanguage;
private ?SyntaxError $validationError;
public function __construct(ExpressionLanguage $expressionLanguage, string $expr)
{
@@ -41,6 +42,7 @@ class ActionExpressionEvaluator
$this->expr = $expr;
$this->isExpression = self::isExpression($expr);
$this->validationError = $this->validate();
}
private static function isExpression(string $expr): bool
@@ -48,17 +50,17 @@ class ActionExpressionEvaluator
return str_starts_with($expr, "=");
}
public function isValid(): bool
private function validate(): ?SyntaxError
{
if (!$this->isExpression) {
return true;
return null;
}
try {
$this->lint(array());
return true;
$this->lint();
return null;
} catch (SyntaxError $e) {
return false;
return $e;
}
}
@@ -67,7 +69,7 @@ class ActionExpressionEvaluator
$this->expressionLanguage->lint($expr, self::$NAMES);
}
public function lint(): void
private function lint(): void
{
if (!$this->isExpression) {
return;
@@ -76,6 +78,16 @@ class ActionExpressionEvaluator
$this->lintExpression(substr($this->expr, 1));
}
public function isValid(): bool
{
return $this->validationError === null;
}
public function getValidationError()
{
return $this->validationError;
}
private function evaluateExpression(string $expr, array $journal): string
{
$result = $this->expressionLanguage->evaluate($expr, [