-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Retrieving validation errors via validation exception
- Extended tests - fixed writeToPath not beeing immutable
- Loading branch information
Markus Schindler
committed
Feb 19, 2020
1 parent
971fda0
commit d353820
Showing
5 changed files
with
158 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mschindler83\ArrayAccess; | ||
|
||
use Opis\JsonSchema\ValidationError; | ||
|
||
class ArrayAccessValidationFailed extends \RuntimeException | ||
{ | ||
private array $errors; | ||
|
||
private ArrayAccess $errorMapping; | ||
|
||
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->errors = $errors; | ||
$instance->errorMapping = ArrayAccess::create([]); | ||
foreach ($errors as $error) { | ||
$instance->errorMapping = $instance->errorMapping->writeAtPath((string) $error->keyword(), ...$error->dataPointer()); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
public function errorMapping(): ArrayAccess | ||
{ | ||
return $this->errorMapping; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Mschindler83\Tests\ArrayAccess; | ||
|
||
use Mschindler83\ArrayAccess\ArrayAccessValidationFailed; | ||
use Opis\JsonSchema\ValidationError; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ArrayAccessValidationFailedTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
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); | ||
|
||
static::assertSame( | ||
[ | ||
'e1dp1' => [ | ||
'e1dp2' => 'kw1' | ||
], | ||
'e2dp1' => [ | ||
'e2dp2' => 'kw2' | ||
], | ||
], | ||
$exception->errorMapping()->data() | ||
); | ||
} | ||
} |