From 66dfd9df8e9dcb6d590e484d2245d678aed0e095 Mon Sep 17 00:00:00 2001 From: Simon L Date: Fri, 26 May 2023 16:19:06 +0200 Subject: [PATCH] validate json against json schema Signed-off-by: Simon L --- php/composer.json | 3 +- php/composer.lock | 72 +++++++++++++++++++++++++- php/src/ContainerDefinitionFetcher.php | 18 ++++++- 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/php/composer.json b/php/composer.json index 32488056..017d2731 100644 --- a/php/composer.json +++ b/php/composer.json @@ -16,7 +16,8 @@ "http-interop/http-factory-guzzle": "^1.2", "slim/twig-view": "^3.3", "slim/csrf": "^1.3", - "ext-apcu": "*" + "ext-apcu": "*", + "justinrainbow/json-schema": "^5.2" }, "scripts": { "psalm": "psalm --threads=1", diff --git a/php/composer.lock b/php/composer.lock index 1a782775..fbe12ec7 100644 --- a/php/composer.lock +++ b/php/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b0074cfbf6b5cde6d6d2207286ad2e85", + "content-hash": "3cbf9ef41575f504b9bdbc8dbe8562e3", "packages": [ { "name": "guzzlehttp/guzzle", @@ -389,6 +389,76 @@ }, "time": "2021-07-21T13:50:14+00:00" }, + { + "name": "justinrainbow/json-schema", + "version": "5.2.12", + "source": { + "type": "git", + "url": "https://github.com/justinrainbow/json-schema.git", + "reference": "ad87d5a5ca981228e0e205c2bc7dfb8e24559b60" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/justinrainbow/json-schema/zipball/ad87d5a5ca981228e0e205c2bc7dfb8e24559b60", + "reference": "ad87d5a5ca981228e0e205c2bc7dfb8e24559b60", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1", + "json-schema/json-schema-test-suite": "1.2.0", + "phpunit/phpunit": "^4.8.35" + }, + "bin": [ + "bin/validate-json" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "JsonSchema\\": "src/JsonSchema/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bruno Prieto Reis", + "email": "bruno.p.reis@gmail.com" + }, + { + "name": "Justin Rainbow", + "email": "justin.rainbow@gmail.com" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + }, + { + "name": "Robert Schönthal", + "email": "seroscho@googlemail.com" + } + ], + "description": "A library to validate a json schema.", + "homepage": "https://github.com/justinrainbow/json-schema", + "keywords": [ + "json", + "schema" + ], + "support": { + "issues": "https://github.com/justinrainbow/json-schema/issues", + "source": "https://github.com/justinrainbow/json-schema/tree/5.2.12" + }, + "time": "2022-04-13T08:02:27+00:00" + }, { "name": "laravel/serializable-closure", "version": "v1.3.0", diff --git a/php/src/ContainerDefinitionFetcher.php b/php/src/ContainerDefinitionFetcher.php index 75bd2c2e..198c5061 100644 --- a/php/src/ContainerDefinitionFetcher.php +++ b/php/src/ContainerDefinitionFetcher.php @@ -12,6 +12,7 @@ use AIO\Container\State\RunningState; use AIO\Data\ConfigurationManager; use AIO\Data\DataConst; use AIO\Docker\DockerActionManager; +use JsonSchema\Validator; class ContainerDefinitionFetcher { @@ -40,12 +41,27 @@ class ContainerDefinitionFetcher throw new \Exception("The provided id " . $id . " was not found in the container definition."); } + private function validateJson(object $data): void { + // Validate against json schema + $validator = new Validator; + $validator->validate($data, (object)[file_get_contents(__DIR__ . '/../containers-schema.json')]); + if (!$validator->isValid()) { + error_log("JSON does not validate. Violations:"); + foreach ($validator->getErrors() as $error) { + error_log(printf("[%s] %s\n", $error['property'], $error['message'])); + } + } + } + /** * @return array */ private function GetDefinition(bool $latest): array { - $data = json_decode(file_get_contents(__DIR__ . '/../containers.json'), true); + $rawData = file_get_contents(__DIR__ . '/../containers.json'); + $objectData = json_decode($rawData, false); + $this->validateJson($objectData); + $data = json_decode($rawData, true); $containers = []; foreach ($data['aio_services_v1'] as $entry) {