Skip to content

Commit

Permalink
Handle validation when fields has no validation guess
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Jul 4, 2022
1 parent 11ea648 commit 4c3f0ac
Show file tree
Hide file tree
Showing 78 changed files with 234 additions and 74 deletions.
4 changes: 1 addition & 3 deletions src/Component/JsonSchema/Guesser/Guess/ClassGuess.php
Expand Up @@ -151,9 +151,7 @@ public function getPropertyValidatorGuesses(): array
{
$validatorGuesses = [];
foreach ($this->properties as $property) {
if (\count($propGuesses = $property->getValidatorGuesses()) > 0) {
$validatorGuesses[$property->getName()] = $propGuesses;
}
$validatorGuesses[$property->getName()] = $property->getValidatorGuesses();
}

return $validatorGuesses;
Expand Down
@@ -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;
}
}
Expand Up @@ -142,6 +142,12 @@ class Model
* @var string
*/
protected $uuidFormat;
/**
*
*
* @var Foo
*/
protected $foo;
/**
*
*
Expand Down Expand Up @@ -625,4 +631,25 @@ public function setUuidFormat(string $uuidFormat) : self
$this->uuidFormat = $uuidFormat;
return $this;
}
/**
*
*
* @return Foo
*/
public function getFoo() : Foo
{
return $this->foo;
}
/**
*
*
* @param Foo $foo
*
* @return self
*/
public function setFoo(Foo $foo) : self
{
$this->foo = $foo;
return $this;
}
}
@@ -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;
}
}
Expand Up @@ -14,7 +14,7 @@ class JaneObjectNormalizer implements DenormalizerInterface, NormalizerInterface
use DenormalizerAwareTrait;
use NormalizerAwareTrait;
use CheckArray;
protected $normalizers = array('Jane\\JsonSchema\\Tests\\Expected\\Model\\Model' => 'Jane\\JsonSchema\\Tests\\Expected\\Normalizer\\ModelNormalizer', '\\Jane\\Component\\JsonSchemaRuntime\\Reference' => '\\Jane\\JsonSchema\\Tests\\Expected\\Runtime\\Normalizer\\ReferenceNormalizer'), $normalizersCache = array();
protected $normalizers = array('Jane\\JsonSchema\\Tests\\Expected\\Model\\Model' => 'Jane\\JsonSchema\\Tests\\Expected\\Normalizer\\ModelNormalizer', 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Foo' => 'Jane\\JsonSchema\\Tests\\Expected\\Normalizer\\FooNormalizer', '\\Jane\\Component\\JsonSchemaRuntime\\Reference' => '\\Jane\\JsonSchema\\Tests\\Expected\\Runtime\\Normalizer\\ReferenceNormalizer'), $normalizersCache = array();
public function supportsDenormalization($data, $type, $format = null) : bool
{
return array_key_exists($type, $this->normalizers);
Expand Down
Expand Up @@ -132,6 +132,9 @@ public function denormalize($data, $class, $format = null, array $context = arra
if (\array_key_exists('uuidFormat', $data)) {
$object->setUuidFormat($data['uuidFormat']);
}
if (\array_key_exists('foo', $data)) {
$object->setFoo($this->denormalizer->denormalize($data['foo'], 'Jane\\JsonSchema\\Tests\\Expected\\Model\\Foo', 'json', $context));
}
return $object;
}
/**
Expand Down Expand Up @@ -225,6 +228,9 @@ public function normalize($object, $format = null, array $context = array())
if (null !== $object->getUuidFormat()) {
$data['uuidFormat'] = $object->getUuidFormat();
}
if (null !== $object->getFoo()) {
$data['foo'] = $this->normalizer->normalize($object->getFoo(), 'json', $context);
}
$validator = new \Jane\JsonSchema\Tests\Expected\Validator\ModelValidator();
if (!($data['skip_validation'] ?? false)) {
$validator->validate($data);
Expand Down
@@ -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);
}
}
}

0 comments on commit 4c3f0ac

Please sign in to comment.