From afe07799636bd40758ea39303452cef3ac5cc03d Mon Sep 17 00:00:00 2001 From: Markus Schindler Date: Wed, 19 Feb 2020 13:50:14 +0100 Subject: [PATCH] Better validation errors --- ArrayAccess/ArrayAccessValidationFailed.php | 19 ++--------- Tests/ArrayAccess/ArrayAccessTest.php | 34 ++++++++++++++++++- .../ArrayAccessValidationFailedTest.php | 25 +++++++++----- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/ArrayAccess/ArrayAccessValidationFailed.php b/ArrayAccess/ArrayAccessValidationFailed.php index c1f198c..6f71641 100644 --- a/ArrayAccess/ArrayAccessValidationFailed.php +++ b/ArrayAccess/ArrayAccessValidationFailed.php @@ -13,25 +13,12 @@ class ArrayAccessValidationFailed extends \RuntimeException public static function withValidationErrors(ValidationError ...$errors): self { - $messages = \array_map( - function(ValidationError $error) { - return \sprintf( - 'Error: [%s], Data pointer: [%s]', - $error->keyword(), - \implode(', ', $error->dataPointer()), - ); - }, - $errors - ); - - $instance = new self( - \sprintf('Json schema validation failed: %s', \implode(', ', $messages)) - ); - + $instance = new self('Json schema validation failed'); $instance->errors = $errors; $instance->errorMapping = ArrayAccess::create([]); + foreach ($errors as $error) { - $instance->errorMapping = $instance->errorMapping->writeAtPath((string) $error->keyword(), ...$error->dataPointer()); + $instance->errorMapping = $instance->errorMapping->writeAtPath([$error->keyword() => $error->keywordArgs()], ...$error->dataPointer()); } return $instance; diff --git a/Tests/ArrayAccess/ArrayAccessTest.php b/Tests/ArrayAccess/ArrayAccessTest.php index 277387a..3f819e9 100644 --- a/Tests/ArrayAccess/ArrayAccessTest.php +++ b/Tests/ArrayAccess/ArrayAccessTest.php @@ -50,7 +50,7 @@ public function it_validates_with_json_schema_validator(): void public function it_raises_an_exception_on_failed_json_schema_validation(): void { $this->expectException(ArrayAccessValidationFailed::class); - $this->expectExceptionMessage('Json schema validation failed: Error: [minLength], Data pointer: [key1], Error: [type], Data pointer: [key2], Error: [additionalProperties], Data pointer: []'); + $this->expectExceptionMessage('Json schema validation failed'); $data = [ 'key1' => 'v', @@ -195,6 +195,38 @@ public function it_can_write_empty_array(): void static::assertSame([], $access->array(0)); } + /** + * @test + */ + public function it_can_write_array_at_path(): void + { + $access = ArrayAccess::create([]); + $arrayToWrite = ['foo' => ['bar' => 'baz']]; + $access = $access->writeAtPath($arrayToWrite); + + static::assertSame( + [ + [ + 'foo' => [ + 'bar' => 'baz' + ], + ] + ], + $access->data() + ); + } + + /** + * @test + */ + public function it_can_write_empty_array_path(): void + { + $access = ArrayAccess::create([]); + $access = $access->writeAtPath('foo', ...[]); + + static::assertSame(['foo'], $access->data()); + } + /** * @test */ diff --git a/Tests/ArrayAccess/ArrayAccessValidationFailedTest.php b/Tests/ArrayAccess/ArrayAccessValidationFailedTest.php index 4a1174f..7fe7e32 100644 --- a/Tests/ArrayAccess/ArrayAccessValidationFailedTest.php +++ b/Tests/ArrayAccess/ArrayAccessValidationFailedTest.php @@ -4,7 +4,8 @@ namespace Mschindler83\Tests\ArrayAccess; use Mschindler83\ArrayAccess\ArrayAccessValidationFailed; -use Opis\JsonSchema\ValidationError; +use Opis\JsonSchema\Schema; +use Opis\JsonSchema\Validator; use PHPUnit\Framework\TestCase; class ArrayAccessValidationFailedTest extends TestCase @@ -14,18 +15,24 @@ class ArrayAccessValidationFailedTest extends TestCase */ public function it_can_return_errors(): void { - $error1 = new ValidationError(null, ['e1dp1', 'e1dp2'], [], false, 'kw1'); - $error2 = new ValidationError(null, ['e2dp1', 'e2dp2'], [], false, 'kw2'); - - $exception = ArrayAccessValidationFailed::withValidationErrors($error1, $error2); + $schema = Schema::fromJsonString(\file_get_contents(__DIR__ . '/../Fixture/json-schema.json')); + $result = (new Validator())->schemaValidation(\json_decode(\json_encode(['key1' => 'a'])), $schema, 10); + $errors = $result->getErrors(); + $exception = ArrayAccessValidationFailed::withValidationErrors(...$errors); + static::assertSame('Json schema validation failed', $exception->getMessage()); static::assertSame( [ - 'e1dp1' => [ - 'e1dp2' => 'kw1' + [ + 'required' => [ + 'missing' => 'key2' + ], ], - 'e2dp1' => [ - 'e2dp2' => 'kw2' + 'key1' => [ + 'minLength' => [ + 'min' => 3, + 'length' => 1, + ], ], ], $exception->errorMapping()->data()