Skip to content

Commit

Permalink
Change json schema validator (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell authored Jun 19, 2019
1 parent e6d3c8b commit fb0aed7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 51 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
73 changes: 35 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions src/ETL/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Harvest\ETL;

use Harvest\Storage\Storage;
use JsonSchema\Validator;
use Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
};

class Factory {

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion tests/HarvesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit fb0aed7

Please sign in to comment.