build-configs/src/Validator/ConfigurationValidator.php

73 lines
2.2 KiB
PHP
Raw Normal View History

<?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(
[
2023-03-10 22:10:07 +00:00
'fields' => [
'name' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Length(['min' => 1]),
],
2023-03-10 22:10:07 +00:00
'language' => [
new Assert\NotNull(),
new Assert\Type('string'),
new Assert\Choice(['node', 'php']),
2023-03-10 22:10:07 +00:00
],
2023-03-10 22:10:07 +00:00
'type' => [
new Assert\NotNull(),
new Assert\Type('string'),
2023-03-12 11:08:00 +00:00
new Assert\Choice(['drupal-project', 'fractal', 'php-library']),
2023-03-10 22:10:07 +00:00
],
'project_root' => [
new Assert\NotNull(),
new Assert\Type('string'),
],
2023-03-10 22:10:07 +00:00
'database' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
'drupal' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
'docker-compose' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
'dockerfile' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
// TODO: this should be a boolean if present.
'justfile' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
'php' => new Assert\Optional(),
2023-03-10 22:10:07 +00:00
'web' => new Assert\Optional(),
],
'allowExtraFields' => false,
'allowMissingFields' => true,
],
);
2023-03-10 22:10:07 +00:00
return $validator->validate(
constraints: $constraint,
groups: $groups,
value: $configurationData,
);
}
}