This commit is contained in:
2023-10-02 23:10:49 +03:00
commit c6cb488379
255 changed files with 18731 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
<?php
namespace Lucent\Schema\Validator;
interface MinMaxInterface
{
public function failMin(mixed $value): bool;
public function failMax(mixed $value): bool;
}
@@ -0,0 +1,9 @@
<?php
namespace Lucent\Schema\Validator;
interface RequiredInterface
{
public function failRequired(mixed $value): bool;
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace Lucent\Schema\Validator;
use Lucent\Channel\ChannelService;
use Lucent\Edge\EdgeCollection;
use Lucent\Primitive\Collection;
use Lucent\Record\RecordData;
use Lucent\Schema\FieldInterface;
class Validator
{
function __construct(
public ChannelService $channelService
)
{
}
/**
* @return Collection<ValidatorError>
*/
public function check(
string $schemaName,
RecordData $data,
?EdgeCollection $edges,
): Collection
{
$schema = $this->channelService->getSchema($schemaName)->get();
return $schema->fields
->map(fn(FieldInterface $f) => $this->validate($f, $data, $edges))
->filter(fn(?ValidatorError $error) => !empty($error))
->values();
}
public function validate(FieldInterface $field, RecordData $recordData, ?EdgeCollection $edges): ?ValidatorError
{
$value = $recordData->get($field->name);
if ($field instanceof RequiredInterface && $field->required && $field->failRequired($value)) {
return new ValidatorError($field->name, $field->label, "Field is required");
}
if ($field instanceof MinMaxInterface && !is_null($field->min) && $field->failMin($value)) {
return new ValidatorError($field->name, $field->label, "The minimum {$field->label} can be {$field->min}");
}
if ($field instanceof MinMaxInterface && !is_null($field->max) && $field->failMax($value)) {
return new ValidatorError($field->name, $field->label, "The maximum {$field->label} can be {$field->min}");
}
return null;
}
/**
* @param Collection<ValidatorError> $errors
* @throws ValidatorException
*/
public function throwException(Collection $errors): void
{
throw new ValidatorException($this->errorsByField($errors));
}
/**
* @param Collection<ValidatorError> $errors
* @return array<string,ValidatorError>
**/
public function errorsByField(Collection $errors): array
{
return collect($errors)->keyBy("fieldName")->toArray();
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Lucent\Schema\Validator;
class ValidatorError
{
function __construct(
public readonly string $fieldName,
public readonly string $fieldLabel,
public readonly string $message,
)
{
}
}
@@ -0,0 +1,32 @@
<?php
namespace Lucent\Schema\Validator;
use Exception;
final class ValidatorException extends Exception
{
private array $validatorErrors;
// Redefine the exception so message isn't optional
public function __construct(array $message, int $code = 0, Exception $previous = null)
{
// make sure everything is assigned properly
$this->validatorErrors = $message;
parent::__construct("You have one or more validation errors", $code, $previous);
}
public function getValidatorErrors(): array
{
return $this->validatorErrors;
}
public function getFirstValidatorError(): array
{
return (array)array_shift($this->validatorErrors);
}
}
@@ -0,0 +1,13 @@
<?php
namespace Lucent\Schema\Validator;
interface ValidatorInterface
{
public function validate(): ValidatorInterface;
public function value(): ValidatorError;
public function hasError(): bool;
}