AssertLength doesn't make the field required (#1389)

This commit is contained in:
Guilhem N 2018-08-30 00:32:11 +02:00 committed by GitHub
parent f0b1eb7849
commit fcb36d8e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 62 deletions

View File

@ -44,36 +44,28 @@ class SymfonyConstraintAnnotationReader
foreach ($annotations as $annotation) {
if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) {
$this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty);
}
if ($annotation instanceof Assert\Length) {
if ($annotation->min > 0) {
$this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty);
// The field is required
if (null === $this->schema) {
continue;
}
$existingRequiredFields = $this->schema->getRequired() ?? [];
$existingRequiredFields[] = $reflectionProperty->getName();
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
} elseif ($annotation instanceof Assert\Length) {
$property->setMinLength($annotation->min);
$property->setMaxLength($annotation->max);
}
if ($annotation instanceof Assert\Regex) {
} elseif ($annotation instanceof Assert\Regex) {
$this->appendPattern($property, $annotation->getHtmlPattern());
}
if ($annotation instanceof Assert\DateTime) {
} elseif ($annotation instanceof Assert\DateTime) {
$this->appendPattern($property, $annotation->format);
}
if ($annotation instanceof Assert\Count) {
} elseif ($annotation instanceof Assert\Count) {
$property->setMinItems($annotation->min);
$property->setMaxItems($annotation->max);
}
if ($annotation instanceof Assert\Choice) {
} elseif ($annotation instanceof Assert\Choice) {
$property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices);
}
if ($annotation instanceof Assert\Expression) {
} elseif ($annotation instanceof Assert\Expression) {
$this->appendPattern($property, $annotation->message);
}
}
@ -84,22 +76,6 @@ class SymfonyConstraintAnnotationReader
$this->schema = $schema;
}
/**
* Set the required properties on the scheme.
*/
private function updateSchemaDefinitionWithRequiredProperty(\ReflectionProperty $reflectionProperty)
{
if (null === $this->schema) {
return;
}
$existingRequiredFields = $this->schema->getRequired() ?? [];
$existingRequiredFields[] = $reflectionProperty->getName();
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
}
/**
* Append the pattern from the constraint to the existing pattern.
*/

View File

@ -29,19 +29,12 @@ class SymfonyConstraints
*/
private $propertyNotNull;
/**
* @var int
*
* @Assert\Length(min="1")
*/
private $propertyAssertLengthRequired;
/**
* @var int
*
* @Assert\Length(min="0", max="50")
*/
private $propertyAssertLengthMinAndMax;
private $propertyAssertLength;
/**
* @var int
@ -98,19 +91,11 @@ class SymfonyConstraints
}
/**
* @param int $propertyAssertLengthRequired
* @param int $propertyAssertLength
*/
public function setPropertyAssertLengthRequired(int $propertyAssertLengthRequired): void
public function setPropertyAssertLength(int $propertyAssertLength): void
{
$this->propertyAssertLengthRequired = $propertyAssertLengthRequired;
}
/**
* @param int $propertyAssertLengthMinAndMax
*/
public function setPropertyAssertLengthMinAndMax(int $propertyAssertLengthMinAndMax): void
{
$this->propertyAssertLengthMinAndMax = $propertyAssertLengthMinAndMax;
$this->propertyAssertLength = $propertyAssertLength;
}
/**

View File

@ -339,7 +339,6 @@ class FunctionalTest extends WebTestCase
'required' => [
'propertyNotBlank',
'propertyNotNull',
'propertyAssertLengthRequired',
],
'properties' => [
'propertyNotBlank' => [
@ -348,11 +347,7 @@ class FunctionalTest extends WebTestCase
'propertyNotNull' => [
'type' => 'integer',
],
'propertyAssertLengthRequired' => [
'type' => 'integer',
'minLength' => '1',
],
'propertyAssertLengthMinAndMax' => [
'propertyAssertLength' => [
'type' => 'integer',
'maxLength' => '50',
'minLength' => '0',