1313
1414use JsonSchema \Exception \InvalidArgumentException ;
1515use JsonSchema \Exception \ResourceNotFoundException ;
16- use JsonSchema \RefResolver ;
17- use JsonSchema \Uri \UriResolver ;
1816use JsonSchema \Uri \UriRetriever ;
19- use JsonSchema \UriResolverInterface ;
2017use JsonSchema \Validator ;
2118use 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 *
60- * @param Validator|null $validator JsonSchema\Validator
61- * instance to use
62- * @param UriRetriever|null $uriRetriever The retriever for fetching
63- * JSON schemas
64- * @param UriResolverInterface|null $uriResolver The resolver for URIs
50+ * @param Validator|null $validator JsonSchema\Validator instance
51+ * to use
52+ * @param UriRetriever|null $uriRetriever The retriever for fetching
53+ * JSON schemas
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 );
0 commit comments