refactor: extract a configuration validator

This commit is contained in:
Oliver Davies 2023-02-22 15:03:01 +00:00
parent 46747d2bf8
commit a70557d2d5
4 changed files with 77 additions and 43 deletions

View file

@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfigs\Validator;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validation;
final class ConfigurationValidator implements ValidatorInterface
{
public function validate(array $configurationData): ConstraintViolationListInterface
{
$validator = Validation::createValidator();
$groups = new Assert\GroupSequence(['Default', 'custom']);
$constraint = new Assert\Collection(
[
'name' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Length(['min' => 1]),
],
'language' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Choice(['php']),
],
'type' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Choice(['drupal-project', 'php-library']),
],
'database' => new Assert\Optional(),
'drupal' => new Assert\Optional(),
'docker-compose' => new Assert\Optional(),
'dockerfile' => new Assert\Optional(),
// TODO: this should be a boolean if present.
'justfile' => new Assert\Optional(),
'php' => new Assert\Optional(),
'web' => new Assert\Optional(),
],
);
return $validator->validate($configurationData, $constraint, $groups);
}
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace OliverDaviesLtd\BuildConfigs\Validator;
use Symfony\Component\Validator\ConstraintViolationListInterface;
interface ValidatorInterface
{
public function validate(array $configurationData): ConstraintViolationListInterface;
}