This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
1,161 additions
and
106 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/vendor/ | ||
.idea | ||
composer.lock | ||
coverage |
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
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,97 @@ | ||
<?php | ||
|
||
namespace RREST\Validator; | ||
|
||
use League\JsonGuard\Validator; | ||
use League\JsonReference\Dereferencer; | ||
use RREST\Error; | ||
use RREST\Exception\InvalidJSONException; | ||
use RREST\Validator\Util\JsonGuardValidationErrorConverter\Converter; | ||
|
||
class JsonValidator | ||
{ | ||
/** | ||
* @var Error[] | ||
*/ | ||
private $errors = []; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $isValidated = false; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $jsonValue; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $jsonSchema; | ||
|
||
/** | ||
* @param string $jsonValue | ||
* @param string $jsonSchema | ||
*/ | ||
public function __construct($jsonValue, $jsonSchema) | ||
{ | ||
$this->jsonValue = $jsonValue; | ||
$this->jsonSchema = $jsonSchema; | ||
} | ||
|
||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function fails() | ||
{ | ||
return empty($this->getErrors()) === false; | ||
} | ||
|
||
/** | ||
* @return Error[] | ||
*/ | ||
public function getErrors() | ||
{ | ||
$this->validate(); | ||
return $this->errors; | ||
} | ||
|
||
public function validate() | ||
{ | ||
if ($this->isValidated) return; | ||
|
||
$schema = Dereferencer::draft4()->dereference( | ||
$this->getJsonFromString($this->jsonSchema) | ||
); | ||
$json = $this->getJsonFromString($this->jsonValue); | ||
$validator = new Validator($json, $schema); | ||
if ($validator->fails()) { | ||
$this->errors = []; | ||
foreach ($validator->errors() as $jsonGuardError) { | ||
$this->errors = \array_merge( | ||
$this->errors, | ||
(new Converter($jsonGuardError))->getErrors() | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @throws InvalidJSONException | ||
* @param $value | ||
* @return \stdClass|array | ||
*/ | ||
private function getJsonFromString($value) | ||
{ | ||
$json = json_decode($value); | ||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
$error = new Error(ucfirst(json_last_error_msg()), Error::INVALID_JSON); | ||
throw new InvalidJSONException([$error]); | ||
} | ||
|
||
return $json; | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/Validator/Util/JsonGuardValidationErrorConverter/AnyOfConverter.php
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,32 @@ | ||
<?php | ||
|
||
namespace RREST\Validator\Util\JsonGuardValidationErrorConverter; | ||
|
||
use League\JsonGuard\Constraint\DraftFour\Required; | ||
use RREST\Error; | ||
|
||
class AnyOfConverter extends ConverterAbstract | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrors() | ||
{ | ||
$errors = []; | ||
$fields = []; | ||
foreach ($this->validationError->getParameter() as $subconstraint) { | ||
if (isset($subconstraint->{Required::KEYWORD})) { | ||
$fields = \array_merge($subconstraint->{Required::KEYWORD}, $fields); | ||
} | ||
} | ||
|
||
$context = new \stdClass; | ||
$context->fields = $fields; | ||
$errors[] = new Error( | ||
\sprintf('The field %s is required', implode(' or/and ', $fields)), | ||
Error::DATA_VALIDATION_REQUIRED_ANYOF, | ||
$context | ||
); | ||
return $errors; | ||
} | ||
} |
Oops, something went wrong.