diff --git a/CHANGELOG.md b/CHANGELOG.md index 50351ba7727..cb140038f96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * Doctrine: Revert #3774 support for binary UUID in search filter (#4134) * Do not override Vary headers already set in the Response * GraphQL: Make sure the order of order filters is preserved if nested resources are used (#4171) +* Validation: properties regex pattern is now correctly anchored (#4176) ## 2.6.3 diff --git a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php index 167152748c8..18077acf25e 100644 --- a/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php +++ b/src/Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaRegexRestriction.php @@ -29,7 +29,7 @@ class PropertySchemaRegexRestriction implements PropertySchemaRestrictionMetadat */ public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array { - return $constraint instanceof Regex && $constraint->getHtmlPattern() ? ['pattern' => $constraint->getHtmlPattern()] : []; + return $constraint instanceof Regex && $constraint->getHtmlPattern() ? ['pattern' => '^(?:'.$constraint->getHtmlPattern().')$'] : []; } /** diff --git a/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php b/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php index f7c59d07666..98b8cb8d88b 100644 --- a/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php +++ b/tests/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php @@ -305,7 +305,7 @@ public function testCreateWithPropertyRegexRestriction(): void $schema = $validationPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy')->getSchema(); $this->assertNotNull($schema); $this->assertArrayHasKey('pattern', $schema); - $this->assertEquals('dummy', $schema['pattern']); + $this->assertEquals('^(?:dummy)$', $schema['pattern']); } public function testCreateWithPropertyFormatRestriction(): void @@ -402,7 +402,7 @@ public function testCreateWithAtLeastOneOfConstraint(): void $this->assertNotNull($schema); $this->assertArrayHasKey('oneOf', $schema); $this->assertSame([ - ['pattern' => '.*#.*'], + ['pattern' => '^(?:.*#.*)$'], ['minLength' => 10], ], $schema['oneOf']); }