Skip to content

Commit

Permalink
Better validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Schindler committed Feb 19, 2020
1 parent 213cc69 commit afe0779
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
19 changes: 3 additions & 16 deletions ArrayAccess/ArrayAccessValidationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion Tests/ArrayAccess/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
*/
Expand Down
25 changes: 16 additions & 9 deletions Tests/ArrayAccess/ArrayAccessValidationFailedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit afe0779

Please sign in to comment.