From e12723c5315512b4bbe53f55e6bd81ea3725344b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 27 Oct 2023 16:55:11 +1300 Subject: [PATCH] Allow enabling/disabling validation --- src/Database/Database.php | 119 ++++++++++++++++++++++++++------------ tests/Database/Base.php | 51 ++++++++++++++++ 2 files changed, 133 insertions(+), 37 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 78fb9cfbd..d07c55f21 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -293,6 +293,8 @@ class Database protected bool $resolveRelationships = true; + protected bool $validate = true; + private int $relationshipFetchDepth = 1; /** @@ -620,12 +622,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; } /** @@ -639,6 +643,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 * @@ -750,9 +778,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)); @@ -770,13 +800,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()); + } } } @@ -830,9 +862,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)); @@ -2176,13 +2210,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); @@ -2267,9 +2302,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( @@ -2691,9 +2728,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); @@ -4143,9 +4182,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); @@ -4314,9 +4355,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); @@ -4359,9 +4402,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); diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 438d5bd53..e2c7e686b 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -12320,6 +12320,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');