Skip to content

Commit

Permalink
Enhancement: Use JsonPointer as provided by ergebnis/json-pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 30, 2022
1 parent 3a364fc commit f5ee9e4
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 196 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ For a full diff see [`2.0.0...3.0.0`][2.0.0...3.0.0].

- Required [`ergebnis/json-pointer`](https://github.com/ergebnis/json-pointer) ([#195]), by [@localheinz]
- Started throwing an `Exception\CanNotResolve` exception instead of an `Exception\ResolvedToRootSchema` when the `JsonPointer` is not a valid URI fragment identifier representation of a JSON pointer ([#202]), by [@localheinz]
- Started using `Ergebnis\Json\Pointer\JsonPointer` instead of `Ergebnis\Json\SchemaValidator\JsonPointer` ([#200]), by [@localheinz]

### Removed

Expand Down Expand Up @@ -82,6 +83,7 @@ For a full diff see [`dcd4cfb...1.0.0`][dcd4cfb...1.0.0].
[#169]: https://github.com/ergebnis/json-schema-validator/pull/169
[#172]: https://github.com/ergebnis/json-schema-validator/pull/172
[#195]: https://github.com/ergebnis/json-schema-validator/pull/195
[#200]: https://github.com/ergebnis/json-schema-validator/pull/200
[#202]: https://github.com/ergebnis/json-schema-validator/pull/202
[#203]: https://github.com/ergebnis/json-schema-validator/pull/203

Expand Down
4 changes: 2 additions & 2 deletions infection.json
Expand Up @@ -3,8 +3,8 @@
"logs": {
"text": ".build/infection/infection-log.txt"
},
"minCoveredMsi": 90,
"minMsi": 88,
"minCoveredMsi": 88,
"minMsi": 86,
"phpUnit": {
"configDir": "test\/Unit"
},
Expand Down
6 changes: 3 additions & 3 deletions src/Exception/CanNotResolve.php
Expand Up @@ -13,15 +13,15 @@

namespace Ergebnis\Json\SchemaValidator\Exception;

use Ergebnis\Json\SchemaValidator;
use Ergebnis\Json\Pointer;

final class CanNotResolve extends \InvalidArgumentException
{
public static function jsonPointer(SchemaValidator\JsonPointer $jsonPointer): self
public static function jsonPointer(Pointer\JsonPointer $jsonPointer): self
{
return new self(\sprintf(
'Can not resolve JSON pointer "%s".',
$jsonPointer->toString(),
$jsonPointer->toJsonString(),
));
}
}
47 changes: 0 additions & 47 deletions src/JsonPointer.php

This file was deleted.

13 changes: 5 additions & 8 deletions src/SchemaValidator.php
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\Json\SchemaValidator;

use Ergebnis\Json\Pointer;
use Ergebnis\Json\SchemaValidator\Exception\CanNotResolve;
use JsonSchema\Constraints;
use JsonSchema\Exception;
Expand All @@ -28,7 +29,7 @@ final class SchemaValidator
public function validate(
Json $json,
Json $schema,
JsonPointer $jsonPointer
Pointer\JsonPointer $jsonPointer
): ValidationResult {
$schemaDecoded = \json_decode(
$schema->toString(),
Expand All @@ -37,20 +38,16 @@ public function validate(

$uriRetriever = new Uri\UriRetriever();

if (!$jsonPointer->equals(JsonPointer::empty())) {
if (!$jsonPointer->equals(Pointer\JsonPointer::document())) {
try {
$subSchemaDecoded = $uriRetriever->resolvePointer(
$schemaDecoded,
$jsonPointer->toString(),
$jsonPointer->toUriFragmentIdentifierString(),
);
} catch (Exception\ResourceNotFoundException $exception) {
throw CanNotResolve::jsonPointer($jsonPointer);
}

if ($schemaDecoded === $subSchemaDecoded) {
throw CanNotResolve::jsonPointer($jsonPointer);
}

$schemaDecoded = $subSchemaDecoded;
}

Expand Down Expand Up @@ -79,7 +76,7 @@ public function validate(

$validationErrors = \array_map(static function (array $error): ValidationError {
return ValidationError::create(
JsonPointer::fromString($error['pointer']),
Pointer\JsonPointer::fromJsonString($error['pointer']),
Message::fromString($error['message']),
);
}, $originalErrors);
Expand Down
10 changes: 6 additions & 4 deletions src/ValidationError.php
Expand Up @@ -13,24 +13,26 @@

namespace Ergebnis\Json\SchemaValidator;

use Ergebnis\Json\Pointer;

/**
* @psalm-immutable
*/
final class ValidationError
{
private JsonPointer $jsonPointer;
private Pointer\JsonPointer $jsonPointer;
private Message $message;

private function __construct(
JsonPointer $jsonPointer,
Pointer\JsonPointer $jsonPointer,
Message $message
) {
$this->jsonPointer = $jsonPointer;
$this->message = $message;
}

public static function create(
JsonPointer $jsonPointer,
Pointer\JsonPointer $jsonPointer,
Message $message
): self {
return new self(
Expand All @@ -39,7 +41,7 @@ public static function create(
);
}

public function jsonPointer(): JsonPointer
public function jsonPointer(): Pointer\JsonPointer
{
return $this->jsonPointer;
}
Expand Down
8 changes: 3 additions & 5 deletions test/Unit/Exception/CanNotResolveTest.php
Expand Up @@ -13,28 +13,26 @@

namespace Ergebnis\Json\SchemaValidator\Test\Unit\Exception;

use Ergebnis\Json\Pointer;
use Ergebnis\Json\SchemaValidator\Exception;
use Ergebnis\Json\SchemaValidator\JsonPointer;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\Json\SchemaValidator\Exception\CanNotResolve
*
* @uses \Ergebnis\Json\SchemaValidator\JsonPointer
*/
final class CanNotResolveTest extends Framework\TestCase
{
public function testJsonPointerReturnsException(): void
{
$jsonPointer = JsonPointer::fromString('#/foo/bar');
$jsonPointer = Pointer\JsonPointer::fromUriFragmentIdentifierString('#/foo/bar');

$exception = Exception\CanNotResolve::jsonPointer($jsonPointer);

$expected = \sprintf(
'Can not resolve JSON pointer "%s".',
$jsonPointer->toString(),
$jsonPointer->toJsonString(),
);

self::assertSame($expected, $exception->getMessage());
Expand Down
59 changes: 0 additions & 59 deletions test/Unit/JsonPointerTest.php

This file was deleted.

0 comments on commit f5ee9e4

Please sign in to comment.