Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Utopia\Database\Document;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Transaction as TransactionException;
use Utopia\Database\Exception\Type as TypeException;
Expand Down Expand Up @@ -1414,7 +1415,11 @@ public function castingBefore(Document $collection, Document $document): Documen
switch ($type) {
case Database::VAR_DATETIME:
if (!($node instanceof UTCDateTime)) {
$node = new UTCDateTime(new \DateTime($node));
try {
$node = new UTCDateTime(new \DateTime($node));
} catch (\Throwable $e) {
throw new StructureException('Invalid datetime value for attribute "' . $key . '": ' . $e->getMessage());
}
}
break;
case Database::VAR_OBJECT:
Expand Down
57 changes: 57 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5801,6 +5801,63 @@ public function testDateTimeDocument(): void
$database->deleteCollection($collection);
}

public function testInvalidCreatedAndUpdatedAtThrowStructureException(): void
{
/** @var Database $database */
$database = $this->getDatabase();
$collection = 'invalid_date_attributes';

$database->createCollection($collection);
$this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false));

$invalidDates = [
'not-a-date',
'2026-13-01T00:00:00.000+00:00',
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
];

$database->setPreserveDates(true);

try {
foreach ($invalidDates as $index => $invalidDate) {
foreach (['$createdAt', '$updatedAt'] as $attribute) {
try {
if ($attribute === '$createdAt') {
$database->createDocument($collection, new Document([
'$id' => $attribute . '-' . $index,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
'$permissions' => [
Permission::read(Role::any()),
Permission::write(Role::any()),
Permission::update(Role::any()),
],
'string' => 'invalid-date',
$attribute => $invalidDate,
]));
} else {
$document = $database->createDocument($collection, new Document([
'$id' => 'doc-' . $index,
'$permissions' => [
Permission::read(Role::any()),
Permission::write(Role::any()),
Permission::update(Role::any()),
],
'string' => 'valid-date',
]));
$document->setAttribute($attribute, $invalidDate);
$database->updateDocument($collection, $document->getId(), $document);
}

$this->fail('Expected StructureException for invalid ' . $attribute);
} catch (Throwable $e) {
$this->assertInstanceOf(StructureException::class, $e);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
}
}
} finally {
$database->setPreserveDates(false);
$database->deleteCollection($collection);
}
}

public function testSingleDocumentDateOperations(): void
{
/** @var Database $database */
Expand Down
Loading