-
-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parametervalidator): parameter validation
- Loading branch information
Showing
16 changed files
with
354 additions
and
11 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
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
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
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
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
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
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> |
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
<?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\HttpFoundation\Request; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\ConstraintViolationList; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* Validates parameters using the symfony validator. | ||
* | ||
* @experimental | ||
*/ | ||
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'] instanceof Request) { | ||
return $body; | ||
} | ||
|
||
$operation = $context['request']->attributes->get('_api_operation'); | ||
foreach ($operation->getParameters() ?? [] as $parameter) { | ||
if (!$constraints = $parameter->getConstraint()) { | ||
continue; | ||
} | ||
|
||
$value = $parameter->getExtraProperties()['_api_values'][$parameter->getKey()] ?? null; | ||
$violations = $this->validator->validate($value, $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->getProperty() ?? $parameter->getKey(), | ||
$violation->getInvalidValue(), | ||
$violation->getPlural(), | ||
$violation->getCode(), | ||
$violation->getConstraint(), | ||
$violation->getCause() | ||
)); | ||
} | ||
|
||
throw new ValidationException($constraintViolationList); | ||
} | ||
} | ||
|
||
return $body; | ||
} | ||
} |
Oops, something went wrong.