diff --git a/composer.json b/composer.json index 7401906..81c7e48 100644 --- a/composer.json +++ b/composer.json @@ -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/" @@ -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" diff --git a/config/services.php b/config/services.php new file mode 100644 index 0000000..103f462 --- /dev/null +++ b/config/services.php @@ -0,0 +1,29 @@ +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); +}; \ No newline at end of file diff --git a/config/services.xml b/config/services.xml deleted file mode 100644 index 601c734..0000000 --- a/config/services.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/DependencyInjection/JsonSchemaExtension.php b/src/DependencyInjection/JsonSchemaExtension.php index f55dedc..081ef11 100644 --- a/src/DependencyInjection/JsonSchemaExtension.php +++ b/src/DependencyInjection/JsonSchemaExtension.php @@ -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'; + } } diff --git a/src/Validator/SwaggestValidator.php b/src/Validator/SwaggestValidator.php new file mode 100644 index 0000000..12cbe50 --- /dev/null +++ b/src/Validator/SwaggestValidator.php @@ -0,0 +1,78 @@ +in($data); + + return null; + } catch (InvalidValue $invalidValue) { + return new Errors( + ...$this->yieldErrors($invalidValue) + ); + } + } + + /** + * @return iterable + */ + 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); + } + } + } +}