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

Allow enabling/disabling validation #349

Merged
merged 1 commit into from
Nov 27, 2023
Merged
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
119 changes: 82 additions & 37 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ class Database

protected bool $resolveRelationships = true;

protected bool $validate = true;

private int $relationshipFetchDepth = 1;

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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
*
Expand Down Expand Up @@ -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));
Expand All @@ -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());
}
}
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down