Skip to content

Commit 605fbec

Browse files
committed
fix: resolve dependencies keyword for oject with subschema
1 parent 551a190 commit 605fbec

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66

77
use JsonSchema\ConstraintError;
88
use JsonSchema\Constraints\ConstraintInterface;
9-
use JsonSchema\Constraints\Factory;
109
use JsonSchema\Entity\ErrorBagProxy;
1110
use JsonSchema\Entity\JsonPointer;
1211

1312
class DependenciesConstraint implements ConstraintInterface
1413
{
1514
use ErrorBagProxy;
1615

16+
/** @var Factory */
17+
private $factory;
18+
1719
public function __construct(?Factory $factory = null)
1820
{
19-
$this->initialiseErrorBag($factory ?: new Factory());
21+
$this->factory = $factory ?: new Factory();
22+
$this->initialiseErrorBag($this->factory);
2023
}
2124

2225
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
@@ -40,9 +43,20 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
4043
$this->addError(ConstraintError::FALSE(), $path, ['dependant' => $dependant]);
4144
continue;
4245
}
43-
foreach ($dependencies as $dependency) {
44-
if (property_exists($value, $dependant) && !property_exists($value, $dependency)) {
45-
$this->addError(ConstraintError::DEPENDENCIES(), $path, ['dependant' => $dependant, 'dependency' => $dependency]);
46+
47+
if (is_array($dependencies)) {
48+
foreach ($dependencies as $dependency) {
49+
if (property_exists($value, $dependant) && !property_exists($value, $dependency)) {
50+
$this->addError(ConstraintError::DEPENDENCIES(), $path, ['dependant' => $dependant, 'dependency' => $dependency]);
51+
}
52+
}
53+
}
54+
55+
if (is_object($dependencies)) {
56+
$schemaConstraint = $this->factory->createInstanceFor('schema');
57+
$schemaConstraint->check($value, $dependencies, $path, $i);
58+
if (!$schemaConstraint->isValid()) {
59+
$this->addErrors($schemaConstraint->getErrors());
4660
}
4761
}
4862
}

0 commit comments

Comments
 (0)