Skip to content

Commit

Permalink
[BUGFIX] Add provided schema under a dummy / internal URI (fixes json…
Browse files Browse the repository at this point in the history
…rainbow#376) (jsonrainbow#378)

* Add provided schema under a dummy / internal URI (fixes jsonrainbow#376)

In order to resolve internal $ref references within a user-provided
schema, SchemaStorage needs to know about the schema. As user-supplied
schemas do not have an associated URI, use a dummy / internal one instead.

* Remove dangling use

* Change URI to class constant on SchemaStorage
  • Loading branch information
erayd committed Mar 17, 2017
1 parent 4c11c17 commit 9d0921e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class SchemaStorage implements SchemaStorageInterface
{
const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema';

protected $uriRetriever;
protected $uriResolver;
protected $schemas = array();
Expand Down Expand Up @@ -43,7 +45,10 @@ public function getUriResolver()
*/
public function addSchema($id, $schema = null)
{
if (is_null($schema)) {
if (is_null($schema) && $id !== self::INTERNAL_PROVIDED_SCHEMA_URI) {
// if the schema was user-provided to Validator and is still null, then assume this is
// what the user intended, as there's no way for us to retrieve anything else. User-supplied
// schemas do not have an associated URI when passed via Validator::validate().
$schema = $this->uriRetriever->retrieve($id);
}
$objectIterator = new ObjectIterator($schema);
Expand Down
4 changes: 4 additions & 0 deletions src/JsonSchema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\InvalidConfigException;
use JsonSchema\SchemaStorage;

/**
* A JsonSchema Constraint
Expand Down Expand Up @@ -41,6 +42,9 @@ public function validate(&$value, $schema = null, $checkMode = null)
$this->factory->setConfig($checkMode);
}

// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
$this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);

$validator = $this->factory->createInstanceFor('schema');
$validator->check($value, $schema);

Expand Down
10 changes: 10 additions & 0 deletions tests/SchemaStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use JsonSchema\SchemaStorage;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;
use Prophecy\Argument;

class SchemaStorageTest extends \PHPUnit_Framework_TestCase
Expand All @@ -31,6 +32,15 @@ public function testResolveRef()
);
}

public function testResolveTopRef()
{
$input = json_decode('{"propertyOne":"notANumber"}');
$schema = json_decode('{"$ref":"#/definition","definition":{"properties":{"propertyOne":{"type":"number"}}}}');
$v = new Validator();
$v->validate($input, $schema);
$this->assertFalse($v->isValid());
}

/**
* @depends testResolveRef
*/
Expand Down

0 comments on commit 9d0921e

Please sign in to comment.