Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Update justinrainbow/json-schema to ~5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Apr 13, 2017
1 parent c50b287 commit 1340e5f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"php": ">=5.4",
"behat/behat": "~3.0",
"behat/mink-extension": "~2.0",
"justinrainbow/json-schema": "~1.4",
"justinrainbow/json-schema": "~5.0",
"symfony/property-access": "~2.3|~3.0",
"symfony/http-foundation": "~2.3|~3.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Json/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class Json
{
private $content;
protected $content;

public function __construct($content)
{
Expand Down
22 changes: 16 additions & 6 deletions src/Json/JsonInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Behatch\Json;

use JsonSchema\RefResolver;
use JsonSchema\Validator;
use JsonSchema\Uri\UriRetriever;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class JsonInspector
Expand Down Expand Up @@ -34,9 +32,21 @@ public function evaluate(Json $json, $expression)

public function validate(Json $json, JsonSchema $schema)
{
return $schema
->resolve(new RefResolver(new UriRetriever))
->validate($json, new Validator)
;
$validator = new \JsonSchema\Validator();

$resolver = new \JsonSchema\SchemaStorage(new \JsonSchema\Uri\UriRetriever, new \JsonSchema\Uri\UriResolver);
$schema->resolve($resolver);

$validator->check($json->getContent(), $schema->getContent());
$isValid = $validator->isValid();
if (!$isValid) {
$msg = "JSON does not validate. Violations:".PHP_EOL;
foreach ($validator->getErrors() as $error) {
$msg .= sprintf(" - [%s] %s".PHP_EOL, $error['property'], $error['message']);
}
throw new \Exception($msg);
}

return $isValid;
}
}
7 changes: 4 additions & 3 deletions src/Json/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Behatch\Json;

use JsonSchema\RefResolver;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;

class JsonSchema extends Json
Expand All @@ -12,16 +12,17 @@ class JsonSchema extends Json
public function __construct($content, $uri = null)
{
$this->uri = $uri;

parent::__construct($content);
}

public function resolve(RefResolver $resolver)
public function resolve(SchemaStorage $resolver)
{
if (!$this->hasUri()) {
return $this;
}

$resolver->resolve($this->getContent(), $this->uri);
$this->content = $resolver->resolveRef($this->uri);

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/files/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 0 additions & 1 deletion tests/units/Json/JsonInspector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Behatch\Tests\Units\Json;

use JsonSchema\RefResolver;
use JsonSchema\Validator;
use JsonSchema\Uri\UriRetriever;
use Symfony\Component\PropertyAccess\PropertyAccess;
Expand Down
11 changes: 6 additions & 5 deletions tests/units/Json/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ class JsonSchema extends \atoum
public function test_resolve_without_uri()
{
$schema = $this->newTestedInstance('{}');
$resolver = new \JsonSchema\RefResolver();
$resolver = new \JsonSchema\SchemaStorage(new \JsonSchema\Uri\UriRetriever, new \JsonSchema\Uri\UriResolver);
$schema->resolve($resolver);
}

public function test_resolve_with_uri()
{
$schema = $this->newTestedInstance('{}', 'file://test');
$resolver = new \JsonSchema\RefResolver();
$result = $schema->resolve($resolver);
$file = 'file://' . __DIR__ . '/../../fixtures/files/schema.json';
$schema = (object)['id' => $file];
$resolver = new \JsonSchema\SchemaStorage(new \JsonSchema\Uri\UriRetriever, new \JsonSchema\Uri\UriResolver);
$result = $resolver->resolveRef($file);

$this->object($result)
->isIdenticalTo($schema);
->isEqualTo($schema);
}

public function test_validate()
Expand Down

0 comments on commit 1340e5f

Please sign in to comment.