13
13
14
14
use JsonSchema \Exception \InvalidArgumentException ;
15
15
use JsonSchema \Exception \ResourceNotFoundException ;
16
- use JsonSchema \RefResolver ;
17
- use JsonSchema \Uri \UriResolver ;
18
16
use JsonSchema \Uri \UriRetriever ;
19
- use JsonSchema \UriResolverInterface ;
20
17
use JsonSchema \Validator ;
21
18
use Webmozart \PathUtil \Path ;
22
19
@@ -47,26 +44,20 @@ class JsonValidator
47
44
*/
48
45
private $ validator ;
49
46
50
- /**
51
- * Reference resolver.
52
- *
53
- * @var RefResolver
54
- */
55
- private $ resolver ;
56
-
57
47
/**
58
48
* JsonValidator constructor.
59
49
*
60
50
* @param Validator|null $validator JsonSchema\Validator
61
51
* instance to use
62
52
* @param UriRetriever|null $uriRetriever The retriever for fetching
63
53
* JSON schemas
64
- * @param UriResolverInterface|null $uriResolver The resolver for URIs
65
54
*/
66
- public function __construct (Validator $ validator = null , UriRetriever $ uriRetriever = null , UriResolverInterface $ uriResolver = null )
55
+ public function __construct (Validator $ validator = null , UriRetriever $ uriRetriever = null )
67
56
{
68
57
$ 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
+ }
70
61
}
71
62
72
63
/**
@@ -107,6 +98,8 @@ public function validate($data, $schema = null)
107
98
108
99
try {
109
100
$ this ->validator ->check ($ data , $ schema );
101
+ } catch (ResourceNotFoundException $ e ) {
102
+ throw new InvalidSchemaException ($ e ->getMessage (), $ e ->getCode (), $ e );
110
103
} catch (InvalidArgumentException $ e ) {
111
104
throw new InvalidSchemaException (sprintf (
112
105
'The schema is invalid: %s ' ,
@@ -134,6 +127,8 @@ private function assertSchemaValid($schema)
134
127
// The meta schema is obviously not validated. If we
135
128
// validate it against itself, we have an endless recursion
136
129
$ 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 );
137
132
}
138
133
139
134
if ($ schema === $ this ->metaSchema ) {
@@ -148,6 +143,10 @@ private function assertSchemaValid($schema)
148
143
implode ("\n" , $ errors )
149
144
));
150
145
}
146
+
147
+ if (!isset ($ schema ->{'$ref ' })) {
148
+ $ this ->validator ->getSchemaStorage ()->addSchema ($ schema ->id , $ schema );
149
+ }
151
150
}
152
151
153
152
private function loadSchema ($ file )
@@ -160,15 +159,7 @@ private function loadSchema($file)
160
159
$ file = 'file:// ' .$ file ;
161
160
}
162
161
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 );
172
163
173
164
try {
174
165
$ this ->assertSchemaValid ($ schema );
0 commit comments