Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,26 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('pattern', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
];

/**
* @param mixed $value
* @param mixed $schema
* @param mixed $i
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);
foreach (self::KEYWORD_SCHEMA_PROPERTIES[$keyword] ?? [$keyword] as $property) {
if (property_exists($schema, $property)) {
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());

$this->addErrors($validator->getErrors());
return;
}
}
}
}
18 changes: 15 additions & 3 deletions src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,28 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('content', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

/**
* @param mixed $value
* @param mixed $schema
* @param mixed $i
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);
foreach (self::KEYWORD_SCHEMA_PROPERTIES[$keyword] ?? [$keyword] as $property) {
if (property_exists($schema, $property)) {
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());

$this->addErrors($validator->getErrors());
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,28 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
$this->checkForKeyword('content', $value, $schema, $path, $i);
}

private const KEYWORD_SCHEMA_PROPERTIES = [
'ref' => ['$ref'],
'ifThenElse' => ['if'],
'content' => ['contentMediaType', 'contentEncoding'],
];

/**
* @param mixed $value
* @param mixed $schema
* @param mixed $i
*/
protected function checkForKeyword(string $keyword, $value, $schema = null, ?JsonPointer $path = null, $i = null): void
{
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);
foreach (self::KEYWORD_SCHEMA_PROPERTIES[$keyword] ?? [$keyword] as $property) {
if (property_exists($schema, $property)) {
$validator = $this->factory->createInstanceFor($keyword);
$validator->check($value, $schema, $path, $i);

$this->addErrors($validator->getErrors());

$this->addErrors($validator->getErrors());
return;
}
}
}
}
79 changes: 79 additions & 0 deletions tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Drafts\Draft06;

use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Constraints\Drafts\Draft06\Draft06Constraint;
use JsonSchema\Constraints\Factory;
use PHPUnit\Framework\TestCase;

class Draft06ConstraintTest extends TestCase
{
public function testNoConstraintsAreInstantiatedForAnEmptySchema(): void
{
$constraint = new Draft06Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{}');
$value = json_decode('{}');
$constraint->check($value, $schema);

$this->assertSame([], $calledKeywords);
}

public function testOnlyKeywordsPresentInSchemaAreInstantiated(): void
{
$constraint = new Draft06Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"type": "object", "required": ["id"]}');
$value = json_decode('{"id": 1}');
$constraint->check($value, $schema);

sort($calledKeywords);
$this->assertSame(['required', 'type'], $calledKeywords);
}

public function testRefKeywordIsInstantiatedWhenDollarRefPropertyIsPresent(): void
{
$constraint = new Draft06Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"$ref": "#/definitions/foo"}');
$value = json_decode('{}');
$constraint->check($value, $schema);

$this->assertSame(['ref'], $calledKeywords);
}

private function createSpyFactory(array &$calledKeywords): Factory
{
$constraintStub = $this->createMock(ConstraintInterface::class);
$constraintStub->method('getErrors')->willReturn([]);

$factory = $this->createMock(Factory::class);
$factory->method('createInstanceFor')
->willReturnCallback(static function (string $keyword) use (&$calledKeywords, $constraintStub) {
$calledKeywords[] = $keyword;

return $constraintStub;
});

return $factory;
}

private function injectFactory(Draft06Constraint $constraint, Factory $factory): void
{
$property = new \ReflectionProperty(BaseConstraint::class, 'factory');
if (PHP_VERSION_ID < 80100) {
$property->setAccessible(true);
}
$property->setValue($constraint, $factory);
}
}
114 changes: 114 additions & 0 deletions tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Drafts\Draft07;

use JsonSchema\Constraints\BaseConstraint;
use JsonSchema\Constraints\ConstraintInterface;
use JsonSchema\Constraints\Drafts\Draft07\Draft07Constraint;
use JsonSchema\Constraints\Factory;
use PHPUnit\Framework\TestCase;

class Draft07ConstraintTest extends TestCase
{
public function testNoConstraintsAreInstantiatedForAnEmptySchema(): void
{
$constraint = new Draft07Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{}');
$value = json_decode('{}');
$constraint->check($value, $schema);

$this->assertSame([], $calledKeywords);
}

public function testOnlyKeywordsPresentInSchemaAreInstantiated(): void
{
$constraint = new Draft07Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"type": "object", "required": ["id"]}');
$value = json_decode('{"id": 1}');
$constraint->check($value, $schema);

sort($calledKeywords);
$this->assertSame(['required', 'type'], $calledKeywords);
}

public function testRefKeywordIsInstantiatedWhenDollarRefPropertyIsPresent(): void
{
$constraint = new Draft07Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"$ref": "#/definitions/foo"}');
$value = json_decode('{}');
$constraint->check($value, $schema);

$this->assertSame(['ref'], $calledKeywords);
}

public function testIfThenElseKeywordIsInstantiatedWhenIfPropertyIsPresent(): void
{
$constraint = new Draft07Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode('{"if": {"type": "string"}}');
$value = json_decode('"hello"');
$constraint->check($value, $schema);

$this->assertSame(['ifThenElse'], $calledKeywords);
}

/**
* @dataProvider contentPropertyProvider
*/
public function testContentKeywordIsInstantiatedWhenEitherContentPropertyIsPresent(string $property): void
{
$constraint = new Draft07Constraint();
$calledKeywords = [];
$this->injectFactory($constraint, $this->createSpyFactory($calledKeywords));

$schema = json_decode(sprintf('{"%s": "text/plain"}', $property));
$value = json_decode('"hello"');
$constraint->check($value, $schema);

$this->assertSame(['content'], $calledKeywords);
}

public static function contentPropertyProvider(): \Generator
{
yield 'contentMediaType' => ['contentMediaType'];
yield 'contentEncoding' => ['contentEncoding'];
}

private function createSpyFactory(array &$calledKeywords): Factory
{
$constraintStub = $this->createMock(ConstraintInterface::class);
$constraintStub->method('getErrors')->willReturn([]);

$factory = $this->createMock(Factory::class);
$factory->method('createInstanceFor')
->willReturnCallback(static function (string $keyword) use (&$calledKeywords, $constraintStub) {
$calledKeywords[] = $keyword;

return $constraintStub;
});

return $factory;
}

private function injectFactory(Draft07Constraint $constraint, Factory $factory): void
{
$property = new \ReflectionProperty(BaseConstraint::class, 'factory');
if (PHP_VERSION_ID < 80100) {
$property->setAccessible(true);
}
$property->setValue($constraint, $factory);
}
}
Loading