Skip to content

Commit

Permalink
bug #37009 [Validator] use "allowedVariables" to configure the Expres…
Browse files Browse the repository at this point in the history
…sionLanguageSyntax constraint (xabbuh)

This PR was merged into the 5.1 branch.

Discussion
----------

[Validator] use "allowedVariables" to configure the ExpressionLanguageSyntax constraint

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix symfony/symfony-docs#13669
| License       | MIT
| Doc PR        |

Commits
-------

4807dab [Validator] use "allowedVariables" to configure the ExpressionLanguageSyntax constraint
  • Loading branch information
nicolas-grekas committed May 30, 2020
2 parents 3e05f1d + 4807dab commit af444f8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Expand Up @@ -29,8 +29,7 @@ class ExpressionLanguageSyntax extends Constraint

public $message = 'This value should be a valid expression.';
public $service;
public $validateNames = true;
public $names = [];
public $allowedVariables = null;

/**
* {@inheritdoc}
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!\is_string($expression)) {
throw new UnexpectedTypeException($expression, 'string');
throw new UnexpectedValueException($expression, 'string');
}

if (null === $this->expressionLanguage) {
$this->expressionLanguage = new ExpressionLanguage();
}

try {
$this->expressionLanguage->lint($expression, ($constraint->validateNames ? ($constraint->names ?? []) : null));
$this->expressionLanguage->lint($expression, $constraint->allowedVariables);
} catch (SyntaxError $exception) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ syntax_error }}', $this->formatValue($exception->getMessage()))
Expand Down
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

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

$this->validator->validate($this->value, new ExpressionLanguageSyntax([
'message' => 'myMessage',
'allowedVariables' => [],
]));

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

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

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

$this->validator->validate($this->value, new ExpressionLanguageSyntax([
'message' => 'myMessage',
'allowedVariables' => [],
]));

$this->buildViolation('myMessage')
Expand Down

0 comments on commit af444f8

Please sign in to comment.