Skip to content

Commit 29b5eaf

Browse files
committed
feat: more support for draft-06
1 parent e09fed3 commit 29b5eaf

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/JsonSchema/Constraints/Drafts/Draft06/AdditionalItemsConstraint.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ class AdditionalItemsConstraint implements ConstraintInterface
1414
{
1515
use ErrorBagProxy;
1616

17+
/** @var \JsonSchema\Constraints\Drafts\Draft06\Factory */
18+
private $factory;
19+
1720
public function __construct(?Factory $factory = null)
1821
{
19-
$this->initialiseErrorBag($factory ?: new Factory());
22+
$this->factory = $factory ?: new Factory();
23+
$this->initialiseErrorBag($this->factory);
2024
}
2125

2226
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
2832
if ($schema->additionalItems === true) {
2933
return;
3034
}
35+
if ($schema->additionalItems === false && ! property_exists($schema, 'items')) {
36+
return;
37+
}
3138

3239
if (!is_array($value)) {
3340
return;
3441
}
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 : []);
3550

51+
foreach ($additionalItems as $propertyName => $propertyValue) {
52+
$schemaConstraint = $this->factory->createInstanceFor('schema');
53+
$schemaConstraint->check($propertyValue, $schema->additionalItems, $path, $i);
3654

37-
$additionalItems = array_diff_key($value, $schema->items);
55+
if ($schemaConstraint->isValid()) {
56+
continue;
57+
}
3858

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]);
4160
}
4261

4362

0 commit comments

Comments
 (0)