Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Add provided schema under a dummy / internal URI (fixes #376) #378

Merged
merged 3 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JsonSchema;

use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\InvalidArgumentException;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this referenced anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope - well spotted; I've removed it.

use JsonSchema\Exception\UnresolvableJsonPointerException;
use JsonSchema\Iterator\ObjectIterator;
use JsonSchema\Uri\UriResolver;
Expand Down Expand Up @@ -43,7 +44,10 @@ public function getUriResolver()
*/
public function addSchema($id, $schema = null)
{
if (is_null($schema)) {
if (is_null($schema) && $id !== 'internal://provided-schema') {
// 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
3 changes: 3 additions & 0 deletions src/JsonSchema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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('internal://provided-schema', $schema);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a class constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea - have made it so.


$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