generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement SchemaValidator
- Loading branch information
1 parent
1e03cf1
commit 96cb048
Showing
5 changed files
with
337 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-schema-validator | ||
*/ | ||
|
||
namespace Ergebnis\Json\SchemaValidator; | ||
|
||
use JsonSchema\Validator; | ||
use function Safe\sprintf; | ||
|
||
final class SchemaValidator | ||
{ | ||
private $decoder; | ||
|
||
private $validator; | ||
|
||
public function __construct(Decoder $decoder, Validator $validator) | ||
{ | ||
$this->decoder = $decoder; | ||
$this->validator = $validator; | ||
} | ||
|
||
public function validate(Json $json, Schema $schema): Result | ||
{ | ||
$this->validator->reset(); | ||
|
||
$this->validator->check( | ||
$this->decoder->decodeNonAssociative($json->encoded()), | ||
$schema->decoded() | ||
); | ||
|
||
/** @var array<int, array> $originalErrors */ | ||
$originalErrors = $this->validator->getErrors(); | ||
|
||
$errors = \array_map(static function (array $error): string { | ||
$property = ''; | ||
|
||
if ( | ||
\array_key_exists('property', $error) | ||
&& \is_string($error['property']) | ||
&& '' !== \trim($error['property']) | ||
) { | ||
$property = \trim($error['property']); | ||
} | ||
|
||
$message = ''; | ||
|
||
if ( | ||
\array_key_exists('message', $error) | ||
&& \is_string($error['message']) | ||
&& '' !== \trim($error['message']) | ||
) { | ||
$message = \trim($error['message']); | ||
} | ||
|
||
if ('' === $property) { | ||
return $message; | ||
} | ||
|
||
return sprintf( | ||
'%s: %s', | ||
$property, | ||
$message | ||
); | ||
}, $originalErrors); | ||
|
||
$filtered = \array_filter($errors, static function (string $error): bool { | ||
return '' !== $error; | ||
}); | ||
|
||
return Result::create(...$filtered); | ||
} | ||
} |
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,221 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2021 Andreas Möller | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE.md file that was distributed with this source code. | ||
* | ||
* @see https://github.com/ergebnis/json-schema-validator | ||
*/ | ||
|
||
namespace Ergebnis\Json\SchemaValidator\Test\Unit; | ||
|
||
use Ergebnis\Json\SchemaValidator\Decoder; | ||
use Ergebnis\Json\SchemaValidator\Json; | ||
use Ergebnis\Json\SchemaValidator\Schema; | ||
use Ergebnis\Json\SchemaValidator\SchemaValidator; | ||
use Ergebnis\Test\Util; | ||
use JsonSchema\Validator; | ||
use PHPUnit\Framework; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \Ergebnis\Json\SchemaValidator\SchemaValidator | ||
* | ||
* @uses \Ergebnis\Json\SchemaValidator\Decoder | ||
* @uses \Ergebnis\Json\SchemaValidator\Json | ||
* @uses \Ergebnis\Json\SchemaValidator\Result | ||
* @uses \Ergebnis\Json\SchemaValidator\Schema | ||
*/ | ||
final class SchemaValidatorTest extends Framework\TestCase | ||
{ | ||
use Util\Helper; | ||
|
||
public function testValidateReturnsResultWhenDataIsNotValidAccordingToSchema(): void | ||
{ | ||
$json = Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"number": 1600, | ||
"street_name": "Pennsylvania", | ||
"street_type": "Avenue", | ||
"direction": "NW" | ||
} | ||
JSON | ||
); | ||
|
||
$schema = Schema::fromJson(Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"email": { | ||
"type": "string" | ||
}, | ||
"address": { | ||
"type": "string" | ||
}, | ||
"telephone": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"email" | ||
], | ||
"additionalProperties": false | ||
} | ||
JSON | ||
)); | ||
|
||
$validator = new SchemaValidator( | ||
new Decoder(), | ||
new Validator() | ||
); | ||
|
||
$result = $validator->validate( | ||
$json, | ||
$schema | ||
); | ||
|
||
self::assertFalse($result->isValid()); | ||
|
||
$expected = [ | ||
'name: The property name is required', | ||
'email: The property email is required', | ||
'The property number is not defined and the definition does not allow additional properties', | ||
'The property street_name is not defined and the definition does not allow additional properties', | ||
'The property street_type is not defined and the definition does not allow additional properties', | ||
'The property direction is not defined and the definition does not allow additional properties', | ||
]; | ||
|
||
self::assertSame($expected, $result->errors()); | ||
} | ||
|
||
public function testValidateReturnsResultWhenDataIsValidAccordingToSchema(): void | ||
{ | ||
$json = Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"name": "Jane Doe", | ||
"email": "[email protected]" | ||
} | ||
JSON | ||
); | ||
|
||
$schema = Schema::fromJson(Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"email": { | ||
"type": "string" | ||
}, | ||
"address": { | ||
"type": "string" | ||
}, | ||
"telephone": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"email" | ||
], | ||
"additionalProperties": false | ||
} | ||
JSON | ||
)); | ||
|
||
$validator = new SchemaValidator( | ||
new Decoder(), | ||
new Validator() | ||
); | ||
|
||
$result = $validator->validate( | ||
$json, | ||
$schema | ||
); | ||
|
||
self::assertTrue($result->isValid()); | ||
self::assertSame([], $result->errors()); | ||
} | ||
|
||
public function testValidateClearsStateOfInternalValidator(): void | ||
{ | ||
$invalidJson = Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"number": 1600, | ||
"street_name": "Pennsylvania", | ||
"street_type": "Avenue", | ||
"direction": "NW" | ||
} | ||
JSON | ||
); | ||
|
||
$validJson = Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"name": "Jane Doe", | ||
"email": "[email protected]" | ||
} | ||
JSON | ||
); | ||
|
||
$schema = Schema::fromJson(Json::fromString( | ||
<<<'JSON' | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"email": { | ||
"type": "string" | ||
}, | ||
"address": { | ||
"type": "string" | ||
}, | ||
"telephone": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"email" | ||
], | ||
"additionalProperties": false | ||
} | ||
JSON | ||
)); | ||
|
||
$validator = new SchemaValidator( | ||
new Decoder(), | ||
new Validator() | ||
); | ||
|
||
$validator->validate( | ||
$invalidJson, | ||
$schema | ||
); | ||
|
||
$secondResult = $validator->validate( | ||
$validJson, | ||
$schema | ||
); | ||
|
||
self::assertTrue($secondResult->isValid()); | ||
self::assertSame([], $secondResult->errors()); | ||
} | ||
} |