-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Move JSON schema validation to dedicated service class
- Loading branch information
1 parent
8589ad5
commit c6d3533
Showing
8 changed files
with
192 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Composer package "cpsit/project-builder". | ||
* | ||
* Copyright (C) 2023 Elias Häußler <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace CPSIT\ProjectBuilder\Json; | ||
|
||
use CPSIT\ProjectBuilder\Helper; | ||
use Opis\JsonSchema; | ||
|
||
/** | ||
* SchemaValidator. | ||
* | ||
* @author Elias Häußler <[email protected]> | ||
* @license GPL-3.0-or-later | ||
*/ | ||
final class SchemaValidator | ||
{ | ||
public function __construct( | ||
private readonly JsonSchema\Validator $validator, | ||
) { | ||
} | ||
|
||
public function validate(mixed $data, string $schemaFile): JsonSchema\ValidationResult | ||
{ | ||
$schemaFile = Helper\FilesystemHelper::resolveRelativePath($schemaFile); | ||
$schemaReference = 'file://'.$schemaFile; | ||
$schemaResolver = $this->validator->resolver(); | ||
|
||
// @codeCoverageIgnoreStart | ||
if (null === $schemaResolver) { | ||
$schemaResolver = new JsonSchema\Resolvers\SchemaResolver(); | ||
$this->validator->setResolver($schemaResolver); | ||
} | ||
// @codeCoverageIgnoreEnd | ||
|
||
$schemaResolver->registerFile($schemaReference, $schemaFile); | ||
|
||
return $this->validator->validate($data, $schemaReference); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2019-09/schema#", | ||
"type": "object", | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"foo" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Composer package "cpsit/project-builder". | ||
* | ||
* Copyright (C) 2023 Elias Häußler <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace CPSIT\ProjectBuilder\Tests\Json; | ||
|
||
use CPSIT\ProjectBuilder as Src; | ||
use CPSIT\ProjectBuilder\Tests; | ||
use Generator; | ||
|
||
use function dirname; | ||
|
||
/** | ||
* SchemaValidatorTest. | ||
* | ||
* @author Elias Häußler <[email protected]> | ||
* @license GPL-3.0-or-later | ||
*/ | ||
final class SchemaValidatorTest extends Tests\ContainerAwareTestCase | ||
{ | ||
private Src\Json\SchemaValidator $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = self::$container->get(Src\Json\SchemaValidator::class); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @dataProvider validateValidatesJsonDataProvider | ||
*/ | ||
public function validateValidatesJson(mixed $data, bool $expected): void | ||
{ | ||
$schemaFile = dirname(__DIR__).'/Fixtures/Files/test.schema.json'; | ||
|
||
self::assertSame($expected, $this->subject->validate($data, $schemaFile)->isValid()); | ||
} | ||
|
||
/** | ||
* @return Generator<string, array{mixed, bool}> | ||
*/ | ||
public function validateValidatesJsonDataProvider(): Generator | ||
{ | ||
yield 'valid json' => [(object) ['foo' => 'baz'], true]; | ||
yield 'invalid json' => [null, false]; | ||
} | ||
} |