9
9
10
10
namespace JsonSchema \Constraints ;
11
11
12
+ use JsonSchema \Uri \UriResolver ;
13
+
12
14
/**
13
15
* The Undefined Constraints
14
16
*
@@ -26,11 +28,15 @@ public function check($value, $schema = null, $path = null, $i = null)
26
28
return ;
27
29
}
28
30
31
+ $ i = is_null ($ i ) ? "" : $ i ;
29
32
$ path = $ this ->incrementPath ($ path , $ i );
30
33
31
34
// check special properties
32
35
$ this ->validateCommonProperties ($ value , $ schema , $ path );
33
36
37
+ // check allOf, anyOf, and oneOf properties
38
+ $ this ->validateOfProperties ($ value , $ schema , $ path );
39
+
34
40
// check known types
35
41
$ this ->validateTypes ($ value , $ schema , $ path , $ i );
36
42
}
@@ -85,20 +91,19 @@ public function validateTypes($value, $schema = null, $path = null, $i = null)
85
91
* @param string $path
86
92
* @param string $i
87
93
*/
88
- protected function validateCommonProperties ($ value , $ schema = null , $ path = null , $ i = null )
94
+ protected function validateCommonProperties ($ value , $ schema = null , $ path = null , $ i = "" )
89
95
{
90
96
// if it extends another schema, it must pass that schema as well
91
97
if (isset ($ schema ->extends )) {
92
98
if (is_string ($ schema ->extends )) {
93
99
$ schema ->extends = $ this ->validateUri ($ schema , $ schema ->extends );
94
100
}
95
- $ increment = is_null ($ i ) ? "" : $ i ;
96
101
if (is_array ($ schema ->extends )) {
97
102
foreach ($ schema ->extends as $ extends ) {
98
- $ this ->checkUndefined ($ value , $ extends , $ path , $ increment );
103
+ $ this ->checkUndefined ($ value , $ extends , $ path , $ i );
99
104
}
100
105
} else {
101
- $ this ->checkUndefined ($ value , $ schema ->extends , $ path , $ increment );
106
+ $ this ->checkUndefined ($ value , $ schema ->extends , $ path , $ i );
102
107
}
103
108
}
104
109
@@ -133,7 +138,19 @@ protected function validateCommonProperties($value, $schema = null, $path = null
133
138
134
139
// if no new errors were raised it must be a disallowed value
135
140
if (count ($ this ->getErrors ()) == count ($ initErrors )) {
136
- $ this ->addError ($ path , " disallowed value was matched " );
141
+ $ this ->addError ($ path , "disallowed value was matched " );
142
+ } else {
143
+ $ this ->errors = $ initErrors ;
144
+ }
145
+ }
146
+
147
+ if (isset ($ schema ->not )) {
148
+ $ initErrors = $ this ->getErrors ();
149
+ $ this ->checkUndefined ($ value , $ schema ->not , $ path , $ i );
150
+
151
+ // if no new errors were raised then the instance validated against the "not" schema
152
+ if (count ($ this ->getErrors ()) == count ($ initErrors )) {
153
+ $ this ->addError ($ path , "matched a schema which it should not " );
137
154
} else {
138
155
$ this ->errors = $ initErrors ;
139
156
}
@@ -143,12 +160,12 @@ protected function validateCommonProperties($value, $schema = null, $path = null
143
160
if (is_object ($ value )) {
144
161
if (isset ($ schema ->minProperties )) {
145
162
if (count (get_object_vars ($ value )) < $ schema ->minProperties ) {
146
- $ this ->addError ($ path , "must contain a minimum of " + $ schema ->minProperties + " properties " );
163
+ $ this ->addError ($ path , "must contain a minimum of " . $ schema ->minProperties . " properties " );
147
164
}
148
165
}
149
166
if (isset ($ schema ->maxProperties )) {
150
167
if (count (get_object_vars ($ value )) > $ schema ->maxProperties ) {
151
- $ this ->addError ($ path , "must contain no more than " + $ schema ->maxProperties + " properties " );
168
+ $ this ->addError ($ path , "must contain no more than " . $ schema ->maxProperties . " properties " );
152
169
}
153
170
}
154
171
}
@@ -159,14 +176,72 @@ protected function validateCommonProperties($value, $schema = null, $path = null
159
176
}
160
177
}
161
178
179
+ /**
180
+ * Validate allOf, anyOf, and oneOf properties
181
+ *
182
+ * @param mixed $value
183
+ * @param mixed $schema
184
+ * @param string $path
185
+ * @param string $i
186
+ */
187
+ protected function validateOfProperties ($ value , $ schema , $ path , $ i = "" )
188
+ {
189
+ if (isset ($ schema ->allOf )) {
190
+ $ isValid = true ;
191
+ foreach ($ schema ->allOf as $ allOf ) {
192
+ $ initErrors = $ this ->getErrors ();
193
+ $ this ->checkUndefined ($ value , $ allOf , $ path , $ i );
194
+ $ isValid = $ isValid && (count ($ this ->getErrors ()) == count ($ initErrors ));
195
+ }
196
+ if (!$ isValid ) {
197
+ $ this ->addError ($ path , "failed to match all schemas " );
198
+ }
199
+ }
200
+
201
+ if (isset ($ schema ->anyOf )) {
202
+ $ isValid = false ;
203
+ $ startErrors = $ this ->getErrors ();
204
+ foreach ($ schema ->anyOf as $ anyOf ) {
205
+ $ initErrors = $ this ->getErrors ();
206
+ $ this ->checkUndefined ($ value , $ anyOf , $ path , $ i );
207
+ if ($ isValid = (count ($ this ->getErrors ()) == count ($ initErrors ))) {
208
+ break ;
209
+ }
210
+ }
211
+ if (!$ isValid ) {
212
+ $ this ->addError ($ path , "failed to match at least one schema " );
213
+ } else {
214
+ $ this ->errors = $ startErrors ;
215
+ }
216
+ }
217
+
218
+ if (isset ($ schema ->oneOf )) {
219
+ $ matchedSchemas = 0 ;
220
+ $ startErrors = $ this ->getErrors ();
221
+ foreach ($ schema ->oneOf as $ oneOf ) {
222
+ $ initErrors = $ this ->getErrors ();
223
+ $ this ->checkUndefined ($ value , $ oneOf , $ path , $ i );
224
+ if (count ($ this ->getErrors ()) == count ($ initErrors )) {
225
+ $ matchedSchemas ++;
226
+ }
227
+ }
228
+ if ($ matchedSchemas !== 1 ) {
229
+ $ this ->addError ($ path , "failed to match exactly one schema " );
230
+ } else {
231
+ $ this ->errors = $ startErrors ;
232
+ }
233
+ }
234
+ }
235
+
162
236
/**
163
237
* Validate dependencies
164
238
*
165
239
* @param mixed $value
166
240
* @param mixed $dependencies
167
241
* @param string $path
242
+ * @param string $i
168
243
*/
169
- protected function validateDependencies ($ value , $ dependencies , $ path )
244
+ protected function validateDependencies ($ value , $ dependencies , $ path, $ i = "" )
170
245
{
171
246
foreach ($ dependencies as $ key => $ dependency ) {
172
247
if (property_exists ($ value , $ key )) {
@@ -184,22 +259,23 @@ protected function validateDependencies($value, $dependencies, $path)
184
259
}
185
260
} else if (is_object ($ dependency )) {
186
261
// Schema - e.g. "dependencies": {"bar": {"properties": {"foo": {...}}}}
187
- $ this ->checkUndefined ($ value , $ dependency , $ path , "" );
262
+ $ this ->checkUndefined ($ value , $ dependency , $ path , $ i );
188
263
}
189
264
}
190
265
}
191
266
}
192
267
193
268
protected function validateUri ($ schema , $ schemaUri = null )
194
269
{
195
- $ resolver = new \ JsonSchema \ Uri \ UriResolver ();
270
+ $ resolver = new UriResolver ();
196
271
$ retriever = $ this ->getUriRetriever ();
197
272
273
+ $ jsonSchema = null ;
198
274
if ($ resolver ->isValid ($ schemaUri )) {
199
275
$ schemaId = property_exists ($ schema , 'id ' ) ? $ schema ->id : null ;
200
276
$ jsonSchema = $ retriever ->retrieve ($ schemaId , $ schemaUri );
201
-
202
- return $ jsonSchema ;
203
277
}
278
+
279
+ return $ jsonSchema ;
204
280
}
205
281
}
0 commit comments