Skip to content

Commit 91c43c7

Browse files
committed
feature #27735 [Validator][DoctrineBridge][FWBundle] Automatic data validation (dunglas)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Validator][DoctrineBridge][FWBundle] Automatic data validation | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes<!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#11132 This feature automatically adds some validation constraints by inferring existing metadata. To do so, it uses the PropertyInfo component and Doctrine metadata, but it has been designed to be easily extendable. Example: ```php use Doctrine\ORM\Mapping as ORM; /** * @Orm\Entity */ class Dummy { /** * @Orm\Id * @Orm\GeneratedValue(strategy="AUTO") * @Orm\Column(type="integer") */ public $id; /** * @Orm\Column(nullable=true) */ public $columnNullable; /** * @Orm\Column(length=20) */ public $columnLength; /** * @Orm\Column(unique=true) */ public $columnUnique; } $manager = $this->managerRegistry->getManager(); $manager->getRepository(Dummy::class); $firstOne = new Dummy(); $firstOne->columnUnique = 'unique'; $firstOne->columnLength = '0'; $manager->persist($firstOne); $manager->flush(); $dummy = new Dummy(); $dummy->columnNullable = 1; // type mistmatch $dummy->columnLength = '012345678901234567890'; // too long $dummy->columnUnique = 'unique'; // not unique $res = $this->validator->validate($dummy); dump((string) $res); /* Object(App\Entity\Dummy).columnUnique:\n This value is already used. (code 23bd9dbf-6b9b-41cd-a99e-4844bcf3077f)\n Object(App\Entity\Dummy).columnLength:\n This value is too long. It should have 20 characters or less. (code d94b19cc-114f-4f44-9cc4-4138e80a87b9)\n Object(App\Entity\Dummy).id:\n This value should not be null. (code ad32d13f-c3d4-423b-909a-857b961eb720)\n Object(App\Entity\Dummy).columnNullable:\n This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n */ ``` It also works for DTOs: ```php class MyDto { /** @var string */ public $name; } $dto = new MyDto(); $dto->name = 1; // type error dump($validator->validate($dto)); /* Object(MyDto).name:\n This value should be of type string. (code ba785a8c-82cb-4283-967c-3cf342181b40)\n */ ``` Supported constraints currently are: * `@NotNull` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc) * `@Type` (using PropertyInfo type extractor, so supports Doctrine metadata, getters/setters and PHPDoc) * `@UniqueEntity` (using Doctrine's `unique` metadata) * `@Length` (using Doctrine's `length` metadata) Many users don't understand that the Doctrine mapping doesn't validate anything (it's just a hint for the schema generator). It leads to usability and security issues (that are not entirely fixed by this PR!!). Even the ones who add constraints often omit important ones like `@Length`, or `@Type` (important when building web APIs). This PR aims to improve things a bit, and ease the development process in RAD and when prototyping. It provides an upgrade path to use proper validation constraints. I plan to make it opt-in, disabled by default, but enabled in the default Flex recipe. (= off by default when using components, on by default when using the full stack framework) TODO: * [x] Add configuration flags * [x] Move the Doctrine-related DI logic from the extension to DoctrineBundle: doctrine/DoctrineBundle#831 * [x] Commit the tests Commits ------- 2d64e703c2 [Validator][DoctrineBridge][FWBundle] Automatic data validation
2 parents ad8357f + f9cbf96 commit 91c43c7

File tree

3 files changed

+273
-0
lines changed

3 files changed

+273
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
16+
use Symfony\Component\Validator\Constraints as Assert;
17+
18+
/**
19+
* @ORM\Entity
20+
* @UniqueEntity(fields={"alreadyMappedUnique"})
21+
*
22+
* @author Kévin Dunglas <[email protected]>
23+
*/
24+
class DoctrineLoaderEntity
25+
{
26+
/**
27+
* @ORM\Id
28+
* @ORM\Column
29+
*/
30+
public $id;
31+
32+
/**
33+
* @ORM\Column(length=20)
34+
*/
35+
public $maxLength;
36+
37+
/**
38+
* @ORM\Column(length=20)
39+
* @Assert\Length(min=5)
40+
*/
41+
public $mergedMaxLength;
42+
43+
/**
44+
* @ORM\Column(length=20)
45+
* @Assert\Length(min=1, max=10)
46+
*/
47+
public $alreadyMappedMaxLength;
48+
49+
/**
50+
* @ORM\Column(unique=true)
51+
*/
52+
public $unique;
53+
54+
/**
55+
* @ORM\Column(unique=true)
56+
*/
57+
public $alreadyMappedUnique;
58+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Validator;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
16+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoctrineLoaderEntity;
17+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
18+
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
19+
use Symfony\Component\Validator\Constraints\Length;
20+
use Symfony\Component\Validator\Mapping\ClassMetadata;
21+
use Symfony\Component\Validator\Tests\Fixtures\Entity;
22+
use Symfony\Component\Validator\Validation;
23+
use Symfony\Component\Validator\ValidatorBuilder;
24+
25+
/**
26+
* @author Kévin Dunglas <[email protected]>
27+
*/
28+
class DoctrineLoaderTest extends TestCase
29+
{
30+
public function testLoadClassMetadata()
31+
{
32+
if (!method_exists(ValidatorBuilder::class, 'addLoader')) {
33+
$this->markTestSkipped('Auto-mapping requires symfony/validation 4.2+');
34+
}
35+
36+
$validator = Validation::createValidatorBuilder()
37+
->enableAnnotationMapping()
38+
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager()))
39+
->getValidator()
40+
;
41+
42+
$classMetadata = $validator->getMetadataFor(new DoctrineLoaderEntity());
43+
44+
$classConstraints = $classMetadata->getConstraints();
45+
$this->assertCount(2, $classConstraints);
46+
$this->assertInstanceOf(UniqueEntity::class, $classConstraints[0]);
47+
$this->assertInstanceOf(UniqueEntity::class, $classConstraints[1]);
48+
$this->assertSame(['alreadyMappedUnique'], $classConstraints[0]->fields);
49+
$this->assertSame('unique', $classConstraints[1]->fields);
50+
51+
$maxLengthMetadata = $classMetadata->getPropertyMetadata('maxLength');
52+
$this->assertCount(1, $maxLengthMetadata);
53+
$maxLengthConstraints = $maxLengthMetadata[0]->getConstraints();
54+
$this->assertCount(1, $maxLengthConstraints);
55+
$this->assertInstanceOf(Length::class, $maxLengthConstraints[0]);
56+
$this->assertSame(20, $maxLengthConstraints[0]->max);
57+
58+
$mergedMaxLengthMetadata = $classMetadata->getPropertyMetadata('mergedMaxLength');
59+
$this->assertCount(1, $mergedMaxLengthMetadata);
60+
$mergedMaxLengthConstraints = $mergedMaxLengthMetadata[0]->getConstraints();
61+
$this->assertCount(1, $mergedMaxLengthConstraints);
62+
$this->assertInstanceOf(Length::class, $mergedMaxLengthConstraints[0]);
63+
$this->assertSame(20, $mergedMaxLengthConstraints[0]->max);
64+
$this->assertSame(5, $mergedMaxLengthConstraints[0]->min);
65+
66+
$alreadyMappedMaxLengthMetadata = $classMetadata->getPropertyMetadata('alreadyMappedMaxLength');
67+
$this->assertCount(1, $alreadyMappedMaxLengthMetadata);
68+
$alreadyMappedMaxLengthConstraints = $alreadyMappedMaxLengthMetadata[0]->getConstraints();
69+
$this->assertCount(1, $alreadyMappedMaxLengthConstraints);
70+
$this->assertInstanceOf(Length::class, $alreadyMappedMaxLengthConstraints[0]);
71+
$this->assertSame(10, $alreadyMappedMaxLengthConstraints[0]->max);
72+
$this->assertSame(1, $alreadyMappedMaxLengthConstraints[0]->min);
73+
}
74+
75+
/**
76+
* @dataProvider regexpProvider
77+
*/
78+
public function testClassValidator(bool $expected, string $classValidatorRegexp = null)
79+
{
80+
$doctrineLoader = new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), $classValidatorRegexp);
81+
82+
$classMetadata = new ClassMetadata(DoctrineLoaderEntity::class);
83+
$this->assertSame($expected, $doctrineLoader->loadClassMetadata($classMetadata));
84+
}
85+
86+
public function regexpProvider()
87+
{
88+
return [
89+
[true, null],
90+
[true, '{^'.preg_quote(DoctrineLoaderEntity::class).'$|^'.preg_quote(Entity::class).'$}'],
91+
[false, '{^'.preg_quote(Entity::class).'$}'],
92+
];
93+
}
94+
}

Validator/DoctrineLoader.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Validator;
13+
14+
use Doctrine\Common\Persistence\Mapping\MappingException;
15+
use Doctrine\ORM\EntityManagerInterface;
16+
use Doctrine\ORM\Mapping\ClassMetadataInfo;
17+
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
18+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19+
use Symfony\Component\Validator\Constraints\Length;
20+
use Symfony\Component\Validator\Mapping\ClassMetadata;
21+
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
22+
23+
/**
24+
* Guesses and loads the appropriate constraints using Doctrine's metadata.
25+
*
26+
* @author Kévin Dunglas <[email protected]>
27+
*/
28+
final class DoctrineLoader implements LoaderInterface
29+
{
30+
private $entityManager;
31+
private $classValidatorRegexp;
32+
33+
public function __construct(EntityManagerInterface $entityManager, string $classValidatorRegexp = null)
34+
{
35+
$this->entityManager = $entityManager;
36+
$this->classValidatorRegexp = $classValidatorRegexp;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function loadClassMetadata(ClassMetadata $metadata): bool
43+
{
44+
$className = $metadata->getClassName();
45+
if (null !== $this->classValidatorRegexp && !preg_match($this->classValidatorRegexp, $className)) {
46+
return false;
47+
}
48+
49+
try {
50+
$doctrineMetadata = $this->entityManager->getClassMetadata($className);
51+
} catch (MappingException | OrmMappingException $exception) {
52+
return false;
53+
}
54+
55+
if (!$doctrineMetadata instanceof ClassMetadataInfo) {
56+
return false;
57+
}
58+
59+
/* Available keys:
60+
- type
61+
- scale
62+
- length
63+
- unique
64+
- nullable
65+
- precision
66+
*/
67+
$existingUniqueFields = $this->getExistingUniqueFields($metadata);
68+
69+
// Type and nullable aren't handled here, use the PropertyInfo Loader instead.
70+
foreach ($doctrineMetadata->fieldMappings as $mapping) {
71+
if (true === $mapping['unique'] && !isset($existingUniqueFields[$mapping['fieldName']])) {
72+
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
73+
}
74+
75+
if (null === $mapping['length']) {
76+
continue;
77+
}
78+
79+
$constraint = $this->getLengthConstraint($metadata, $mapping['fieldName']);
80+
if (null === $constraint) {
81+
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
82+
} elseif (null === $constraint->max) {
83+
// If a Length constraint exists and no max length has been explicitly defined, set it
84+
$constraint->max = $mapping['length'];
85+
}
86+
}
87+
88+
return true;
89+
}
90+
91+
private function getLengthConstraint(ClassMetadata $metadata, string $fieldName): ?Length
92+
{
93+
foreach ($metadata->getPropertyMetadata($fieldName) as $propertyMetadata) {
94+
foreach ($propertyMetadata->getConstraints() as $constraint) {
95+
if ($constraint instanceof Length) {
96+
return $constraint;
97+
}
98+
}
99+
}
100+
101+
return null;
102+
}
103+
104+
private function getExistingUniqueFields(ClassMetadata $metadata): array
105+
{
106+
$fields = [];
107+
foreach ($metadata->getConstraints() as $constraint) {
108+
if (!$constraint instanceof UniqueEntity) {
109+
continue;
110+
}
111+
112+
if (\is_string($constraint->fields)) {
113+
$fields[$constraint->fields] = true;
114+
} elseif (\is_array($constraint->fields) && 1 === \count($constraint->fields)) {
115+
$fields[$constraint->fields[0]] = true;
116+
}
117+
}
118+
119+
return $fields;
120+
}
121+
}

0 commit comments

Comments
 (0)