@@ -14,9 +14,13 @@ class AdditionalItemsConstraint implements ConstraintInterface
14
14
{
15
15
use ErrorBagProxy;
16
16
17
+ /** @var \JsonSchema\Constraints\Drafts\Draft06\Factory */
18
+ private $ factory ;
19
+
17
20
public function __construct (?Factory $ factory = null )
18
21
{
19
- $ this ->initialiseErrorBag ($ factory ?: new Factory ());
22
+ $ this ->factory = $ factory ?: new Factory ();
23
+ $ this ->initialiseErrorBag ($ this ->factory );
20
24
}
21
25
22
26
public function check (&$ value , $ schema = null , ?JsonPointer $ path = null , $ i = null ): void
@@ -28,16 +32,31 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
28
32
if ($ schema ->additionalItems === true ) {
29
33
return ;
30
34
}
35
+ if ($ schema ->additionalItems === false && ! property_exists ($ schema , 'items ' )) {
36
+ return ;
37
+ }
31
38
32
39
if (!is_array ($ value )) {
33
40
return ;
34
41
}
42
+ if (!property_exists ($ schema , 'items ' )) {
43
+ return ;
44
+ }
45
+ if (property_exists ($ schema , 'items ' ) && is_object ($ schema ->items )) {
46
+ return ;
47
+ }
48
+
49
+ $ additionalItems = array_diff_key ($ value , property_exists ($ schema , 'items ' ) ? $ schema ->items : []);
35
50
51
+ foreach ($ additionalItems as $ propertyName => $ propertyValue ) {
52
+ $ schemaConstraint = $ this ->factory ->createInstanceFor ('schema ' );
53
+ $ schemaConstraint ->check ($ propertyValue , $ schema ->additionalItems , $ path , $ i );
36
54
37
- $ additionalItems = array_diff_key ($ value , $ schema ->items );
55
+ if ($ schemaConstraint ->isValid ()) {
56
+ continue ;
57
+ }
38
58
39
- foreach ($ additionalItems as $ key => $ _ ) {
40
- $ this ->addError (ConstraintError::ADDITIONAL_ITEMS (), $ path , ['item ' => $ i , 'property ' => $ key , 'additionalItems ' => $ schema ->additionalItems ]);
59
+ $ this ->addError (ConstraintError::ADDITIONAL_ITEMS (), $ path , ['item ' => $ i , 'property ' => $ propertyName , 'additionalItems ' => $ schema ->additionalItems ]);
41
60
}
42
61
43
62
0 commit comments