Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): parameter validation
- Loading branch information
Showing
6 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,17 +15,19 @@ | |
|
||
use ApiPlatform\OpenApi; | ||
use ApiPlatform\State\ProviderInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @experimental | ||
*/ | ||
abstract class Parameter | ||
{ | ||
/** | ||
* @param array{type?: string}|null $schema | ||
* @param array<string, mixed> $extraProperties | ||
* @param ProviderInterface|callable|string|null $provider | ||
* @param FilterInterface|string|null $filter | ||
* @param array{type?: string}|null $schema | ||
* @param array<string, mixed> $extraProperties | ||
* @param ProviderInterface|callable|string|null $provider | ||
* @param FilterInterface|string|null $filter | ||
* @param Symfony\Component\Validator\Constraint|string|null $constraint | ||
*/ | ||
public function __construct( | ||
Check failure on line 32 in src/Metadata/Parameter.php GitHub Actions / PHPStan (PHP 8.3)
|
||
protected ?string $key = null, | ||
|
@@ -37,6 +39,7 @@ public function __construct( | |
protected ?string $description = null, | ||
protected ?bool $required = null, | ||
protected ?int $priority = null, | ||
protected array|Constraint|null $constraint = null, | ||
Check failure on line 42 in src/Metadata/Parameter.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 42 in src/Metadata/Parameter.php GitHub Actions / PHPStan (PHP 8.3)
|
||
protected ?array $extraProperties = [], | ||
) { | ||
} | ||
|
@@ -89,6 +92,14 @@ public function getPriority(): ?int | |
return $this->priority; | ||
} | ||
|
||
/** | ||
* @return Constraint|Constraint[]|null | ||
*/ | ||
public function getConstraint(): array|Constraint|null | ||
{ | ||
return $this->constraint; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
|
@@ -178,6 +189,14 @@ public function withRequired(bool $required): static | |
return $self; | ||
} | ||
|
||
public function withConstraint(array|Constraint $constraint): static | ||
{ | ||
$self = clone $this; | ||
$self->constraint = $constraint; | ||
|
||
return $self; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $extraProperties | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Symfony/Bundle/Resources/config/symfony/parameter_validator.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="api_platform.symfony.parameter_validator" class="ApiPlatform\Symfony\Validator\State\ParameterValidatorProvider" public="true" decorates="api_platform.state_provider.parameter"> | ||
<argument type="service" id="api_platform.symfony.parameter_validator.inner" /> | ||
<argument type="service" id="validator" /> | ||
</service> | ||
</services> | ||
</container> |
74 changes: 74 additions & 0 deletions
74
src/Symfony/Validator/State/ParameterValidatorProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Symfony\Validator\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Validator\Exception\ValidationException; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\ConstraintViolationList; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class ParameterValidatorProvider implements ProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly ProviderInterface $decorated, | ||
private readonly ValidatorInterface $validator | ||
) { | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$body = $this->decorated->provide($operation, $uriVariables, $context); | ||
if (!$context['request'] ?? null) { | ||
return $body; | ||
} | ||
|
||
$operation = $context['request']->attributes->get('_api_operation'); | ||
foreach ($operation->getParameters() as $parameter) { | ||
if (!$constraints = $parameter->getConstraint()) { | ||
continue; | ||
} | ||
|
||
// This is computed in @see ApiPlatform\State\Provider\ParameterProvider | ||
if (!\array_key_exists('_api_values', $parameter->getExtraProperties())) { | ||
continue; | ||
} | ||
|
||
$violations = $this->validator->validate(current($parameter->getExtraProperties()['_api_values']), $constraints); | ||
|
||
if (0 !== \count($violations)) { | ||
$constraintViolationList = new ConstraintViolationList(); | ||
foreach ($violations as $violation) { | ||
$constraintViolationList->add(new ConstraintViolation( | ||
$violation->getMessage(), | ||
$violation->getMessageTemplate(), | ||
$violation->getParameters(), | ||
$violation->getRoot(), | ||
$parameter->getKey(), | ||
$violation->getInvalidValue(), | ||
$violation->getPlural(), | ||
$violation->getCode(), | ||
$violation->getConstraint(), | ||
$violation->getCause() | ||
)); | ||
} | ||
|
||
throw new ValidationException($constraintViolationList); | ||
} | ||
} | ||
|
||
return $body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters