Skip to content

Commit

Permalink
feature #27735 [Validator][DoctrineBridge][FWBundle] Automatic data v…
Browse files Browse the repository at this point in the history
…alidation (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
  • Loading branch information
fabpot committed Mar 31, 2019
2 parents a0b89d9 + 8c9858f commit ffc4adc
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 3 deletions.
39 changes: 39 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -804,6 +804,45 @@ private function addValidationSection(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()
->arrayNode('auto_mapping')
->useAttributeAsKey('namespace')
->normalizeKeys(false)
->beforeNormalization()
->ifArray()
->then(function (array $values): array {
foreach ($values as $k => $v) {
if (isset($v['service'])) {
continue;
}

if (isset($v['namespace'])) {
$values[$k]['services'] = [];
continue;
}

if (!\is_array($v)) {
$values[$v]['services'] = [];
unset($values[$k]);
continue;
}

$tmp = $v;
unset($values[$k]);
$values[$k]['services'] = $tmp;
}

return $values;
})
->end()
->arrayPrototype()
->fixXmlConfig('service')
->children()
->arrayNode('services')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
13 changes: 10 additions & 3 deletions DependencyInjection/FrameworkExtension.php
Expand Up @@ -110,6 +110,7 @@
use Symfony\Component\Translation\Command\XliffLintCommand as BaseXliffLintCommand;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\ObjectInitializerInterface;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\Workflow;
Expand Down Expand Up @@ -284,7 +285,8 @@ public function load(array $configs, ContainerBuilder $container)
$container->removeDefinition('console.command.messenger_setup_transports');
}

$this->registerValidationConfiguration($config['validation'], $container, $loader);
$propertyInfoEnabled = $this->isConfigEnabled($container, $config['property_info']);
$this->registerValidationConfiguration($config['validation'], $container, $loader, $propertyInfoEnabled);
$this->registerEsiConfiguration($config['esi'], $container, $loader);
$this->registerSsiConfiguration($config['ssi'], $container, $loader);
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
Expand All @@ -305,7 +307,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerSerializerConfiguration($config['serializer'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['property_info'])) {
if ($propertyInfoEnabled) {
$this->registerPropertyInfoConfiguration($container, $loader);
}

Expand Down Expand Up @@ -1166,7 +1168,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
}
}

private function registerValidationConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
private function registerValidationConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader, bool $propertyInfoEnabled)
{
if (!$this->validatorConfigEnabled = $this->isConfigEnabled($container, $config)) {
return;
Expand Down Expand Up @@ -1217,6 +1219,11 @@ private function registerValidationConfiguration(array $config, ContainerBuilder
if (!$container->getParameter('kernel.debug')) {
$validatorBuilder->addMethodCall('setMetadataCache', [new Reference('validator.mapping.cache.symfony')]);
}

$container->setParameter('validator.auto_mapping', $config['auto_mapping']);
if (!$propertyInfoEnabled || !$config['auto_mapping'] || !class_exists(PropertyInfoLoader::class)) {
$container->removeDefinition('validator.property_info_loader');
}
}

private function registerValidatorMapping(ContainerBuilder $container, array $config, array &$files)
Expand Down
2 changes: 2 additions & 0 deletions FrameworkBundle.php
Expand Up @@ -53,6 +53,7 @@
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Translation\DependencyInjection\TranslatorPathsPass;
use Symfony\Component\Validator\DependencyInjection\AddAutoMappingConfigurationPass;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;

Expand Down Expand Up @@ -124,6 +125,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new TestServiceContainerRealRefPass(), PassConfig::TYPE_AFTER_REMOVING);
$this->addCompilerPassIfExists($container, AddMimeTypeGuesserPass::class);
$this->addCompilerPassIfExists($container, MessengerPass::class);
$this->addCompilerPassIfExists($container, AddAutoMappingConfigurationPass::class);
$container->addCompilerPass(new RegisterReverseContainerPass(true));
$container->addCompilerPass(new RegisterReverseContainerPass(false), PassConfig::TYPE_AFTER_REMOVING);

Expand Down
8 changes: 8 additions & 0 deletions Resources/config/schema/symfony-1.0.xsd
Expand Up @@ -190,6 +190,7 @@
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="static-method" type="xsd:string" />
<xsd:element name="mapping" type="file_mapping" />
<xsd:element name="auto-mapping" type="auto_mapping" />
</xsd:choice>

<xsd:attribute name="enabled" type="xsd:boolean" />
Expand All @@ -207,6 +208,13 @@
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="auto_mapping">
<xsd:sequence>
<xsd:element name="service" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="namespace" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:simpleType name="email-validation-mode">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="html5" />
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/validator.xml
Expand Up @@ -60,5 +60,12 @@
<argument></argument>
<tag name="validator.constraint_validator" alias="Symfony\Component\Validator\Constraints\EmailValidator" />
</service>

<service id="validator.property_info_loader" class="Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader">
<argument type="service" id="property_info" />
<argument type="service" id="property_info" />

<tag name="validator.auto_mapper" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Expand Up @@ -233,6 +233,7 @@ protected static function getBundleDefaultConfig()
'mapping' => [
'paths' => [],
],
'auto_mapping' => [],
],
'annotations' => [
'cache' => 'php_array',
Expand Down
12 changes: 12 additions & 0 deletions Tests/DependencyInjection/Fixtures/php/validation_auto_mapping.php
@@ -0,0 +1,12 @@
<?php

$container->loadFromExtension('framework', [
'property_info' => ['enabled' => true],
'validation' => [
'auto_mapping' => [
'App\\' => ['foo', 'bar'],
'Symfony\\' => ['a', 'b'],
'Foo\\',
],
],
]);
20 changes: 20 additions & 0 deletions Tests/DependencyInjection/Fixtures/xml/validation_auto_mapping.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:framework="http://symfony.com/schema/dic/symfony">

<framework:config>
<framework:property-info enabled="true" />
<framework:validation>
<framework:auto-mapping namespace="App\">
<framework:service>foo</framework:service>
<framework:service>bar</framework:service>
</framework:auto-mapping>
<framework:auto-mapping namespace="Symfony\">
<framework:service>a</framework:service>
<framework:service>b</framework:service>
</framework:auto-mapping>
<framework:auto-mapping namespace="Foo\" />
</framework:validation>
</framework:config>
</container>
@@ -0,0 +1,7 @@
framework:
property_info: { enabled: true }
validation:
auto_mapping:
'App\': ['foo', 'bar']
'Symfony\': ['a', 'b']
'Foo\': []
19 changes: 19 additions & 0 deletions Tests/DependencyInjection/FrameworkExtensionTest.php
Expand Up @@ -50,6 +50,8 @@
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Workflow;
use Symfony\Contracts\HttpClient\HttpClientInterface;

Expand Down Expand Up @@ -1057,6 +1059,23 @@ public function testValidationMapping()
$this->assertContains('validation.yaml', $calls[4][1][0][2]);
}

public function testValidationAutoMapping()
{
if (!class_exists(PropertyInfoLoader::class)) {
$this->markTestSkipped('Auto-mapping requires symfony/validation 4.2+');
}

$container = $this->createContainerFromFile('validation_auto_mapping');
$parameter = [
'App\\' => ['services' => ['foo', 'bar']],
'Symfony\\' => ['services' => ['a', 'b']],
'Foo\\' => ['services' => []],
];

$this->assertSame($parameter, $container->getParameter('validator.auto_mapping'));
$this->assertTrue($container->hasDefinition('validator.property_info_loader'));
}

public function testFormsCanBeEnabledWithoutCsrfProtection()
{
$container = $this->createContainerFromFile('form_no_csrf');
Expand Down

0 comments on commit ffc4adc

Please sign in to comment.