Skip to content

Commit

Permalink
Split validation + add missing validations
Browse files Browse the repository at this point in the history
  • Loading branch information
keichinger committed Oct 27, 2023
1 parent c1fb323 commit 747560f
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions src/Field/Definition/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Type;
use Torr\Storyblok\Context\ComponentContext;
Expand Down Expand Up @@ -81,30 +82,76 @@ protected function toManagementApiData () : array
*/
public function validateData (ComponentContext $context, array $contentPath, mixed $data, array $fullData) : void
{
$allowedValueTypeConstraints = new AtLeastOneOf([
new Type("string"),
new Type("int"),
]);
if ($this->allowMultiselect)
{
$this->validateMultiSelect($context, $contentPath, $data);
}
else
{
$this->validateSingleSelect($context, $contentPath, $data);
}
}


private function validateSingleSelect (ComponentContext $context, array $contentPath, mixed $data) : void
{
$context->ensureDataIsValid(
$contentPath,
$this,
$data,
[
$this->allowMultiselect
? new All([new NotNull(), $allowedValueTypeConstraints])
: $allowedValueTypeConstraints,
new AtLeastOneOf([
new Type("string"),
new Type("int"),
]),
$this->required
? new NotBlank()
: null,
],
);

\assert(null === $data || \is_array($data) || \is_int($data) || \is_string($data));
\assert(null === $data || \is_int($data) || \is_string($data));

if (\is_string($data))
{
$data = $context->normalizeOptionalString($data);
}

if (\is_array($data) && $this->allowMultiselect && (null !== $this->minimumNumberOfOptions || null !== $this->maximumNumberOfOptions))
$choicesConstraints = $this->choices->getValidationConstraints(false);

if (null !== $data && !empty($choicesConstraints))
{
$context->ensureDataIsValid(
$contentPath,
$this,
$data,
$choicesConstraints,
);
}
}


private function validateMultiSelect (ComponentContext $context, array $contentPath, mixed $data) : void
{
$context->ensureDataIsValid(
$contentPath,
$this,
$data,
[
new Type("Array"),
new All([
new NotNull(),
new AtLeastOneOf([
new Type("string"),
new Type("int"),
]),
]),
],
);

\assert(null === $data || \is_array($data));

if (null !== $this->minimumNumberOfOptions || null !== $this->maximumNumberOfOptions)
{
$context->ensureDataIsValid(
$contentPath,
Expand All @@ -121,7 +168,7 @@ public function validateData (ComponentContext $context, array $contentPath, mix
);
}

$choicesConstraints = $this->choices->getValidationConstraints($this->allowMultiselect);
$choicesConstraints = $this->choices->getValidationConstraints(true);

if (null !== $data && !empty($choicesConstraints))
{
Expand Down

0 comments on commit 747560f

Please sign in to comment.