@@ -26,10 +26,14 @@ public function check($value, $schema = null, $path = null, $i = null)
26
26
return ;
27
27
}
28
28
29
+ $ i = is_null ($ i ) ? "" : $ i ;
29
30
$ path = $ this ->incrementPath ($ path , $ i );
30
31
31
32
// check special properties
32
- $ this ->validateCommonProperties ($ value , $ schema , $ path );
33
+ $ this ->validateCommonProperties ($ value , $ schema , $ path , $ i );
34
+
35
+ // check allOf, anyOf, and oneOf properties
36
+ $ this ->validateOfProperties ($ value , $ schema , $ path , $ i );
33
37
34
38
// check known types
35
39
$ this ->validateTypes ($ value , $ schema , $ path , $ i );
@@ -92,13 +96,12 @@ protected function validateCommonProperties($value, $schema = null, $path = null
92
96
if (is_string ($ schema ->extends )) {
93
97
$ schema ->extends = $ this ->validateUri ($ schema , $ schema ->extends );
94
98
}
95
- $ increment = is_null ($ i ) ? "" : $ i ;
96
99
if (is_array ($ schema ->extends )) {
97
100
foreach ($ schema ->extends as $ extends ) {
98
- $ this ->checkUndefined ($ value , $ extends , $ path , $ increment );
101
+ $ this ->checkUndefined ($ value , $ extends , $ path , $ i );
99
102
}
100
103
} else {
101
- $ this ->checkUndefined ($ value , $ schema ->extends , $ path , $ increment );
104
+ $ this ->checkUndefined ($ value , $ schema ->extends , $ path , $ i );
102
105
}
103
106
}
104
107
@@ -117,10 +120,10 @@ protected function validateCommonProperties($value, $schema = null, $path = null
117
120
}
118
121
}
119
122
} else {
120
- $ this ->checkType ($ value , $ schema , $ path );
123
+ $ this ->checkType ($ value , $ schema , $ path, $ i );
121
124
}
122
125
} else {
123
- $ this ->checkType ($ value , $ schema , $ path );
126
+ $ this ->checkType ($ value , $ schema , $ path, $ i );
124
127
}
125
128
126
129
// Verify disallowed items
@@ -129,23 +132,23 @@ protected function validateCommonProperties($value, $schema = null, $path = null
129
132
130
133
$ typeSchema = new \stdClass ();
131
134
$ typeSchema ->type = $ schema ->disallow ;
132
- $ this ->checkType ($ value , $ typeSchema , $ path );
135
+ $ this ->checkType ($ value , $ typeSchema , $ path, $ i );
133
136
134
137
// if no new errors were raised it must be a disallowed value
135
138
if (count ($ this ->getErrors ()) == count ($ initErrors )) {
136
- $ this ->addError ($ path , " disallowed value was matched " );
139
+ $ this ->addError ($ path , "disallowed value was matched " );
137
140
} else {
138
141
$ this ->errors = $ initErrors ;
139
142
}
140
143
}
141
144
142
145
if (isset ($ schema ->not )) {
143
146
$ initErrors = $ this ->getErrors ();
144
- $ this ->checkUndefined ($ value , $ schema ->not , $ path , is_null ( $ i ) ? "" : $ i );
147
+ $ this ->checkUndefined ($ value , $ schema ->not , $ path , $ i );
145
148
146
149
// if no new errors were raised then the instance validated against the "not" schema
147
150
if (count ($ this ->getErrors ()) == count ($ initErrors )) {
148
- $ this ->addError ($ path , "matches a schema which it should not " );
151
+ $ this ->addError ($ path , "matched a schema which it should not " );
149
152
} else {
150
153
$ this ->errors = $ initErrors ;
151
154
}
@@ -167,7 +170,64 @@ protected function validateCommonProperties($value, $schema = null, $path = null
167
170
168
171
// Verify that dependencies are met
169
172
if (is_object ($ value ) && isset ($ schema ->dependencies )) {
170
- $ this ->validateDependencies ($ value , $ schema ->dependencies , $ path );
173
+ $ this ->validateDependencies ($ value , $ schema ->dependencies , $ path , $ i );
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Validate allOf, anyOf, and oneOf properties
179
+ *
180
+ * @param mixed $value
181
+ * @param mixed $schema
182
+ * @param string $path
183
+ * @param string $i
184
+ */
185
+ protected function validateOfProperties ($ value , $ schema , $ path , $ i )
186
+ {
187
+ if (isset ($ schema ->allOf )) {
188
+ $ isValid = true ;
189
+ foreach ($ schema ->allOf as $ allOf ) {
190
+ $ initErrors = $ this ->getErrors ();
191
+ $ this ->checkUndefined ($ value , $ allOf , $ path , $ i );
192
+ $ isValid = $ isValid && (count ($ this ->getErrors ()) == count ($ initErrors ));
193
+ }
194
+ if (!$ isValid ) {
195
+ $ this ->addError ($ path , "failed to match all schemas " );
196
+ }
197
+ }
198
+
199
+ if (isset ($ schema ->anyOf )) {
200
+ $ isValid = false ;
201
+ $ startErrors = $ this ->getErrors ();
202
+ foreach ($ schema ->anyOf as $ anyOf ) {
203
+ $ initErrors = $ this ->getErrors ();
204
+ $ this ->checkUndefined ($ value , $ anyOf , $ path , $ i );
205
+ if (!$ isValid ) {
206
+ $ isValid = (count ($ this ->getErrors ()) == count ($ initErrors ));
207
+ }
208
+ }
209
+ if (!$ isValid ) {
210
+ $ this ->addError ($ path , "failed to match at least one schema " );
211
+ } else {
212
+ $ this ->errors = $ startErrors ;
213
+ }
214
+ }
215
+
216
+ if (isset ($ schema ->oneOf )) {
217
+ $ matchedSchemas = 0 ;
218
+ $ startErrors = $ this ->getErrors ();
219
+ foreach ($ schema ->oneOf as $ oneOf ) {
220
+ $ initErrors = $ this ->getErrors ();
221
+ $ this ->checkUndefined ($ value , $ oneOf , $ path , $ i );
222
+ if (count ($ this ->getErrors ()) == count ($ initErrors )) {
223
+ $ matchedSchemas ++;
224
+ }
225
+ }
226
+ if ($ matchedSchemas !== 1 ) {
227
+ $ this ->addError ($ path , "failed to match exactly one schema " );
228
+ } else {
229
+ $ this ->errors = $ startErrors ;
230
+ }
171
231
}
172
232
}
173
233
@@ -177,8 +237,9 @@ protected function validateCommonProperties($value, $schema = null, $path = null
177
237
* @param mixed $value
178
238
* @param mixed $dependencies
179
239
* @param string $path
240
+ * @param string $i
180
241
*/
181
- protected function validateDependencies ($ value , $ dependencies , $ path )
242
+ protected function validateDependencies ($ value , $ dependencies , $ path, $ i )
182
243
{
183
244
foreach ($ dependencies as $ key => $ dependency ) {
184
245
if (property_exists ($ value , $ key )) {
@@ -196,7 +257,7 @@ protected function validateDependencies($value, $dependencies, $path)
196
257
}
197
258
} else if (is_object ($ dependency )) {
198
259
// Schema - e.g. "dependencies": {"bar": {"properties": {"foo": {...}}}}
199
- $ this ->checkUndefined ($ value , $ dependency , $ path , "" );
260
+ $ this ->checkUndefined ($ value , $ dependency , $ path , $ i );
200
261
}
201
262
}
202
263
}
0 commit comments