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 819247d commit 7c35602
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 148 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Expand Up @@ -8,9 +8,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

For a full diff see [`2.0.0...main`][2.0.0...main].

## [`3.0.0`][3.0.0]

For a full diff see [`2.0.0...3.0.0`][2.0.0...3.0.0].

### Changed

- Required [`ergebnis/json-pointer`](https://github.com/ergebnis/json-pointer) ([#195]), by [@localheinz]
- Started using `Ergebnis\Json\Pointer\JsonPointer` instead of `Ergebnis\Json\SchemaValidator\JsonPointer` ([#200]), by [@localheinz]

## [`2.0.0`][2.0.0]

Expand Down Expand Up @@ -48,10 +53,12 @@ For a full diff see [`dcd4cfb...1.0.0`][dcd4cfb...1.0.0].

[1.0.0]: https://github.com/ergebnis/json-schema-validator/releases/tag/1.0.0
[2.0.0]: https://github.com/ergebnis/json-schema-validator/releases/tag/2.0.0
[3.0.0]: https://github.com/ergebnis/json-schema-validator/releases/tag/3.0.0

[dcd4cfb...1.0.0]: https://github.com/ergebnis/json-schema-validator/compare/dcd4cfb...1.0.0
[1.0.0...2.0.0]: https://github.com/ergebnis/json-schema-validator/compare/1.0.0...2.0.0
[2.0.0...main]: https://github.com/ergebnis/json-schema-validator/compare/2.0.0...main
[2.0.0...3.0.0]: https://github.com/ergebnis/json-schema-validator/compare/2.0.0...3.0.0
[3.0.0...main]: https://github.com/ergebnis/json-schema-validator/compare/3.0.0...main

[#2]: https://github.com/ergebnis/json-schema-validator/pull/2
[#3]: https://github.com/ergebnis/json-schema-validator/pull/3
Expand All @@ -71,5 +78,6 @@ 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

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion psalm-baseline.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820">
<files psalm-version="4.19.0@a2ad69ae4f5ab1f7d225a8dc4e2ec2d9415ed599">
<file src="src/Json.php">
<UnusedFunctionCall occurrences="2">
<code>\json_decode</code>
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(),
));
}
}
6 changes: 3 additions & 3 deletions src/Exception/ResolvedToRootSchema.php
Expand Up @@ -13,15 +13,15 @@

namespace Ergebnis\Json\SchemaValidator\Exception;

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

final class ResolvedToRootSchema extends \RuntimeException
{
public static function jsonPointer(SchemaValidator\JsonPointer $jsonPointer): self
public static function jsonPointer(Pointer\JsonPointer $jsonPointer): self
{
return new self(\sprintf(
'Resolved JSON pointer "%s" to root schema.',
$jsonPointer->toString(),
$jsonPointer->toJsonString(),
));
}
}
47 changes: 0 additions & 47 deletions src/JsonPointer.php

This file was deleted.

9 changes: 5 additions & 4 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 Ergebnis\Json\SchemaValidator\Exception\ResolvedToRootSchema;
use JsonSchema\Constraints;
Expand All @@ -30,7 +31,7 @@ final class SchemaValidator
public function validate(
Json $json,
Json $schema,
JsonPointer $jsonPointer
Pointer\JsonPointer $jsonPointer
): ValidationResult {
$schemaDecoded = \json_decode(
$schema->toString(),
Expand All @@ -39,11 +40,11 @@ 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);
Expand Down Expand Up @@ -81,7 +82,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
6 changes: 3 additions & 3 deletions test/Unit/Exception/CanNotResolveTest.php
Expand Up @@ -13,8 +13,8 @@

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

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

/**
Expand All @@ -28,13 +28,13 @@ final class CanNotResolveTest extends Framework\TestCase
{
public function testJsonPointerReturnsException(): void
{
$jsonPointer = JsonPointer::fromString('/foo/bar');
$jsonPointer = Pointer\JsonPointer::fromJsonString('/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
8 changes: 3 additions & 5 deletions test/Unit/Exception/ResolvedToRootSchemaTest.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\ResolvedToRootSchema
*
* @uses \Ergebnis\Json\SchemaValidator\JsonPointer
*/
final class ResolvedToRootSchemaTest extends Framework\TestCase
{
public function testJsonPointerReturnsException(): void
{
$jsonPointer = JsonPointer::fromString('/foo/bar');
$jsonPointer = Pointer\JsonPointer::fromJsonString('/foo/bar');

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

$expected = \sprintf(
'Resolved JSON pointer "%s" to root schema.',
$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 7c35602

Please sign in to comment.