Skip to content

Commit

Permalink
Merge pull request #349 from utopia-php/feat-enable-disable-validation
Browse files Browse the repository at this point in the history
Allow enabling/disabling validation
  • Loading branch information
abnegate authored Nov 27, 2023
2 parents 60a61d0 + e12723c commit 4691f62
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 37 deletions.
119 changes: 82 additions & 37 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ class Database

protected bool $resolveRelationships = true;

protected bool $validate = true;

private int $relationshipFetchDepth = 1;

/**
Expand Down Expand Up @@ -624,12 +626,14 @@ public function resetMetadata(): void
*
* @param int $milliseconds
* @param string $event
* @return void
* @return self
* @throws Exception
*/
public function setTimeout(int $milliseconds, string $event = Database::EVENT_ALL): void
public function setTimeout(int $milliseconds, string $event = Database::EVENT_ALL): self
{
$this->adapter->setTimeout($milliseconds, $event);

return $this;
}

/**
Expand All @@ -643,6 +647,30 @@ public function clearTimeout(string $event = Database::EVENT_ALL): void
$this->adapter->clearTimeout($event);
}

/**
* Enable validation
*
* @return $this
*/
public function enableValidation(): self
{
$this->validate = true;

return $this;
}

/**
* Disable validation
*
* @return $this
*/
public function disableValidation(): self
{
$this->validate = false;

return $this;
}

/**
* Ping Database
*
Expand Down Expand Up @@ -754,9 +782,11 @@ public function createCollection(string $id, array $attributes = [], array $inde
Permission::create(Role::any()),
];

$validator = new Permissions();
if (!$validator->isValid($permissions)) {
throw new InvalidArgumentException($validator->getDescription());
if ($this->validate) {
$validator = new Permissions();
if (!$validator->isValid($permissions)) {
throw new InvalidArgumentException($validator->getDescription());
}
}

$collection = $this->silent(fn () => $this->getCollection($id));
Expand All @@ -774,13 +804,15 @@ public function createCollection(string $id, array $attributes = [], array $inde
'documentSecurity' => $documentSecurity
]);

$validator = new IndexValidator(
$attributes,
$this->adapter->getMaxIndexLength()
);
foreach ($indexes as $index) {
if (!$validator->isValid($index)) {
throw new DatabaseException($validator->getDescription());
if ($this->validate) {
$validator = new IndexValidator(
$attributes,
$this->adapter->getMaxIndexLength()
);
foreach ($indexes as $index) {
if (!$validator->isValid($index)) {
throw new DatabaseException($validator->getDescription());
}
}
}

Expand Down Expand Up @@ -834,9 +866,11 @@ public function createCollection(string $id, array $attributes = [], array $inde
*/
public function updateCollection(string $id, array $permissions, bool $documentSecurity): Document
{
$validator = new Permissions();
if (!$validator->isValid($permissions)) {
throw new InvalidArgumentException($validator->getDescription());
if ($this->validate) {
$validator = new Permissions();
if (!$validator->isValid($permissions)) {
throw new InvalidArgumentException($validator->getDescription());
}
}

$collection = $this->silent(fn () => $this->getCollection($id));
Expand Down Expand Up @@ -2180,13 +2214,14 @@ public function createIndex(string $collection, string $id, string $type, array

$collection->setAttribute('indexes', $index, Document::SET_TYPE_APPEND);

$validator = new IndexValidator(
$collection->getAttribute('attributes', []),
$this->adapter->getMaxIndexLength()
);

if (!$validator->isValid($index)) {
throw new DatabaseException($validator->getDescription());
if ($this->validate) {
$validator = new IndexValidator(
$collection->getAttribute('attributes', []),
$this->adapter->getMaxIndexLength()
);
if (!$validator->isValid($index)) {
throw new DatabaseException($validator->getDescription());
}
}

$index = $this->adapter->createIndex($collection->getId(), $id, $type, $attributes, $lengths, $orders);
Expand Down Expand Up @@ -2271,9 +2306,11 @@ public function getDocument(string $collection, string $id, array $queries = [])

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

$validator = new DocumentValidator($attributes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
if ($this->validate) {
$validator = new DocumentValidator($attributes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
}
}

$relationships = \array_filter(
Expand Down Expand Up @@ -2695,9 +2732,11 @@ public function createDocument(string $collection, Document $document): Document

$document = $this->encode($collection, $document);

$validator = new Permissions();
if (!$validator->isValid($document->getPermissions())) {
throw new InvalidArgumentException($validator->getDescription());
if ($this->validate) {
$validator = new Permissions();
if (!$validator->isValid($document->getPermissions())) {
throw new InvalidArgumentException($validator->getDescription());
}
}

$structure = new Structure($collection);
Expand Down Expand Up @@ -4263,9 +4302,11 @@ public function find(string $collection, array $queries = []): array
$attributes = $collection->getAttribute('attributes', []);
$indexes = $collection->getAttribute('indexes', []);

$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
if ($this->validate) {
$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
}
}

$authorization = new Authorization(self::PERMISSION_READ);
Expand Down Expand Up @@ -4434,9 +4475,11 @@ public function count(string $collection, array $queries = [], ?int $max = null)
$attributes = $collection->getAttribute('attributes', []);
$indexes = $collection->getAttribute('indexes', []);

$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
if ($this->validate) {
$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
}
}

$authorization = new Authorization(self::PERMISSION_READ);
Expand Down Expand Up @@ -4479,9 +4522,11 @@ public function sum(string $collection, string $attribute, array $queries = [],
$attributes = $collection->getAttribute('attributes', []);
$indexes = $collection->getAttribute('indexes', []);

$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
if ($this->validate) {
$validator = new DocumentsValidator($attributes, $indexes);
if (!$validator->isValid($queries)) {
throw new QueryException($validator->getDescription());
}
}

$queries = self::convertQueries($collection, $queries);
Expand Down
51 changes: 51 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -12449,6 +12449,57 @@ public function testLabels(): void
$this->assertCount(1, $documents);
}

public function testEnableDisableValidation(): void
{
$database = static::getDatabase();

$database->createCollection('validation', permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any())
]);

$database->createAttribute(
'validation',
'name',
Database::VAR_STRING,
10,
false
);

$database->createDocument('validation', new Document([
'$id' => 'docwithmorethan36charsasitsidentifier',
'name' => 'value1',
]));

try {
$database->find('validation', queries: [
Query::equal('$id', ['docwithmorethan36charsasitsidentifier']),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
}

$database->disableValidation();

$database->find('validation', queries: [
Query::equal('$id', ['docwithmorethan36charsasitsidentifier']),
]);

$database->enableValidation();

try {
$database->find('validation', queries: [
Query::equal('$id', ['docwithmorethan36charsasitsidentifier']),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
}
}

public function testMetadata(): void
{
static::getDatabase()->setMetadata('key', 'value');
Expand Down

0 comments on commit 4691f62

Please sign in to comment.