Problem/Motivation

Subissue of #2869792: [meta] Add constraints to all config entity types ... let's add validation to UUIDs.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

dawehner created an issue. See original summary.

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new6.95 KB

Status: Needs review » Needs work

The last submitted patch, 2: 2870878-2.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new6.97 KB
new564 bytes

Oops

tstoeckler’s picture

+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UuidConstraint.php
@@ -0,0 +1,25 @@
+  public function validatedBy() {
+    return UuidValidator::class;
+  }

So this is interesting, as we otherwise use \Drupal\Component\Uuid\Uuid::isValid() in core. In general it would be nice to use the upstream validation, but I think if we want to do that we should do so wholesale and deprecate our own version? Or alternatively make our Uuid::isValid() use Symfony's implementation internally?

Status: Needs review » Needs work

The last submitted patch, 4: 2870878-4.patch, failed testing.

dawehner’s picture

So this is interesting, as we otherwise use \Drupal\Component\Uuid\Uuid::isValid() in core. In general it would be nice to use the upstream validation, but I think if we want to do that we should do so wholesale and deprecate our own version? Or alternatively make our Uuid::isValid() use Symfony's implementation internally?

Interesting point, well, I was thinking simply "meh". It wouldn't really be worth to reuse this method, because well, every UUID validation will work pretty much the same. In case this is about performance optimization I believe that we are dealing with validating content anyway, which probably isn't even remotely fast.

dawehner’s picture

I don't care about this. @tstoeckler can you please make a decision?

wim leers’s picture

wim leers’s picture

wim leers’s picture

Status: Needs work » Reviewed & tested by the community
Related issues: +#2876659: Deprecate \Drupal\Component\Uuid\Uuid in favor of \Symfony\Component\Validator\Constraints\UuidValidator

Let's get this issue moving forward again — almost 2 weeks without progress for something tiny.


So, what uses \Drupal\Component\Uuid\Uuid::isValid()? 3 classes:

  1. \Drupal\Tests\field\Kernel\String\UuidItemTest
  2. \Drupal\KernelTests\Core\Entity\EntityFieldDefaultValueTest
  3. \Drupal\Tests\Component\Uuid\UuidTest

IOW: only tests (which coincidentally implies that we aren't validating UUIDs anywhere … GASP!).


+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UuidConstraint.php
@@ -0,0 +1,25 @@
+class UuidConstraint extends Uuid {
...
+  public function validatedBy() {
+    return UuidValidator::class;
+  }

This is reusing \Symfony\Component\Validator\Constraints\Uuid, whose corresponding pre-existing (!) validator is \Symfony\Component\Validator\Constraints\UuidValidator.

Why is this then even subclassing it? Because Drupal uses a annotation-based discovery system.

And so, to add an annotation, we must subclass. And in subclassing, we rename for consistency with Drupal's other validation constraints. And in subclassing, we must override the default validatedBy() implementation.

But, in the end, this is really just reusing the existing validation constraint!


I think it's fine to deprecate \Drupal\Component\Uuid\Uuid in favor of \Symfony\Component\Validator\Constraints\UuidValidator, but doing so is out of scope here. Opened #2876659: Deprecate \Drupal\Component\Uuid\Uuid in favor of \Symfony\Component\Validator\Constraints\UuidValidator for that.

dawehner’s picture

I think it's fine to deprecate \Drupal\Component\Uuid\Uuid in favor of \Symfony\Component\Validator\Constraints\UuidValidator, but doing so is out of scope here. Opened #2876659: Deprecate \Drupal\Component\Uuid\Uuid in favor of \Symfony\Component\Validator\Constraints\UuidValidator for that.

Thank you for the RTBC.

I made a comment on the other issue :)

wim leers’s picture

Issue tags: +API-First Initiative

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 4: 2870878-4.patch, failed testing.

kristiaanvandeneynde’s picture

@Wim in #11: Lame as it may be, that is indeed how core expects constraints to be declared right now :)

See other constraints such as Length or Count. Even though they change the parent's messages, they basically do the same. A solution could be to "discover" symfony constraints along with annotation based discovery, but that is also out of scope for this issue.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new8.55 KB
new2 KB

Fixed the failure ...

wim leers’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed b7eb07e and pushed to 8.4.x. Thanks!

I credited @Wim Leers, @tstoeckler and @kristiaanvandeneynde for reviews as they all considered the impact of adding the constraint and its implementation.

+++ b/core/config/schema/core.data_types.schema.yml
@@ -275,7 +282,7 @@ config_entity:
     uuid:
-      type: string
+      type: uuid

<3

diff --git a/core/tests/Drupal/KernelTests/Core/Validation/ConstraintsTest.php b/core/tests/Drupal/KernelTests/Core/Validation/ConstraintsTest.php
index a018f54..8e17422 100644
--- a/core/tests/Drupal/KernelTests/Core/Validation/ConstraintsTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Validation/ConstraintsTest.php
@@ -40,8 +40,6 @@ public function testUuid() {
     $typed_config->get('uuid')
       ->setValue(\Drupal::service('uuid')->generate() . '-invalid');
     $this->assertCount(1, $typed_config->validate());
-    
   }
-  
 
 }

Fixed whitespace on commit.

  • alexpott committed b7eb07e on 8.4.x
    Issue #2870878 by dawehner, Wim Leers, tstoeckler, kristiaanvandeneynde...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

wim leers’s picture

Issue tags: +Entity validation

quietone credited Arla.

quietone’s picture

Issue tags: +Bug Smash Initiative