Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 58 additions & 52 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
{
"name": "event-engine/php-json-schema",
"description": "Event Engine JSON Schema PHP Package",
"homepage": "https://event-engine.io/",
"license": "MIT",
"authors": [
{
"name": "Alexander Miertsch",
"email": "[email protected]",
"homepage": "http://www.prooph.de"
"name": "event-engine/php-json-schema",
"description": "Event Engine JSON Schema PHP Package",
"homepage": "https://event-engine.io/",
"license": "MIT",
"authors": [
{
"name": "Alexander Miertsch",
"email": "[email protected]",
"homepage": "http://www.prooph.de"
},
{
"name": "Sandro Keil",
"email": "[email protected]",
"homepage": "http://prooph-software.com/"
}
],
"require": {
"php": "^7.2",
"event-engine/php-data": "^0.1",
"event-engine/php-engine-utils": "^0.1",
"event-engine/php-schema": "^0.1",
"ramsey/uuid": "^3.6"
},
{
"name": "Sandro Keil",
"email": "[email protected]",
"homepage": "http://prooph-software.com/"
}
],
"require": {
"php": "^7.2",
"roave/security-advisories": "dev-master",
"event-engine/php-schema": "^0.1",
"event-engine/php-data": "^0.1",
"event-engine/php-engine-utils": "^0.1",
"ramsey/uuid" : "^3.6"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"justinrainbow/json-schema": "^5.2",
"opis/json-schema": "^1.0",
"prooph/php-cs-fixer-config": "^0.3",
"satooshi/php-coveralls": "^1.0",
"malukenho/docheader": "^0.1.4"
},
"autoload": {
"psr-4": {
"EventEngine\\JsonSchema\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"EventEngineTest\\JsonSchema\\": "tests/"
"require-dev": {
"ext-json": "*",
"justinrainbow/json-schema": "^5.2",
"malukenho/docheader": "^0.1.4",
"opis/json-schema": "^1.0",
"phpunit/phpunit": "^7.0",
"prooph/php-cs-fixer-config": "^0.3",
"roave/security-advisories": "dev-master",
"satooshi/php-coveralls": "^1.0"
},
"autoload": {
"psr-4": {
"EventEngine\\JsonSchema\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"EventEngineTest\\JsonSchema\\": "tests/"
}
},
"prefer-stable": true,
"config": {
"sort-packages": true,
"platform": {
}
},
"scripts": {
"check": [
"@cs",
"@docheader",
"@test"
],
"docheader": "vendor/bin/docheader check examples/ src/ tests/",
"cs": "php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix -v --diff",
"test": "vendor/bin/phpunit"
}
},
"prefer-stable": true,
"scripts": {
"check": [
"@cs",
"@docheader",
"@test"
],
"docheader": "vendor/bin/docheader check examples/ src/ tests/",
"cs": "php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix -v --diff",
"test": "vendor/bin/phpunit"
}
}
17 changes: 17 additions & 0 deletions src/Exception/JsonValidationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2019 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema\Exception;


class JsonValidationError extends InvalidArgumentException
{
}
58 changes: 58 additions & 0 deletions src/Exception/JustinRainbowJsonValidationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2019 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema\Exception;


class JustinRainbowJsonValidationError extends JsonValidationError
{
/**
* @var array
*/
private $errors;

public static function withError(string $objectName, array ...$errors): JustinRainbowJsonValidationError
{
$self = new self('Validation of "' . $objectName . '" failed: ');
$self->errors = $errors;

$self->message .= \array_reduce(
$errors,
static function ($message, array $error) use ($self) {
return $message . "\n" . $self->errorMessage($error);
}
);

return $self;
}

public function errors(): array
{
return $this->errors;
}

private function errorMessage(array $error): string
{
$dataPointer = $error['pointer'];

if ($dataPointer === '') {
return \sprintf('[%s] %s', $error['constraint'], $error['message']);
}

return \sprintf('field "%s" [%s] %s',
$error['property'],
$error['constraint'],
$error['message']
);

// return sprintf('field "%s" [%s] %s', $error['constraint'], $error['property'], $error['message']);
}
}
67 changes: 67 additions & 0 deletions src/Exception/OpisJsonValidationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* This file is part of event-engine/php-json-schema.
* (c) 2018-2019 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\JsonSchema\Exception;


use Opis\JsonSchema\ValidationError;

class OpisJsonValidationError extends JsonValidationError
{
/**
* @var ValidationError[]
*/
private $errors;

public static function withError(string $objectName, ValidationError ...$validationErrors): OpisJsonValidationError
{
$self = new self('Validation of "' . $objectName . '" failed: ');
$self->errors = $validationErrors;

foreach ($validationErrors as $error) {
$self->message .= $self->errorMessage($error);

if ($error->subErrorsCount()) {
$self->message .= \array_reduce(
$error->subErrors(),
static function ($message, ValidationError $error) use ($self) {
return $message . "\n" . $self->errorMessage($error);
}
);
}
}

return $self;
}

/**
* @return ValidationError[]
*/
public function errors(): array
{
return $this->errors;
}

private function errorMessage(ValidationError $error): string
{
$dataPointer = $error->dataPointer();

if (count($dataPointer) === 0) {
return \sprintf('[%s] %s', $error->keyword(), \json_encode($error->keywordArgs(), JSON_PRETTY_PRINT));
}

return \sprintf('field "%s" [%s] %s',
implode('.', $dataPointer),
$error->keyword(),
\json_encode($error->keywordArgs(), JSON_PRETTY_PRINT)
);
}
}
12 changes: 2 additions & 10 deletions src/JustinRainbowJsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace EventEngine\JsonSchema;

use EventEngine\JsonSchema\Exception\InvalidArgumentException;
use EventEngine\JsonSchema\Exception\JustinRainbowJsonValidationError;
use JsonSchema\Validator;

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

if (! $this->jsonValidator()->isValid()) {
$errors = $this->jsonValidator()->getErrors();

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

foreach ($errors as $i => $error) {
$errors[$i] = \sprintf("[%s] %s\n", $error['property'], $error['message']);
}

throw new InvalidArgumentException(
"Validation of $objectName failed: " . \implode("\n", $errors)
);
throw JustinRainbowJsonValidationError::withError($objectName, ...$errors);
}

$this->jsonValidator()->reset();
Expand Down
18 changes: 2 additions & 16 deletions src/OpisJsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace EventEngine\JsonSchema;

use EventEngine\JsonSchema\Exception\InvalidArgumentException;
use EventEngine\JsonSchema\Exception\OpisJsonValidationError;
use Opis\JsonSchema\Schema as OpisSchema;
use Opis\JsonSchema\Validator;

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

if (! $result->isValid()) {
$errors = [];

foreach ($result->getErrors() as $error) {
$errors[] = \sprintf('[%s] %s', $error->keyword(), \json_encode($error->keywordArgs(), JSON_PRETTY_PRINT));

if ($error->subErrorsCount()) {
foreach ($error->subErrors() as $subError) {
$errors[] = \sprintf("[%s] %s\n", $subError->keyword(), \json_encode($subError->keywordArgs(), JSON_PRETTY_PRINT));
}
}
}

throw new InvalidArgumentException(
"Validation of $objectName failed: " . \implode("\n", $errors)
);
throw OpisJsonValidationError::withError($objectName, ...$result->getErrors());
}
}

Expand Down
Loading