From 9968027aada65bf1473cc8c68e18844359ec882b Mon Sep 17 00:00:00 2001 From: Romain BigZ Richard Date: Wed, 19 Dec 2018 15:41:27 +0100 Subject: [PATCH] feature: Add new validation from constraints. - Add minimum and maximum from the range annotation - Add maximum from the LessThan and LessThanOrEqual annotation FIX: remove the extra date-time format, which was not standard and not in sync with what symfony exposes Swagger specifies that date-time should follow the RFC3339, and this is what symfony does as default. --- .../SymfonyConstraintAnnotationReader.php | 9 +++- .../Functional/Entity/SymfonyConstraints.php | 45 +++++++++++++++++++ Tests/Functional/FunctionalTest.php | 13 ++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index 80d4231..db25d0d 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -63,8 +63,6 @@ class SymfonyConstraintAnnotationReader $property->setMaxLength($annotation->max); } elseif ($annotation instanceof Assert\Regex) { $this->appendPattern($property, $annotation->getHtmlPattern()); - } elseif ($annotation instanceof Assert\DateTime) { - $this->appendPattern($property, $annotation->format); } elseif ($annotation instanceof Assert\Count) { $property->setMinItems($annotation->min); $property->setMaxItems($annotation->max); @@ -72,6 +70,13 @@ class SymfonyConstraintAnnotationReader $property->setEnum($annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices); } elseif ($annotation instanceof Assert\Expression) { $this->appendPattern($property, $annotation->message); + } elseif ($annotation instanceof Assert\Range) { + $property->setMinimum($annotation->min); + $property->setMaximum($annotation->max); + } elseif ($annotation instanceof Assert\LessThan) { + $property->setExclusiveMaximum($annotation->value); + } elseif ($annotation instanceof Assert\LessThanOrEqual) { + $property->setMaximum($annotation->value); } } } diff --git a/Tests/Functional/Entity/SymfonyConstraints.php b/Tests/Functional/Entity/SymfonyConstraints.php index d4d51f8..6057d02 100644 --- a/Tests/Functional/Entity/SymfonyConstraints.php +++ b/Tests/Functional/Entity/SymfonyConstraints.php @@ -81,6 +81,27 @@ class SymfonyConstraints */ private $propertyExpression; + /** + * @var int + * + * @Assert\Range(min=1, max=5) + */ + private $propertyRange; + + /** + * @var int + * + * @Assert\LessThan(42) + */ + private $propertyLessThan; + + /** + * @var int + * + * @Assert\LessThanOrEqual(23) + */ + private $propertyLessThanOrEqual; + /** * @param int $propertyNotBlank */ @@ -153,6 +174,30 @@ class SymfonyConstraints $this->propertyExpression = $propertyExpression; } + /** + * @param int $propertyRange + */ + public function setPropertyRange(int $propertyRange): void + { + $this->propertyRange = $propertyRange; + } + + /** + * @param int $propertyLessThan + */ + public function setPropertyLessThan(int $propertyLessThan): void + { + $this->propertyLessThan = $propertyLessThan; + } + + /** + * @param int $propertyLessThanOrEqual + */ + public function setPropertyLessThanOrEqual(int $propertyLessThanOrEqual): void + { + $this->propertyLessThanOrEqual = $propertyLessThanOrEqual; + } + /** * @return array */ diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index a1ef239..d862ea0 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -385,6 +385,19 @@ class FunctionalTest extends WebTestCase 'type' => 'integer', 'pattern' => 'If this is a tech post, the category should be either php or symfony!', ], + 'propertyRange' => [ + 'type' => 'integer', + 'maximum' => 5, + 'minimum' => 1, + ], + 'propertyLessThan' => [ + 'type' => 'integer', + 'exclusiveMaximum' => 42, + ], + 'propertyLessThanOrEqual' => [ + 'type' => 'integer', + 'maximum' => 23, + ], ], 'type' => 'object', ], $this->getModel('SymfonyConstraints')->toArray());