10000 [Validator] use "allowedVariables" to configure the ExpressionLanguageSyntax constraint by xabbuh · Pull Request #37009 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 1 commit into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files. 10000
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
5077
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
'message' => 'myMessage',
'allowedVariables' => [],
]));

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