diff --git a/src/Database/Validator/Key.php b/src/Database/Validator/Key.php index 7bb42f0c1..3b51738be 100644 --- a/src/Database/Validator/Key.php +++ b/src/Database/Validator/Key.php @@ -42,7 +42,7 @@ public function isValid($value) return false; } - if (\preg_match('/[^A-Za-z0-9\-\_]/', $value)) { + if (\preg_match('/[^A-Za-z0-9\_]/', $value)) { return false; } diff --git a/src/Database/Validator/UID.php b/src/Database/Validator/UID.php index 2742364ef..1d586da21 100644 --- a/src/Database/Validator/UID.php +++ b/src/Database/Validator/UID.php @@ -2,9 +2,9 @@ namespace Utopia\Database\Validator; -use Utopia\Validator; +use Utopia\Database\Validator\Key; -class UID extends Validator +class UID extends Key { /** * Get Description. @@ -15,56 +15,6 @@ class UID extends Validator */ public function getDescription() { - return 'Invalid UID format'; - } - - /** - * Is valid. - * - * Returns true if valid or false if not. - * - * @param mixed $value - * - * @return bool - */ - public function isValid($value) - { - if ($value === 0) { // TODO Deprecate confition when we get the chance. - return true; - } - - if (!is_string($value)) { - return false; - } - - if (mb_strlen($value) > 32) { - return false; - } - - return true; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; + return 'UID must contain only alphanumeric chars or non-leading underscore, shorter than 32 chars'; } } diff --git a/tests/Database/Validator/KeyTest.php b/tests/Database/Validator/KeyTest.php index 6b6e21dcd..3e024f5bb 100644 --- a/tests/Database/Validator/KeyTest.php +++ b/tests/Database/Validator/KeyTest.php @@ -23,17 +23,30 @@ public function tearDown(): void public function testValues() { - $this->assertEquals($this->object->isValid('dasda asdasd'), false); - $this->assertEquals($this->object->isValid('asdasdasdas'), true); - $this->assertEquals($this->object->isValid('_asdasdasdas'), false); - $this->assertEquals($this->object->isValid('asd"asdasdas'), false); - $this->assertEquals($this->object->isValid('asd\'asdasdas'), false); - $this->assertEquals($this->object->isValid('as$$5dasdasdas'), false); - $this->assertEquals($this->object->isValid(false), false); - $this->assertEquals($this->object->isValid(null), false); - $this->assertEquals($this->object->isValid('socialAccountForYoutubeSubscribers'), false); - $this->assertEquals($this->object->isValid('socialAccountForYoutubeSubscriber'), false); - $this->assertEquals($this->object->isValid('socialAccountForYoutubeSubscribe'), true); - $this->assertEquals($this->object->isValid('socialAccountForYoutubeSubscrib'), true); + // Must be strings + $this->assertEquals(false, $this->object->isValid(false)); + $this->assertEquals(false, $this->object->isValid(null)); + $this->assertEquals(false, $this->object->isValid(['value'])); + $this->assertEquals(false, $this->object->isValid(0)); + $this->assertEquals(false, $this->object->isValid(1.5)); + $this->assertEquals(true, $this->object->isValid('asdas7as9as')); + $this->assertEquals(true, $this->object->isValid('5f058a8925807')); + + // No leading underscore + $this->assertEquals(false, $this->object->isValid('_asdasdasdas')); + $this->assertEquals(true, $this->object->isValid('a_sdasdasdas')); + + // No Special characters + $this->assertEquals(false, $this->object->isValid('dasda asdasd')); + $this->assertEquals(false, $this->object->isValid('asd"asd6sdas')); + $this->assertEquals(false, $this->object->isValid('asd\'as0asdas')); + $this->assertEquals(false, $this->object->isValid('as$$5dasdasdas')); + $this->assertEquals(false, $this->object->isValid('as-5dasdasdas')); + + // At most 32 chars + $this->assertEquals(true, $this->object->isValid('socialAccountForYoutubeSubscribe')); + $this->assertEquals(false, $this->object->isValid('socialAccountForYoutubeSubscribers')); + $this->assertEquals(true, $this->object->isValid('5f058a89258075f058a89258075f058t')); + $this->assertEquals(false, $this->object->isValid('5f058a89258075f058a89258075f058tx')); } } diff --git a/tests/Database/Validator/UIDTest.php b/tests/Database/Validator/UIDTest.php index 8877eb31b..b890a8ecf 100644 --- a/tests/Database/Validator/UIDTest.php +++ b/tests/Database/Validator/UIDTest.php @@ -2,29 +2,8 @@ namespace Utopia\Tests\Validator; -use Utopia\Database\Validator\UID; -use PHPUnit\Framework\TestCase; +use Utopia\Database\Validator\Key; -class UIDTest extends TestCase +class UIDTest extends KeyTest { - /** - * @var UID - */ - protected $object = null; - - public function setUp(): void - { - $this->object = new UID(); - } - - public function tearDown(): void - { - } - - public function testValues() - { - $this->assertEquals($this->object->isValid('5f058a8925807'), true); - $this->assertEquals($this->object->isValid('5f058a89258075f058a89258075f058t'), true); - $this->assertEquals($this->object->isValid('5f058a89258075f058a89258075f058tx'), false); - } }