[Validator] use "allowedVariables" to configure the ExpressionLanguageSyntax constraint
This commit is contained in:
parent
2ff26b7ce8
commit
4807dab305
@ -29,8 +29,7 @@ class ExpressionLanguageSyntax extends Constraint
|
|||||||
|
|
||||||
public $message = 'This value should be a valid expression.';
|
public $message = 'This value should be a valid expression.';
|
||||||
public $service;
|
public $service;
|
||||||
public $validateNames = true;
|
public $allowedVariables = null;
|
||||||
public $names = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
|
@ -16,6 +16,7 @@ use Symfony\Component\ExpressionLanguage\SyntaxError;
|
|||||||
use Symfony\Component\Validator\Constraint;
|
use Symfony\Component\Validator\Constraint;
|
||||||
use Symfony\Component\Validator\ConstraintValidator;
|
use Symfony\Component\Validator\ConstraintValidator;
|
||||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||||
|
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrey Sevastianov <mrpkmail@gmail.com>
|
* @author Andrey Sevastianov <mrpkmail@gmail.com>
|
||||||
@ -39,7 +40,7 @@ class ExpressionLanguageSyntaxValidator extends ConstraintValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!\is_string($expression)) {
|
if (!\is_string($expression)) {
|
||||||
throw new UnexpectedTypeException($expression, 'string');
|
throw new UnexpectedValueException($expression, 'string');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null === $this->expressionLanguage) {
|
if (null === $this->expressionLanguage) {
|
||||||
@ -47,7 +48,7 @@ class ExpressionLanguageSyntaxValidator extends ConstraintValidator
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->expressionLanguage->lint($expression, ($constraint->validateNames ? ($constraint->names ?? []) : null));
|
$this->expressionLanguage->lint($expression, $constraint->allowedVariables);
|
||||||
} catch (SyntaxError $exception) {
|
} catch (SyntaxError $exception) {
|
||||||
$this->context->buildViolation($constraint->message)
|
$this->context->buildViolation($constraint->message)
|
||||||
->setParameter('{{ syntax_error }}', $this->formatValue($exception->getMessage()))
|
->setParameter('{{ syntax_error }}', $this->formatValue($exception->getMessage()))
|
||||||
|
@ -18,7 +18,7 @@ use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax;
|
|||||||
use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator;
|
use Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator;
|
||||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||||
|
|
||||||
class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase
|
class ExpressionLanguageSyntaxValidatorTest extends ConstraintValidatorTestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
|
* @var \PHPUnit\Framework\MockObject\MockObject|ExpressionLanguage
|
||||||
@ -45,6 +45,7 @@ class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
|
'allowedVariables' => [],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
@ -58,7 +59,6 @@ class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
'validateNames' => false,
|
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->assertNoViolation();
|
$this->assertNoViolation();
|
||||||
@ -73,6 +73,7 @@ class ExpressionLanguageSyntaxTest extends ConstraintValidatorTestCase
|
|||||||
|
|
||||||
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
$this->validator->validate($this->value, new ExpressionLanguageSyntax([
|
||||||
'message' => 'myMessage',
|
'message' => 'myMessage',
|
||||||
|
'allowedVariables' => [],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->buildViolation('myMessage')
|
$this->buildViolation('myMessage')
|
Reference in New Issue
Block a user