Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle validation when fields has no validation guess
- Loading branch information
Showing
78 changed files
with
234 additions
and
74 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
34 changes: 34 additions & 0 deletions
34
src/Component/JsonSchema/Tests/fixtures/validator/expected/Model/Foo.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,34 @@ | ||
<?php | ||
|
||
namespace Jane\JsonSchema\Tests\Expected\Model; | ||
|
||
class Foo | ||
{ | ||
/** | ||
* | ||
* | ||
* @var string | ||
*/ | ||
protected $foo; | ||
/** | ||
* | ||
* | ||
* @return string | ||
*/ | ||
public function getFoo() : string | ||
{ | ||
return $this->foo; | ||
} | ||
/** | ||
* | ||
* | ||
* @param string $foo | ||
* | ||
* @return self | ||
*/ | ||
public function setFoo(string $foo) : self | ||
{ | ||
$this->foo = $foo; | ||
return $this; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/Component/JsonSchema/Tests/fixtures/validator/expected/Normalizer/FooNormalizer.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,66 @@ | ||
<?php | ||
|
||
namespace Jane\JsonSchema\Tests\Expected\Normalizer; | ||
|
||
use Jane\Component\JsonSchemaRuntime\Reference; | ||
use Jane\JsonSchema\Tests\Expected\Runtime\Normalizer\CheckArray; | ||
use Symfony\Component\Serializer\Exception\InvalidArgumentException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
class FooNormalizer implements DenormalizerInterface, NormalizerInterface, DenormalizerAwareInterface, NormalizerAwareInterface | ||
{ | ||
use DenormalizerAwareTrait; | ||
use NormalizerAwareTrait; | ||
use CheckArray; | ||
public function supportsDenormalization($data, $type, $format = null) : bool | ||
{ | ||
return $type === 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Foo'; | ||
} | ||
public function supportsNormalization($data, $format = null) : bool | ||
{ | ||
return $data instanceof \Jane\JsonSchema\Tests\Expected\Model\Foo; | ||
} | ||
/** | ||
* @return mixed | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = array()) | ||
{ | ||
if (isset($data['$ref'])) { | ||
return new Reference($data['$ref'], $context['document-origin']); | ||
} | ||
if (isset($data['$recursiveRef'])) { | ||
return new Reference($data['$recursiveRef'], $context['document-origin']); | ||
} | ||
$object = new \Jane\JsonSchema\Tests\Expected\Model\Foo(); | ||
$validator = new \Jane\JsonSchema\Tests\Expected\Validator\FooValidator(); | ||
if (!($data['skip_validation'] ?? false)) { | ||
$validator->validate($data); | ||
} | ||
if (null === $data || false === \is_array($data)) { | ||
return $object; | ||
} | ||
if (\array_key_exists('foo', $data)) { | ||
$object->setFoo($data['foo']); | ||
} | ||
return $object; | ||
} | ||
/** | ||
* @return array|string|int|float|bool|\ArrayObject|null | ||
*/ | ||
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
$data = array(); | ||
if (null !== $object->getFoo()) { | ||
$data['foo'] = $object->getFoo(); | ||
} | ||
$validator = new \Jane\JsonSchema\Tests\Expected\Validator\FooValidator(); | ||
if (!($data['skip_validation'] ?? false)) { | ||
$validator->validate($data); | ||
} | ||
return $data; | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
src/Component/JsonSchema/Tests/fixtures/validator/expected/Validator/FooValidator.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,16 @@ | ||
<?php | ||
|
||
namespace Jane\JsonSchema\Tests\Expected\Validator; | ||
|
||
class FooValidator implements \Jane\JsonSchema\Tests\Expected\Validator\ValidatorInterface | ||
{ | ||
public function validate($data) : void | ||
{ | ||
$constraints = array(new \Symfony\Component\Validator\Constraints\Collection(array('fields' => array('foo' => new \Symfony\Component\Validator\Constraints\Optional(array(new \Symfony\Component\Validator\Constraints\Type(array('0' => 'string'))))), 'allowExtraFields' => true))); | ||
$validator = \Symfony\Component\Validator\Validation::createValidator(); | ||
$violations = $validator->validate($data, $constraints); | ||
if ($violations->count() > 0) { | ||
throw new \Jane\JsonSchema\Tests\Expected\Validator\ValidationException($violations); | ||
} | ||
} | ||
} |
Oops, something went wrong.