-
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.
Merge pull request #9 from KnpLabs/feat/add-opis-validator
feat: add opis validator
- Loading branch information
Showing
5 changed files
with
118 additions
and
2 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
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KnpLabs\JsonSchemaBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
class Configuration implements ConfigurationInterface | ||
{ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder('knp_json_schema'); | ||
|
||
$treeBuilder->getRootNode() | ||
->children() | ||
->enumNode('validator') | ||
->values(['OpisValidator', 'SwaggestValidator']) | ||
->defaultValue('SwaggestValidator') | ||
->end() | ||
->end() | ||
; | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KnpLabs\JsonSchemaBundle\Validator; | ||
|
||
use KnpLabs\JsonSchema\JsonSchemaInterface; | ||
use KnpLabs\JsonSchema\Validator; | ||
use KnpLabs\JsonSchema\Validator\Error as KnpJsonSchemaError; | ||
use KnpLabs\JsonSchema\Validator\Errors; | ||
use Opis\JsonSchema\Errors\ErrorFormatter; | ||
use Opis\JsonSchema\Errors\ValidationError; | ||
use Opis\JsonSchema\Validator as JsonSchemaValidator; | ||
|
||
class OpisValidator implements Validator | ||
{ | ||
private JsonSchemaValidator $validator; | ||
|
||
public function __construct() | ||
{ | ||
$this->validator = new JsonSchemaValidator(); | ||
$this->validator->setMaxErrors(10); | ||
} | ||
|
||
public function validate(array $data, JsonSchemaInterface $schema): ?Errors | ||
{ | ||
/** | ||
* @var mixed | ||
*/ | ||
$data = json_decode( | ||
json_encode( | ||
$data, | ||
flags: JSON_THROW_ON_ERROR, | ||
), | ||
flags: JSON_THROW_ON_ERROR | ||
); | ||
|
||
$schema = json_decode( | ||
json_encode( | ||
$schema->getSchema(), | ||
flags: JSON_THROW_ON_ERROR, | ||
), | ||
flags: JSON_THROW_ON_ERROR | ||
); | ||
|
||
$result = $this->validator->validate($data, $schema); | ||
|
||
if ($result->isValid()) { | ||
return null; | ||
} | ||
|
||
return new Errors( | ||
...$this->yieldErrors($result->error()) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<KnpJsonSchemaError> | ||
*/ | ||
private function yieldErrors(ValidationError $error): iterable | ||
{ | ||
$formatter = new ErrorFormatter(); | ||
|
||
$errors = $formatter->format($error); | ||
|
||
foreach ($errors as $field => $message) { | ||
$formatted = sizeof($message) === 1 | ||
? $message[0] | ||
: json_encode($message) | ||
; | ||
|
||
yield new KnpJsonSchemaError( | ||
$field, | ||
$formatted, | ||
); | ||
} | ||
} | ||
} |