Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursors #322

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/Database/Validator/Cursor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Utopia\Database\Validator;

class Cursor extends UID
{
protected int $maxLength = 100;

/**
* Get Description.
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
{
$message = 'Cursor must contain at most %u chars. Valid chars are a-z, A-Z, 0-9, and underscore. Can\'t start with a leading underscore';
return sprintf($message, $this->maxLength);
}
}
5 changes: 3 additions & 2 deletions src/Database/Validator/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

class Key extends Validator
{
protected bool $allowInternal = false; // If true, you keys starting with $ are allowed
protected bool $allowInternal = false; // If true, keys starting with $ are allowed
protected int $maxLength = 36;

/**
* @var string
Expand Down Expand Up @@ -73,7 +74,7 @@ public function isValid($value): bool
return false;
}

if (\mb_strlen($value) > 36) {
if (\mb_strlen($value) > $this->maxLength) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Database/Validator/Query/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\UID;
use Utopia\Database\Validator\Cursor as CursorUid;

class Cursor extends Base
{
Expand Down Expand Up @@ -33,7 +33,7 @@ public function isValid($value): bool
$cursor = $cursor->getId();
}

$validator = new UID();
$validator = new CursorUid();
if ($validator->isValid($cursor)) {
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/Validator/Query/CursorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utopia\Tests\Validator\Query;

use PHPUnit\Framework\TestCase;
use Utopia\Database\Document;
use Utopia\Database\Query;
use Utopia\Database\Validator\Query\Cursor;

Expand All @@ -14,6 +15,7 @@ public function testValueSuccess(): void

$this->assertTrue($validator->isValid(new Query(Query::TYPE_CURSORAFTER, values: ['asdf'])));
$this->assertTrue($validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: ['asdf'])));
$this->assertTrue($validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: [new Document(['$id' => 'abc102030'])])));
}

public function testValueFailure(): void
Expand All @@ -28,5 +30,11 @@ public function testValueFailure(): void
$this->assertFalse($validator->isValid(Query::equal('attr', ['v'])));
$this->assertFalse($validator->isValid(Query::orderAsc('attr')));
$this->assertFalse($validator->isValid(Query::orderDesc('attr')));

$uid = 'uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_uid0123456_';

$this->assertFalse($validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: [$uid])));
$this->assertFalse($validator->isValid(new Query(Query::TYPE_CURSORBEFORE, values: [new Document(['$id' => $uid])])));
$this->assertEquals('Invalid cursor: Cursor must contain at most 100 chars. Valid chars are a-z, A-Z, 0-9, and underscore. Can\'t start with a leading underscore', $validator->getDescription());
}
}