Skip to content

Commit a125d6a

Browse files
author
Alexander Miertsch
authored
Merge pull request #1 from event-engine/feature/improve-error-message
Add JsonValidationError exception and improve JSON schema error message
2 parents d13dd5f + db80d19 commit a125d6a

8 files changed

+454
-78
lines changed

composer.json

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,62 @@
11
{
2-
"name": "event-engine/php-json-schema",
3-
"description": "Event Engine JSON Schema PHP Package",
4-
"homepage": "https://event-engine.io/",
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Alexander Miertsch",
9-
"email": "[email protected]",
10-
"homepage": "http://www.prooph.de"
2+
"name": "event-engine/php-json-schema",
3+
"description": "Event Engine JSON Schema PHP Package",
4+
"homepage": "https://event-engine.io/",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Alexander Miertsch",
9+
"email": "[email protected]",
10+
"homepage": "http://www.prooph.de"
11+
},
12+
{
13+
"name": "Sandro Keil",
14+
"email": "[email protected]",
15+
"homepage": "http://prooph-software.com/"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2",
20+
"event-engine/php-data": "^0.1",
21+
"event-engine/php-engine-utils": "^0.1",
22+
"event-engine/php-schema": "^0.1",
23+
"ramsey/uuid": "^3.6"
1124
},
12-
{
13-
"name": "Sandro Keil",
14-
"email": "[email protected]",
15-
"homepage": "http://prooph-software.com/"
16-
}
17-
],
18-
"require": {
19-
"php": "^7.2",
20-
"roave/security-advisories": "dev-master",
21-
"event-engine/php-schema": "^0.1",
22-
"event-engine/php-data": "^0.1",
23-
"event-engine/php-engine-utils": "^0.1",
24-
"ramsey/uuid" : "^3.6"
25-
},
26-
"require-dev": {
27-
"phpunit/phpunit": "^7.0",
28-
"justinrainbow/json-schema": "^5.2",
29-
"opis/json-schema": "^1.0",
30-
"prooph/php-cs-fixer-config": "^0.3",
31-
"satooshi/php-coveralls": "^1.0",
32-
"malukenho/docheader": "^0.1.4"
33-
},
34-
"autoload": {
35-
"psr-4": {
36-
"EventEngine\\JsonSchema\\": "src/"
37-
}
38-
},
39-
"autoload-dev": {
40-
"psr-4": {
41-
"EventEngineTest\\JsonSchema\\": "tests/"
25+
"require-dev": {
26+
"ext-json": "*",
27+
"justinrainbow/json-schema": "^5.2",
28+
"malukenho/docheader": "^0.1.4",
29+
"opis/json-schema": "^1.0",
30+
"phpunit/phpunit": "^7.0",
31+
"prooph/php-cs-fixer-config": "^0.3",
32+
"roave/security-advisories": "dev-master",
33+
"satooshi/php-coveralls": "^1.0"
34+
},
35+
"autoload": {
36+
"psr-4": {
37+
"EventEngine\\JsonSchema\\": "src/"
38+
}
39+
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"EventEngineTest\\JsonSchema\\": "tests/"
43+
}
44+
},
45+
"prefer-stable": true,
46+
"config": {
47+
"sort-packages": true,
48+
"platform": {
49+
}
50+
},
51+
"scripts": {
52+
"check": [
53+
"@cs",
54+
"@docheader",
55+
"@test"
56+
],
57+
"docheader": "vendor/bin/docheader check examples/ src/ tests/",
58+
"cs": "php-cs-fixer fix -v --diff --dry-run",
59+
"cs-fix": "php-cs-fixer fix -v --diff",
60+
"test": "vendor/bin/phpunit"
4261
}
43-
},
44-
"prefer-stable": true,
45-
"scripts": {
46-
"check": [
47-
"@cs",
48-
"@docheader",
49-
"@test"
50-
],
51-
"docheader": "vendor/bin/docheader check examples/ src/ tests/",
52-
"cs": "php-cs-fixer fix -v --diff --dry-run",
53-
"cs-fix": "php-cs-fixer fix -v --diff",
54-
"test": "vendor/bin/phpunit"
55-
}
5662
}

src/Exception/JsonValidationError.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* This file is part of event-engine/php-json-schema.
4+
* (c) 2018-2019 prooph software GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace EventEngine\JsonSchema\Exception;
13+
14+
15+
class JsonValidationError extends InvalidArgumentException
16+
{
17+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* This file is part of event-engine/php-json-schema.
4+
* (c) 2018-2019 prooph software GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace EventEngine\JsonSchema\Exception;
13+
14+
15+
class JustinRainbowJsonValidationError extends JsonValidationError
16+
{
17+
/**
18+
* @var array
19+
*/
20+
private $errors;
21+
22+
public static function withError(string $objectName, array ...$errors): JustinRainbowJsonValidationError
23+
{
24+
$self = new self('Validation of "' . $objectName . '" failed: ');
25+
$self->errors = $errors;
26+
27+
$self->message .= \array_reduce(
28+
$errors,
29+
static function ($message, array $error) use ($self) {
30+
return $message . "\n" . $self->errorMessage($error);
31+
}
32+
);
33+
34+
return $self;
35+
}
36+
37+
public function errors(): array
38+
{
39+
return $this->errors;
40+
}
41+
42+
private function errorMessage(array $error): string
43+
{
44+
$dataPointer = $error['pointer'];
45+
46+
if ($dataPointer === '') {
47+
return \sprintf('[%s] %s', $error['constraint'], $error['message']);
48+
}
49+
50+
return \sprintf('field "%s" [%s] %s',
51+
$error['property'],
52+
$error['constraint'],
53+
$error['message']
54+
);
55+
56+
// return sprintf('field "%s" [%s] %s', $error['constraint'], $error['property'], $error['message']);
57+
}
58+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* This file is part of event-engine/php-json-schema.
4+
* (c) 2018-2019 prooph software GmbH <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace EventEngine\JsonSchema\Exception;
13+
14+
15+
use Opis\JsonSchema\ValidationError;
16+
17+
class OpisJsonValidationError extends JsonValidationError
18+
{
19+
/**
20+
* @var ValidationError[]
21+
*/
22+
private $errors;
23+
24+
public static function withError(string $objectName, ValidationError ...$validationErrors): OpisJsonValidationError
25+
{
26+
$self = new self('Validation of "' . $objectName . '" failed: ');
27+
$self->errors = $validationErrors;
28+
29+
foreach ($validationErrors as $error) {
30+
$self->message .= $self->errorMessage($error);
31+
32+
if ($error->subErrorsCount()) {
33+
$self->message .= \array_reduce(
34+
$error->subErrors(),
35+
static function ($message, ValidationError $error) use ($self) {
36+
return $message . "\n" . $self->errorMessage($error);
37+
}
38+
);
39+
}
40+
}
41+
42+
return $self;
43+
}
44+
45+
/**
46+
* @return ValidationError[]
47+
*/
48+
public function errors(): array
49+
{
50+
return $this->errors;
51+
}
52+
53+
private function errorMessage(ValidationError $error): string
54+
{
55+
$dataPointer = $error->dataPointer();
56+
57+
if (count($dataPointer) === 0) {
58+
return \sprintf('[%s] %s', $error->keyword(), \json_encode($error->keywordArgs(), JSON_PRETTY_PRINT));
59+
}
60+
61+
return \sprintf('field "%s" [%s] %s',
62+
implode('.', $dataPointer),
63+
$error->keyword(),
64+
\json_encode($error->keywordArgs(), JSON_PRETTY_PRINT)
65+
);
66+
}
67+
}

src/JustinRainbowJsonSchema.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace EventEngine\JsonSchema;
1313

14-
use EventEngine\JsonSchema\Exception\InvalidArgumentException;
14+
use EventEngine\JsonSchema\Exception\JustinRainbowJsonValidationError;
1515
use JsonSchema\Validator;
1616

1717
final class JustinRainbowJsonSchema extends AbstractJsonSchema
@@ -31,16 +31,8 @@ public function assert(string $objectName, array $data, array $jsonSchema)
3131

3232
if (! $this->jsonValidator()->isValid()) {
3333
$errors = $this->jsonValidator()->getErrors();
34-
3534
$this->jsonValidator()->reset();
36-
37-
foreach ($errors as $i => $error) {
38-
$errors[$i] = \sprintf("[%s] %s\n", $error['property'], $error['message']);
39-
}
40-
41-
throw new InvalidArgumentException(
42-
"Validation of $objectName failed: " . \implode("\n", $errors)
43-
);
35+
throw JustinRainbowJsonValidationError::withError($objectName, ...$errors);
4436
}
4537

4638
$this->jsonValidator()->reset();

src/OpisJsonSchema.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace EventEngine\JsonSchema;
1313

14-
use EventEngine\JsonSchema\Exception\InvalidArgumentException;
14+
use EventEngine\JsonSchema\Exception\OpisJsonValidationError;
1515
use Opis\JsonSchema\Schema as OpisSchema;
1616
use Opis\JsonSchema\Validator;
1717

@@ -38,21 +38,7 @@ public function assert(string $objectName, array $data, array $jsonSchema)
3838
$result = $this->jsonValidator()->schemaValidation($enforcedObjectData, OpisSchema::fromJsonString(\json_encode($jsonSchema)));
3939

4040
if (! $result->isValid()) {
41-
$errors = [];
42-
43-
foreach ($result->getErrors() as $error) {
44-
$errors[] = \sprintf('[%s] %s', $error->keyword(), \json_encode($error->keywordArgs(), JSON_PRETTY_PRINT));
45-
46-
if ($error->subErrorsCount()) {
47-
foreach ($error->subErrors() as $subError) {
48-
$errors[] = \sprintf("[%s] %s\n", $subError->keyword(), \json_encode($subError->keywordArgs(), JSON_PRETTY_PRINT));
49-
}
50-
}
51-
}
52-
53-
throw new InvalidArgumentException(
54-
"Validation of $objectName failed: " . \implode("\n", $errors)
55-
);
41+
throw OpisJsonValidationError::withError($objectName, ...$result->getErrors());
5642
}
5743
}
5844

0 commit comments

Comments
 (0)