Skip to content

Commit 9572d47

Browse files
xabbuhnicolas-grekas
authored andcommitted
[Validator] use "allowedVariables" to configure the ExpressionLanguageSyntax constraint
1 parent d49f884 commit 9572d47

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Constraints/ExpressionLanguageSyntax.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class ExpressionLanguageSyntax extends Constraint
2929

3030
public $message = 'This value should be a valid expression.';
3131
public $service;
32-
public $validateNames = true;
33-
public $names = [];
32+
public $allowedVariables = null;
3433

3534
/**
3635
* {@inheritdoc}

Constraints/ExpressionLanguageSyntaxValidator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraint;
1717
use Symfony\Component\Validator\ConstraintValidator;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* @author Andrey Sevastianov <mrpkmail@gmail.com>
@@ -39,15 +40,15 @@ public function validate($expression, Constraint $constraint): void
3940
}
4041

4142
if (!\is_string($expression)) {
42-
throw new UnexpectedTypeException($expression, 'string');
43+
throw new UnexpectedValueException($expression, 'string');
4344
}
4445

4546
if (null === $this->expressionLanguage) {
4647
$this->expressionLanguage = new ExpressionLanguage();
4748
}
4849

4950
try {
50-
$this->expressionLanguage->lint($expression, ($constraint->validateNames ? ($constraint->names ?? []) : null));
51+
$this->expressionLanguage->lint($expression, $constraint->allowedVariables);
5152
} catch (SyntaxError $exception) {
5253
$this->context->buildViolation($constraint->message)
5354
->setParameter('{{ syntax_error }}', $this->formatValue($exception->getMessage()))

Tests/Constraints/ExpressionLanguageSyntaxTest.php renamed to Tests/Constraints/ExpressionLanguageSyntaxValidatorTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator;
1919
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
2020

21-
class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase
21+
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
2222
{
2323
/**
2424
* @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
@@ -45,6 +45,7 @@ public function testExpressionValid(): void
4545

4646
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
4747
'message' => 'myMessage',
48+
'allowedVariables' => [],
4849
]));
4950

5051
$this->assertNoViolation();
@@ -58,7 +59,6 @@ public function testExpressionWithoutNames(): void
5859

5960
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
6061
'message' => 'myMessage',
61-
'validateNames' => false,
6262
]));
6363

6464
$this->assertNoViolation();
@@ -73,6 +73,7 @@ public function testExpressionIsNotValid(): void
7373

7474
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
7575
'message' => 'myMessage',
76+
'allowedVariables' => [],
7677
]));
7778

7879
$this->buildViolation('myMessage')

0 commit comments

Comments
 (0)