mirror of
https://github.com/appwrite/appwrite
synced 2026-05-22 16:38:32 +00:00
Add compound UID valdiator
This commit is contained in:
parent
ff2e22d78f
commit
d3913c422b
1 changed files with 58 additions and 0 deletions
58
src/Appwrite/Utopia/Database/Validator/CompoundUID.php
Normal file
58
src/Appwrite/Utopia/Database/Validator/CompoundUID.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Utopia\Database\Validator;
|
||||
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Validator;
|
||||
|
||||
class CompoundUID extends Validator
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Must consist of multiple UIDs separated by a colon. Each UID must contain at most 36 chars. Valid chars are a-z, A-Z, 0-9, and underscore. Can\'t start with a special char.';
|
||||
}
|
||||
|
||||
public function isArray(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isValid($value): bool
|
||||
{
|
||||
if (!\is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ids = static::parse($value);
|
||||
|
||||
if (\count($ids) < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($ids as $id) {
|
||||
$validator = new UID();
|
||||
if (!$validator->isValid($id)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return self::TYPE_STRING;
|
||||
}
|
||||
|
||||
public static function parse(string $key): array
|
||||
{
|
||||
$parts = \explode(':', $key);
|
||||
$result = [];
|
||||
|
||||
foreach ($parts as $part) {
|
||||
$result[] = $part;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue