Skip to content

Commit

Permalink
feat: implements default validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Lelaisant committed Oct 26, 2022
1 parent e62f880 commit 8c8799c
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 26 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A Symfony bundle for knplabs/php-json-schema",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"Knp\\JsonSchemaBundle\\": "src/"
Expand All @@ -21,7 +21,8 @@
"zircote/swagger-php": ">=4.4",
"symfony/http-kernel": ">=6.0",
"symfony/dependency-injection": ">=6.0",
"symfony/config": ">=6.0"
"symfony/config": ">=6.0",
"swaggest/json-schema": ">=0.12.41"
},
"require-dev": {
"vimeo/psalm": "5.x-dev"
Expand Down
29 changes: 29 additions & 0 deletions config/services.php
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);
};
21 changes: 0 additions & 21 deletions config/services.xml

This file was deleted.

11 changes: 8 additions & 3 deletions src/DependencyInjection/JsonSchemaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class JsonSchemaExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader(
$loader = new PhpFileLoader(
$container,
new FileLocator(__DIR__ . '/../../config')
);

$loader->load('services.xml');
$loader->load('services.php');

$container
->registerForAutoconfiguration(JsonSchemaInterface::class)
->addTag('knp.json_schema')
;
}

public function getAlias(): string
{
return 'knp_json_schema';
}
}
78 changes: 78 additions & 0 deletions src/Validator/SwaggestValidator.php
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);
}
}
}
}

0 comments on commit 8c8799c

Please sign in to comment.