From 3be51c9cbeab4a8d3e29dee4a1985fb0f8f64f7d Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 17 Oct 2024 10:17:49 +0200 Subject: [PATCH] fix(symfony): ECMA-262 pattern with RegExp validator --- ...rameterValidationResourceMetadataCollectionFactory.php | 2 +- tests/Fixtures/TestBundle/ApiResource/WithParameter.php | 2 +- tests/Fixtures/TestBundle/Filter/PatternFilter.php | 2 +- tests/Functional/Parameters/ValidationTest.php | 8 +++++++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php b/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php index 84dfebe8ea4..1c16ed527e3 100644 --- a/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php +++ b/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php @@ -130,7 +130,7 @@ private function addSchemaValidation(Parameter $parameter, ?array $schema = null } if (isset($schema['pattern'])) { - $assertions[] = new Regex($schema['pattern']); + $assertions[] = new Regex('#'.$schema['pattern'].'#'); } if (isset($schema['maxLength']) || isset($schema['minLength'])) { diff --git a/tests/Fixtures/TestBundle/ApiResource/WithParameter.php b/tests/Fixtures/TestBundle/ApiResource/WithParameter.php index 9b828885a17..be6b288fba9 100644 --- a/tests/Fixtures/TestBundle/ApiResource/WithParameter.php +++ b/tests/Fixtures/TestBundle/ApiResource/WithParameter.php @@ -62,7 +62,7 @@ 'length' => new QueryParameter(schema: ['maxLength' => 1, 'minLength' => 3]), 'array' => new QueryParameter(schema: ['minItems' => 2, 'maxItems' => 3]), 'multipleOf' => new QueryParameter(schema: ['multipleOf' => 2]), - 'pattern' => new QueryParameter(schema: ['pattern' => '/\d/']), + 'pattern' => new QueryParameter(schema: ['pattern' => '\d']), ], provider: [self::class, 'collectionProvider'] )] diff --git a/tests/Fixtures/TestBundle/Filter/PatternFilter.php b/tests/Fixtures/TestBundle/Filter/PatternFilter.php index cd601059b30..6a1940fe8a4 100644 --- a/tests/Fixtures/TestBundle/Filter/PatternFilter.php +++ b/tests/Fixtures/TestBundle/Filter/PatternFilter.php @@ -36,7 +36,7 @@ public function getDescription(string $resourceClass): array 'type' => 'string', 'required' => false, 'schema' => [ - 'pattern' => '/^(pattern|nrettap)$/', + 'pattern' => '^(pattern|nrettap)$', ], ], ]; diff --git a/tests/Functional/Parameters/ValidationTest.php b/tests/Functional/Parameters/ValidationTest.php index 655e145c1f9..7ae22b9a00d 100644 --- a/tests/Functional/Parameters/ValidationTest.php +++ b/tests/Functional/Parameters/ValidationTest.php @@ -113,7 +113,7 @@ public static function provideQueryStrings(): array public function testBlank(): void { - $response = self::createClient()->request('GET', 'validate_parameters?blank=f'); + self::createClient()->request('GET', 'validate_parameters?blank=f'); $this->assertResponseIsSuccessful(); } @@ -146,4 +146,10 @@ public function testValidatePropertyPlaceholder(): void ], ], $response->toArray(false)); } + + public function testValidatePattern(): void + { + self::createClient()->request('GET', 'validate_parameters?pattern=2'); + $this->assertResponseIsSuccessful(); + } }