Skip to content

Commit

Permalink
Init V2 validators
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Oct 29, 2024
1 parent c7af565 commit a87eed3
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 42 deletions.
16 changes: 11 additions & 5 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Utopia\Database\Validator\Index as IndexValidator;
use Utopia\Database\Validator\Permissions;
use Utopia\Database\Validator\Queries\Document as DocumentValidator;
use Utopia\Database\Validator\Queries\Documents as DocumentsValidator;
use Utopia\Database\Validator\Queries\V2 as DocumentsValidator;
use Utopia\Database\Validator\Structure;

class Database
Expand Down Expand Up @@ -5006,11 +5006,17 @@ public function find(string $collection, array $queries = []): array
throw new DatabaseException('Collection not found');
}

$attributes = $collection->getAttribute('attributes', []);
$indexes = $collection->getAttribute('indexes', []);

if ($this->validate) {
$validator = new DocumentsValidator($attributes, $indexes);
$collections[] = $collection;

$joins = Query::getByType($queries, [Query::TYPE_JOIN]);
var_dump($joins);
$collections = [];
foreach ($joins as $join) {
$collections[] = $this->silent(fn () => $this->getCollection($join->getCollection()));
}

$validator = new DocumentsValidator($collections);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
}
Expand Down
6 changes: 6 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ public static function getByType(array $queries, array $types): array
public static function groupByType(array $queries): array
{
$filters = [];
$joins = [];
$selections = [];
$limit = null;
$offset = null;
Expand Down Expand Up @@ -753,6 +754,10 @@ public static function groupByType(array $queries): array
$selections[] = clone $query;
break;

case Query::TYPE_JOIN:
$joins[] = clone $query;
break;

default:
$filters[] = clone $query;
break;
Expand All @@ -768,6 +773,7 @@ public static function groupByType(array $queries): array
'orderTypes' => $orderTypes,
'cursor' => $cursor,
'cursorDirection' => $cursorDirection,
'join' => $joins,
];
}

Expand Down
60 changes: 30 additions & 30 deletions src/Database/Validator/Queries/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@ class Documents extends IndexedQueries
* @param array<mixed> $indexes
* @throws Exception
*/
public function __construct(array $attributes, array $indexes)
public function __construct(array $collections)
{
$attributes[] = new Document([
'$id' => '$id',
'key' => '$id',
'type' => Database::VAR_STRING,
'array' => false,
]);
$attributes[] = new Document([
'$id' => '$internalId',
'key' => '$internalId',
'type' => Database::VAR_STRING,
'array' => false,
]);
$attributes[] = new Document([
'$id' => '$createdAt',
'key' => '$createdAt',
'type' => Database::VAR_DATETIME,
'array' => false,
]);
$attributes[] = new Document([
'$id' => '$updatedAt',
'key' => '$updatedAt',
'type' => Database::VAR_DATETIME,
'array' => false,
]);
// $attributes[] = new Document([
// '$id' => '$id',
// 'key' => '$id',
// 'type' => Database::VAR_STRING,
// 'array' => false,
// ]);
// $attributes[] = new Document([
// '$id' => '$internalId',
// 'key' => '$internalId',
// 'type' => Database::VAR_STRING,
// 'array' => false,
// ]);
// $attributes[] = new Document([
// '$id' => '$createdAt',
// 'key' => '$createdAt',
// 'type' => Database::VAR_DATETIME,
// 'array' => false,
// ]);
// $attributes[] = new Document([
// '$id' => '$updatedAt',
// 'key' => '$updatedAt',
// 'type' => Database::VAR_DATETIME,
// 'array' => false,
// ]);

$validators = [
new Limit(),
new Offset(),
new Cursor(),
new Filter($attributes),
new Order($attributes),
new Select($attributes),
new Join($attributes),
new Filter($collections),
new Order($collections),
new Select($collections),
new Join($collections),
];

parent::__construct($attributes, $indexes, $validators);
parent::__construct($collections, $validators);
}
}
Loading

0 comments on commit a87eed3

Please sign in to comment.