From fb0aed792d2833c818da41e189cb0fdc2288db98 Mon Sep 17 00:00:00 2001 From: fmizzell Date: Wed, 19 Jun 2019 15:21:30 -0500 Subject: [PATCH] Change json schema validator (#5) --- composer.json | 4 +-- composer.lock | 73 ++++++++++++++++++++--------------------- src/ETL/Factory.php | 32 ++++++++++++------ tests/HarvesterTest.php | 2 +- 4 files changed, 60 insertions(+), 51 deletions(-) diff --git a/composer.json b/composer.json index 3749b63..3022099 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ ], "require": { "fmizzell/contracts": "dev-master", - "justinrainbow/json-schema": "^5.2", - "guzzlehttp/guzzle": "^6.3" + "guzzlehttp/guzzle": "^6.3", + "opis/json-schema": "^1.0" } } diff --git a/composer.lock b/composer.lock index 99301ed..ed5dc8f 100644 --- a/composer.lock +++ b/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": "097a854a6f0f23053c44a75f36b9e20e", + "content-hash": "64e6f99e3385b2b666f73d17f07de10c", "packages": [ { "name": "fmizzell/contracts", @@ -226,70 +226,67 @@ "time": "2018-12-04T20:46:45+00:00" }, { - "name": "justinrainbow/json-schema", - "version": "5.2.8", + "name": "opis/json-schema", + "version": "1.0.18", "source": { "type": "git", - "url": "https://github.com/justinrainbow/json-schema.git", - "reference": "dcb6e1006bb5fd1e392b4daa68932880f37550d4" + "url": "https://github.com/opis/json-schema.git", + "reference": "19868514d5e4c27553b21c7f0cf6388734c67d18" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/justinrainbow/json-schema/zipball/dcb6e1006bb5fd1e392b4daa68932880f37550d4", - "reference": "dcb6e1006bb5fd1e392b4daa68932880f37550d4", + "url": "https://api.github.com/repos/opis/json-schema/zipball/19868514d5e4c27553b21c7f0cf6388734c67d18", + "reference": "19868514d5e4c27553b21c7f0cf6388734c67d18", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": ">=7.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "~2.2.20", - "json-schema/json-schema-test-suite": "1.2.0", - "phpunit/phpunit": "^4.8.35" + "ext-bcmath": "*", + "ext-intl": "*", + "ext-json": "*", + "ext-mbstring": "*", + "opis/string": "^1.4", + "phpunit/phpunit": "^6.5" + }, + "suggest": { + "opis/string": "A standalone library for manipulating multibyte strings" }, - "bin": [ - "bin/validate-json" - ], "type": "library", "extra": { "branch-alias": { - "dev-master": "5.0.x-dev" + "dev-master": "1.0.x-dev" } }, "autoload": { "psr-4": { - "JsonSchema\\": "src/JsonSchema/" + "Opis\\JsonSchema\\": "src/" } }, "notification-url": "https://packagist.org/downloads/", "license": [ - "MIT" + "Apache-2.0" ], "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": "Marius Sarca", + "email": "marius.sarca@gmail.com" }, { - "name": "Robert Schönthal", - "email": "seroscho@googlemail.com" + "name": "Sorin Sarca", + "email": "sarca_sorin@hotmail.com" } ], - "description": "A library to validate a json schema.", - "homepage": "https://github.com/justinrainbow/json-schema", + "description": "Json Schema Validator", + "homepage": "http://www.opis.io/json-schema", "keywords": [ "json", - "schema" + "schema", + "validation", + "validator" ], - "time": "2019-01-14T23:55:14+00:00" + "time": "2019-05-22T05:16:45+00:00" }, { "name": "psr/http-message", @@ -1058,16 +1055,16 @@ }, { "name": "phpunit/phpunit", - "version": "7.5.12", + "version": "7.5.13", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "9ba59817745b0fe0c1a5a3032dfd4a6d2994ad1c" + "reference": "b9278591caa8630127f96c63b598712b699e671c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9ba59817745b0fe0c1a5a3032dfd4a6d2994ad1c", - "reference": "9ba59817745b0fe0c1a5a3032dfd4a6d2994ad1c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/b9278591caa8630127f96c63b598712b699e671c", + "reference": "b9278591caa8630127f96c63b598712b699e671c", "shasum": "" }, "require": { @@ -1138,7 +1135,7 @@ "testing", "xunit" ], - "time": "2019-05-28T11:59:40+00:00" + "time": "2019-06-19T12:01:51+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/src/ETL/Factory.php b/src/ETL/Factory.php index ff219b4..fecf5f8 100644 --- a/src/ETL/Factory.php +++ b/src/ETL/Factory.php @@ -3,7 +3,9 @@ namespace Harvest\ETL; use Harvest\Storage\Storage; -use JsonSchema\Validator; +use Opis\JsonSchema\{ + Validator, ValidationResult, ValidationError, Schema +}; class Factory { @@ -12,7 +14,9 @@ class Factory { private $hashStorage; public function __construct($harvest_plan, Storage $item_storage, Storage $hash_storage) { - $this->validateHarvestPlan($harvest_plan); + if (self::validateHarvestPlan($harvest_plan)) { + $this->harvestPlan = $harvest_plan; + } $this->itemStorage = $item_storage; $this->hashStorage = $hash_storage; } @@ -54,21 +58,29 @@ private function getOne($class, $config = NULL) { return new $class($config); } - private function validateHarvestPlan($harvest_plan) { - + public static function validateHarvestPlan(object $harvest_plan) { $path_to_schema = __DIR__ . "/../../schema/schema.json"; $json_schema = file_get_contents($path_to_schema); $schema = json_decode($json_schema); - $validator = new Validator; - $validator->validate($harvest_plan, $schema); + if ($schema == null) { + throw new \Exception("the json-schema is invalid json."); + } - $is_valid = $validator->isValid(); + $data = $harvest_plan; + $schema = Schema::fromJsonString($json_schema); + $validator = new Validator(); - if (!$is_valid) { - throw new \Exception(json_encode(['valid' => $is_valid, 'errors' => $validator->getErrors()])); + /** @var $result ValidationResult */ + $result = $validator->schemaValidation($data, $schema); + + if (!$result->isValid()) { + /** @var $error ValidationError */ + $error = $result->getFirstError(); + throw new \Exception("Invalid harvest plan. " . implode("->", $error->dataPointer()) . " " . json_encode($error->keywordArgs())); } - $this->harvestPlan = $harvest_plan; + + return TRUE; } } diff --git a/tests/HarvesterTest.php b/tests/HarvesterTest.php index 41c456c..c300806 100644 --- a/tests/HarvesterTest.php +++ b/tests/HarvesterTest.php @@ -5,7 +5,7 @@ class HarvesterTest extends \PHPUnit\Framework\TestCase { public function testPlanValidation() { - $this->expectExceptionMessage("{\"valid\":false,\"errors\":[{\"property\":\"load.type\",\"pointer\":\"\/load\/type\",\"message\":\"The property type is required\",\"constraint\":\"required\",\"context\":1}]}"); + $this->expectExceptionMessage("Invalid harvest plan. load {\"missing\":\"type\"}"); $plan = $this->getPlan("badplan"); $this->getHarvester($plan, new MemStore(), new MemStore()); }