-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from KnpLabs/feature/implements-default-validator
feat: implements default validator
- Loading branch information
Showing
5 changed files
with
118 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Knp\JsonSchema\Collection; | ||
use Knp\JsonSchema\JsonSchemaInterface; | ||
use Knp\JsonSchemaBundle\RequestHandler; | ||
use Knp\JsonSchemaBundle\Validator\SwaggestValidator; | ||
|
||
return function(ContainerConfigurator $configurator) { | ||
$services = $configurator->services() | ||
->defaults() | ||
->autowire() | ||
->autoconfigure() | ||
; | ||
|
||
$services->instanceof(JsonSchemaInterface::class) | ||
->tag('knp.json_schema') | ||
; | ||
|
||
$services->set(SwaggestValidator::class); | ||
$services->alias('Knp\JsonSchema\Validator', SwaggestValidator::class); | ||
|
||
$services->set(Collection::class) | ||
->arg('$schemas', tagged_iterator('knp.json_schema')) | ||
; | ||
|
||
$services->set(RequestHandler::class); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Knp\JsonSchemaBundle\Validator; | ||
|
||
use Knp\JsonSchema\JsonSchemaInterface; | ||
use Knp\JsonSchema\Validator; | ||
use Knp\JsonSchema\Validator\Errors; | ||
use Knp\JsonSchema\Validator\Error as KnpJsonSchemaError; | ||
use Swaggest\JsonSchema\Exception\Error; | ||
use Swaggest\JsonSchema\Exception\LogicException; | ||
use Swaggest\JsonSchema\InvalidValue; | ||
use Swaggest\JsonSchema\Schema; | ||
|
||
class SwaggestValidator implements Validator | ||
{ | ||
public function validate(array $data, JsonSchemaInterface $schema): ?Errors | ||
{ | ||
$schema = Schema::import( | ||
json_decode( | ||
json_encode( | ||
$schema, | ||
flags: JSON_THROW_ON_ERROR, | ||
), | ||
flags: JSON_THROW_ON_ERROR, | ||
) | ||
); | ||
|
||
/** | ||
* @var mixed | ||
*/ | ||
$data = json_decode( | ||
json_encode( | ||
$data, | ||
flags: JSON_THROW_ON_ERROR, | ||
), | ||
flags: JSON_THROW_ON_ERROR | ||
); | ||
|
||
try { | ||
$schema->in($data); | ||
|
||
return null; | ||
} catch (InvalidValue $invalidValue) { | ||
return new Errors( | ||
...$this->yieldErrors($invalidValue) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return iterable<KnpJsonSchemaError> | ||
*/ | ||
private function yieldErrors(InvalidValue $invalidValue): iterable | ||
{ | ||
/** | ||
* @var Error | ||
*/ | ||
$inspectionError = $invalidValue->inspect(); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
$errorMessage = $invalidValue->error; | ||
|
||
yield new KnpJsonSchemaError( | ||
$inspectionError->dataPointer, | ||
$errorMessage, | ||
); | ||
|
||
if ($invalidValue instanceof LogicException) { | ||
foreach ($invalidValue->subErrors as $subError) { | ||
yield from $this->yieldErrors($subError); | ||
} | ||
} | ||
} | ||
} |