Skip to content

Commit 90cce2c

Browse files
justinrainbow/json-schema version 3
1 parent d823428 commit 90cce2c

File tree

5 files changed

+14
-50
lines changed

5 files changed

+14
-50
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"require": {
1212
"php": "^5.3.3|^7.0",
13-
"justinrainbow/json-schema": "^2.0",
13+
"justinrainbow/json-schema": "^3.0",
1414
"seld/jsonlint": "^1.0",
1515
"webmozart/assert": "^1.0",
1616
"webmozart/path-util": "^2.3"

src/JsonValidator.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
use JsonSchema\Exception\InvalidArgumentException;
1515
use JsonSchema\Exception\ResourceNotFoundException;
16-
use JsonSchema\RefResolver;
17-
use JsonSchema\Uri\UriResolver;
1816
use JsonSchema\Uri\UriRetriever;
19-
use JsonSchema\UriResolverInterface;
2017
use JsonSchema\Validator;
2118
use Webmozart\PathUtil\Path;
2219

@@ -47,26 +44,20 @@ class JsonValidator
4744
*/
4845
private $validator;
4946

50-
/**
51-
* Reference resolver.
52-
*
53-
* @var RefResolver
54-
*/
55-
private $resolver;
56-
5747
/**
5848
* JsonValidator constructor.
5949
*
6050
* @param Validator|null $validator JsonSchema\Validator
6151
* instance to use
6252
* @param UriRetriever|null $uriRetriever The retriever for fetching
6353
* JSON schemas
64-
* @param UriResolverInterface|null $uriResolver The resolver for URIs
6554
*/
66-
public function __construct(Validator $validator = null, UriRetriever $uriRetriever = null, UriResolverInterface $uriResolver = null)
55+
public function __construct(Validator $validator = null, UriRetriever $uriRetriever = null)
6756
{
6857
$this->validator = $validator ?: new Validator();
69-
$this->resolver = new RefResolver($uriRetriever ?: new UriRetriever(), $uriResolver ?: new UriResolver());
58+
if($uriRetriever) {
59+
$this->validator->setUriRetriever($uriRetriever);
60+
}
7061
}
7162

7263
/**
@@ -107,6 +98,8 @@ public function validate($data, $schema = null)
10798

10899
try {
109100
$this->validator->check($data, $schema);
101+
} catch (ResourceNotFoundException $e) {
102+
throw new InvalidSchemaException($e->getMessage(), $e->getCode(), $e);
110103
} catch (InvalidArgumentException $e) {
111104
throw new InvalidSchemaException(sprintf(
112105
'The schema is invalid: %s',
@@ -134,6 +127,8 @@ private function assertSchemaValid($schema)
134127
// The meta schema is obviously not validated. If we
135128
// validate it against itself, we have an endless recursion
136129
$this->metaSchema = json_decode(file_get_contents(__DIR__.'/../res/meta-schema.json'));
130+
131+
$this->validator->getSchemaStorage()->addSchema('http://json-schema.org/draft-04/schema', $this->metaSchema);
137132
}
138133

139134
if ($schema === $this->metaSchema) {
@@ -148,6 +143,10 @@ private function assertSchemaValid($schema)
148143
implode("\n", $errors)
149144
));
150145
}
146+
147+
if(!isset($schema->{'$ref'})) {
148+
$this->validator->getSchemaStorage()->addSchema($schema->id, $schema);
149+
}
151150
}
152151

153152
private function loadSchema($file)
@@ -160,15 +159,7 @@ private function loadSchema($file)
160159
$file = 'file://'.$file;
161160
}
162161

163-
// Resolve references to other schemas
164-
try {
165-
$schema = $this->resolver->resolve($file);
166-
} catch (ResourceNotFoundException $e) {
167-
throw new InvalidSchemaException(sprintf(
168-
'The schema %s does not exist.',
169-
$file
170-
), 0, $e);
171-
}
162+
$schema = (object) array('$ref' => $file);
172163

173164
try {
174165
$this->assertSchemaValid($schema);

tests/Fixtures/schema-invalid.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/Fixtures/schema-no-object.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/JsonValidatorTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,6 @@ public function testValidateFailsIfSchemaNeitherStringNorObject()
180180
$this->validator->validate((object) array('name' => 'Bernhard'), 12345);
181181
}
182182

183-
/**
184-
* @expectedException \Webmozart\Json\InvalidSchemaException
185-
*/
186-
public function testValidateFailsIfSchemaFileContainsNoObject()
187-
{
188-
$this->validator->validate(
189-
(object) array('name' => 'Bernhard'),
190-
$this->fixturesDir.'/schema-no-object.json'
191-
);
192-
}
193-
194-
/**
195-
* @expectedException \Webmozart\Json\InvalidSchemaException
196-
*/
197-
public function testValidateFailsIfSchemaFileInvalid()
198-
{
199-
$this->validator->validate(
200-
(object) array('name' => 'Bernhard'),
201-
$this->fixturesDir.'/schema-invalid.json'
202-
);
203-
}
204-
205183
/**
206184
* @expectedException \Webmozart\Json\InvalidSchemaException
207185
*/

0 commit comments

Comments
 (0)