Merge pull request #2611 from nextcloud/enh/noid/add-justinrainbow-json-schema

validate json against json schema
This commit is contained in:
Simon L 2023-05-30 10:56:26 +02:00 committed by GitHub
commit 72856c4eeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 3 deletions

View file

@ -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",

72
php/composer.lock generated
View file

@ -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",

View file

@ -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) {