Skip to content

Commit 57ac469

Browse files
author
Alexander Miertsch
authored
Merge pull request #16 from rikgirbes/opi-json-schema-update
Updated: opis/json-schema to 2.3
2 parents 70bebeb + f017afc commit 57ac469

File tree

5 files changed

+66
-35
lines changed

5 files changed

+66
-35
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^7.4 || ^8.0",
19+
"php": "^8.1",
2020
"ext-json": "*",
2121
"event-engine/php-data": "^2.0.1",
2222
"event-engine/php-engine-utils": "^0.2.1",
@@ -26,7 +26,7 @@
2626
"require-dev": {
2727
"justinrainbow/json-schema": "^5.2",
2828
"malukenho/docheader": "^0.1.4",
29-
"opis/json-schema": "^1.0",
29+
"opis/json-schema": "^2.3.0",
3030
"phpunit/phpunit": "^8.0 || ^9.0",
3131
"prooph/php-cs-fixer-config": "^v0.4.0",
3232
"roave/security-advisories": "dev-latest",

src/Exception/OpisJsonValidationError.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
namespace EventEngine\JsonSchema\Exception;
1313

1414

15-
use Opis\JsonSchema\ValidationError;
15+
use Opis\JsonSchema\Errors\ErrorFormatter;
16+
use Opis\JsonSchema\Errors\ValidationError;
1617

1718
class OpisJsonValidationError extends JsonValidationError
1819
{
1920
/**
2021
* @var ValidationError[]
2122
*/
22-
private $errors;
23+
private array $errors;
24+
private ?ErrorFormatter $errorFormatter = null;
2325

2426
public static function withError(string $objectName, ValidationError ...$validationErrors): OpisJsonValidationError
2527
{
@@ -28,15 +30,6 @@ public static function withError(string $objectName, ValidationError ...$validat
2830

2931
foreach ($validationErrors as $error) {
3032
$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-
}
4033
}
4134

4235
return $self;
@@ -50,18 +43,13 @@ public function errors(): array
5043
return $this->errors;
5144
}
5245

53-
private function errorMessage(ValidationError $error): string
46+
private function errorFormatter(): ErrorFormatter
5447
{
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-
}
48+
return $this->errorFormatter ??= new ErrorFormatter();
49+
}
6050

61-
return \sprintf('field "%s" [%s] %s',
62-
implode('.', $dataPointer),
63-
$error->keyword(),
64-
\json_encode($error->keywordArgs(), JSON_PRETTY_PRINT)
65-
);
51+
private function errorMessage(ValidationError $error): string
52+
{
53+
return json_encode($this->errorFormatter()->formatOutput($error, "basic"), JSON_PRETTY_PRINT);
6654
}
6755
}

src/OpisJsonSchema.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace EventEngine\JsonSchema;
1313

1414
use EventEngine\JsonSchema\Exception\OpisJsonValidationError;
15-
use Opis\JsonSchema\Schema as OpisSchema;
15+
use Opis\JsonSchema\Helper as OpisHelper;
1616
use Opis\JsonSchema\Validator;
1717

1818
class OpisJsonSchema extends AbstractJsonSchema
@@ -35,10 +35,14 @@ public function assert(string $objectName, array $data, array $jsonSchema)
3535

3636
$enforcedObjectData = \json_decode(\json_encode($data));
3737

38-
$result = $this->jsonValidator()->schemaValidation($enforcedObjectData, OpisSchema::fromJsonString(\json_encode($jsonSchema)));
38+
$schema = $this->jsonValidator()
39+
->loader()
40+
->loadObjectSchema(OpisHelper::toJSON($jsonSchema));
41+
42+
$result = $this->jsonValidator()->validate($enforcedObjectData, $schema);
3943

4044
if (! $result->isValid()) {
41-
throw OpisJsonValidationError::withError($objectName, ...$result->getErrors());
45+
throw OpisJsonValidationError::withError($objectName, $result->error());
4246
}
4347
}
4448

tests/OpisJsonSchemaTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ public function it_throws_json_validation_error_exception(): void
8484
$data['unknown'] = 'set';
8585

8686
$expectedMessage = <<<'Msg'
87-
Validation of "myObject" failed: [additionalProperties] []
88-
field "unknown" [$schema] {
89-
"schema": false
87+
Validation of "myObject" failed: {
88+
"valid": false,
89+
"errors": [
90+
{
91+
"keywordLocation": "#\/additionalProperties",
92+
"instanceLocation": "#",
93+
"error": "Additional object properties are not allowed: unknown"
94+
}
95+
]
9096
}
9197
Msg;
9298

@@ -108,8 +114,20 @@ public function it_throws_justin_rainbow_json_validation_error_exception(): void
108114
$data['subObject']['unknown'] = 'set';
109115

110116
$expectedMessage = <<<'Msg'
111-
Validation of "myObject" failed: field "subObject" [required] {
112-
"missing": "p1"
117+
Validation of "myObject" failed: {
118+
"valid": false,
119+
"errors": [
120+
{
121+
"keywordLocation": "#\/properties",
122+
"instanceLocation": "#",
123+
"error": "The properties must match schema: subObject"
124+
},
125+
{
126+
"keywordLocation": "#\/properties\/subObject\/required",
127+
"instanceLocation": "#\/subObject",
128+
"error": "The required properties (p1) are missing"
129+
}
130+
]
113131
}
114132
Msg;
115133

tests/TypeSchemaReferenceTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,30 @@ public function it_throws_json_validation_error_exception(): void
9494
$data['subObject']['p1'] = 'to';
9595

9696
$expectedMessage = <<<'Msg'
97-
Validation of "TestMessage payload" failed: field "subObject.p1" [minLength] {
98-
"min": 3,
99-
"length": 2
97+
Validation of "TestMessage payload" failed: {
98+
"valid": false,
99+
"errors": [
100+
{
101+
"keywordLocation": "#\/properties",
102+
"instanceLocation": "#",
103+
"error": "The properties must match schema: subObject"
104+
},
105+
{
106+
"keywordLocation": "#\/properties\/subObject\/properties",
107+
"instanceLocation": "#\/subObject",
108+
"error": "The properties must match schema: p1"
109+
},
110+
{
111+
"keywordLocation": "#\/properties\/subObject\/properties\/p1\/%24ref",
112+
"instanceLocation": "#\/subObject\/p1",
113+
"error": "The data must match $ref"
114+
},
115+
{
116+
"keywordLocation": "#\/definitions\/SubObject\/P1\/minLength",
117+
"instanceLocation": "#\/subObject\/p1",
118+
"error": "Minimum string length is 3, found 2"
119+
}
120+
]
100121
}
101122
Msg;
102123

0 commit comments

Comments
 (0)