Skip to content

Commit 5932e71

Browse files
committed
Add encapsulated Collection constraint in Yaml constraint.
Allow to use apply Collection constraint to Yaml parsed data array.
1 parent cb16097 commit 5932e71

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/Symfony/Component/Validator/Constraints/Yaml.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(
3838
public int $flags = 0,
3939
?array $groups = null,
4040
mixed $payload = null,
41+
public ?Collection $collection = null,
4142
) {
4243
if (!class_exists(Parser::class)) {
4344
throw new LogicException('The Yaml component is required to use the Yaml constraint. Try running "composer require symfony/yaml".');

src/Symfony/Component/Validator/Constraints/YamlValidator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function validate(mixed $value, Constraint $constraint): void
4949
});
5050

5151
try {
52-
(new Parser())->parse($value, $constraint->flags);
52+
$data = (new Parser())->parse($value, $constraint->flags);
5353
} catch (ParseException $e) {
5454
$this->context->buildViolation($constraint->message)
5555
->setParameter('{{ error }}', $e->getMessage())
@@ -59,5 +59,12 @@ public function validate(mixed $value, Constraint $constraint): void
5959
} finally {
6060
restore_error_handler();
6161
}
62+
63+
if (isset($data) && null !== $constraint->collection) {
64+
$validator = ($context = $this->context)
65+
->getValidator()->inContext($context);
66+
67+
$validator->validate($data, $constraint->collection);
68+
}
6269
}
6370
}

src/Symfony/Component/Validator/Tests/Constraints/YamlTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\Collection;
16+
use Symfony\Component\Validator\Constraints\NotNull;
1517
use Symfony\Component\Validator\Constraints\Yaml;
1618
use Symfony\Component\Validator\Mapping\ClassMetadata;
1719
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
@@ -38,6 +40,9 @@ public function testAttributes()
3840

3941
[$cConstraint] = $metadata->properties['d']->getConstraints();
4042
self::assertSame(YamlParser::PARSE_CONSTANT | YamlParser::PARSE_CUSTOM_TAGS, $cConstraint->flags);
43+
44+
[$eConstraint] = $metadata->properties['e']->getConstraints();
45+
self::assertInstanceOf(Collection::class, $eConstraint->collection);
4146
}
4247
}
4348

@@ -54,4 +59,11 @@ class YamlDummy
5459

5560
#[Yaml(flags: YamlParser::PARSE_CONSTANT | YamlParser::PARSE_CUSTOM_TAGS)]
5661
private $d;
62+
63+
#[Yaml(collection: new Collection(
64+
fields: [
65+
'foo' => new NotNull(),
66+
],
67+
))]
68+
private $e;
5769
}

src/Symfony/Component/Validator/Tests/Constraints/YamlValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Validator\Tests\Constraints;
1313

14+
use Symfony\Component\Validator\Constraints\Collection;
15+
use Symfony\Component\Validator\Constraints\NotNull;
1416
use Symfony\Component\Validator\Constraints\Yaml;
1517
use Symfony\Component\Validator\Constraints\YamlValidator;
1618
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -71,6 +73,16 @@ public function testInvalidFlags()
7173
->assertRaised();
7274
}
7375

76+
public function testConfigValidData()
77+
{
78+
$constraint = new Yaml(
79+
collection: new Collection(['foo' => new NotNull()]),
80+
);
81+
82+
$this->validator->validate('foo: "bar"', $constraint);
83+
$this->assertNoViolation();
84+
}
85+
7486
public static function getValidValues()
7587
{
7688
return [

0 commit comments

Comments
 (0)