Skip to content

Commit de9023c

Browse files
committed
feat: Add dependentSchemas support
1 parent 115a09e commit de9023c

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JsonSchema\Constraints\Drafts\Draft2019;
6+
7+
use JsonSchema\ConstraintError;
8+
use JsonSchema\Constraints\ConstraintInterface;
9+
use JsonSchema\Entity\ErrorBagProxy;
10+
use JsonSchema\Entity\JsonPointer;
11+
12+
class DependentSchemasConstraint implements ConstraintInterface
13+
{
14+
use ErrorBagProxy;
15+
16+
/** @var Factory */
17+
private $factory;
18+
19+
public function __construct(?Factory $factory = null)
20+
{
21+
$this->factory = $factory ?: new Factory();
22+
$this->initialiseErrorBag($this->factory);
23+
}
24+
25+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
26+
{
27+
if (!property_exists($schema, 'dependentSchemas')) {
28+
return;
29+
}
30+
31+
if (!is_object($value)) {
32+
return;
33+
}
34+
35+
foreach ($schema->dependentSchemas as $dependant => $dependentSchema) {
36+
if (!property_exists($value, $dependant)) {
37+
continue;
38+
}
39+
40+
if ($dependentSchema === true) {
41+
continue;
42+
}
43+
44+
if ($dependentSchema === false) {
45+
$this->addError(ConstraintError::FALSE(), $path, ['dependant' => $dependant]);
46+
continue;
47+
}
48+
49+
if (is_object($dependentSchema)) {
50+
$schemaConstraint = $this->factory->createInstanceFor('schema');
51+
$schemaConstraint->check($value, $dependentSchema, $path, $i);
52+
if (!$schemaConstraint->isValid()) {
53+
$this->addErrors($schemaConstraint->getErrors());
54+
}
55+
}
56+
}
57+
}
58+
}

src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
4141
$this->checkForKeyword('type', $value, $schema, $path, $i);
4242
$this->checkForKeyword('not', $value, $schema, $path, $i);
4343
$this->checkForKeyword('dependencies', $value, $schema, $path, $i);
44+
$this->checkForKeyword('dependentSchemas', $value, $schema, $path, $i);
4445
$this->checkForKeyword('allOf', $value, $schema, $path, $i);
4546
$this->checkForKeyword('anyOf', $value, $schema, $path, $i);
4647
$this->checkForKeyword('oneOf', $value, $schema, $path, $i);

src/JsonSchema/Constraints/Drafts/Draft2019/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Factory extends \JsonSchema\Constraints\Factory
1414
'additionalProperties' => AdditionalPropertiesConstraint::class,
1515
'additionalItems' => AdditionalItemsConstraint::class,
1616
'dependencies' => DependenciesConstraint::class,
17+
'dependentSchemas' => DependentSchemasConstraint::class,
1718
'type' => TypeConstraint::class,
1819
'const' => ConstConstraint::class,
1920
'enum' => EnumConstraint::class,

0 commit comments

Comments
 (0)