Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add is_valid function to Expression constraint #50438

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
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.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.4
---

* Add `is_valid` function to the `Expression` constraint, its behavior is the same as `ValidatorInterface::validate`
* Allow single integer for the `versions` option of the `Uuid` constraint
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
* Deprecate Doctrine annotations support in favor of native attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
Expand All @@ -20,13 +22,16 @@
* @author Fabien Potencier <[email protected]>
* @author Bernhard Schussek <[email protected]>
*/
class ExpressionValidator extends ConstraintValidator
class ExpressionValidator extends ConstraintValidator implements ExpressionFunctionProviderInterface
{
private ?ExpressionLanguage $expressionLanguage;
private ExpressionLanguage $expressionLanguage;

public function __construct(ExpressionLanguage $expressionLanguage = null)
{
$this->expressionLanguage = $expressionLanguage;
if ($expressionLanguage) {
$this->expressionLanguage = clone $expressionLanguage;
$this->expressionLanguage->registerProvider($this);
}
}

/**
Expand All @@ -41,6 +46,7 @@ public function validate(mixed $value, Constraint $constraint)
$variables = $constraint->values;
$variables['value'] = $value;
$variables['this'] = $this->context->getObject();
$variables['context'] = $this->context;

if ($constraint->negate xor $this->getExpressionLanguage()->evaluate($constraint->expression, $variables)) {
$this->context->buildViolation($constraint->message)
Expand All @@ -50,8 +56,27 @@ public function validate(mixed $value, Constraint $constraint)
}
}

public function getFunctions(): array
{
return [
new ExpressionFunction('is_valid', function (...$arguments) {
return sprintf(
'0 === $context->getValidator()->inContext($context)->validate(%s)->getViolations()->count()',
implode(', ', $arguments)
);
}, function (array $variables, ...$arguments): bool {
return 0 === $variables['context']->getValidator()->inContext($variables['context'])->validate(...$arguments)->getViolations()->count();
}),
];
}

private function getExpressionLanguage(): ExpressionLanguage
{
return $this->expressionLanguage ??= new ExpressionLanguage();
if (!isset($this->expressionLanguage)) {
$this->expressionLanguage = new ExpressionLanguage();
$this->expressionLanguage->registerProvider($this);
}

return $this->expressionLanguage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\ExpressionValidator;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\ToString;
Expand Down Expand Up @@ -304,4 +307,81 @@ public function testViolationOnPass()
->setCode(Expression::EXPRESSION_FAILED_ERROR)
->assertRaised();
}

public function testIsValidExpression()
{
$constraints = [new NotNull(), new Range(['min' => 2])];

$constraint = new Expression(
['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]]
);

$object = new Entity();
$object->data = 7;

$this->setObject($object);

$this->expectValidateValue(0, $object->data, $constraints);

$this->validator->validate($object, $constraint);

$this->assertNoViolation();
}

public function testIsValidExpressionInvalid()
{
$constraints = [new Range(['min' => 2, 'max' => 5])];

$constraint = new Expression(
['expression' => 'is_valid(this.data, a)', 'values' => ['a' => $constraints]]
);

$object = new Entity();
$object->data = 7;

$this->setObject($object);

$this->expectFailingValueValidation(
0,
7,
$constraints,
null,
new ConstraintViolation('error_range', '', [], '', '', 7, null, 'range')
);

$this->validator->validate($object, $constraint);

$this->assertCount(2, $this->context->getViolations());
}

/**
* @dataProvider provideCompileIsValid
*/
public function testCompileIsValid(string $expression, array $names, string $expected)
{
$provider = new ExpressionValidator();

$expressionLanguage = new ExpressionLanguage();
$expressionLanguage->registerProvider($provider);

$result = $expressionLanguage->compile($expression, $names);

$this->assertSame($expected, $result);
}

public static function provideCompileIsValid(): array
{
return [
[
'is_valid("foo", constraints)',
['constraints'],
'0 === $context->getValidator()->inContext($context)->validate("foo", $constraints)->getViolations()->count()',
],
[
'is_valid(this.data, constraints, groups)',
['this', 'constraints', 'groups'],
'0 === $context->getValidator()->inContext($context)->validate($this->data, $constraints, $groups)->getViolations()->count()',
],
];
}
}