Skip to content

Commit 615a530

Browse files
feat: add StructureException handling for invalid datetime values in Mongo adapter and corresponding tests
1 parent fad8e6b commit 615a530

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/Database/Adapter/Mongo.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Utopia\Database\Document;
1414
use Utopia\Database\Exception as DatabaseException;
1515
use Utopia\Database\Exception\Duplicate as DuplicateException;
16+
use Utopia\Database\Exception\Structure as StructureException;
1617
use Utopia\Database\Exception\Timeout as TimeoutException;
1718
use Utopia\Database\Exception\Transaction as TransactionException;
1819
use Utopia\Database\Exception\Type as TypeException;
@@ -1414,7 +1415,11 @@ public function castingBefore(Document $collection, Document $document): Documen
14141415
switch ($type) {
14151416
case Database::VAR_DATETIME:
14161417
if (!($node instanceof UTCDateTime)) {
1417-
$node = new UTCDateTime(new \DateTime($node));
1418+
try {
1419+
$node = new UTCDateTime(new \DateTime($node));
1420+
} catch (\Throwable $e) {
1421+
throw new StructureException('Invalid datetime value for attribute "' . $key . '": ' . $e->getMessage());
1422+
}
14181423
}
14191424
break;
14201425
case Database::VAR_OBJECT:

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5801,6 +5801,63 @@ public function testDateTimeDocument(): void
58015801
$database->deleteCollection($collection);
58025802
}
58035803

5804+
public function testInvalidCreatedAndUpdatedAtThrowStructureException(): void
5805+
{
5806+
/** @var Database $database */
5807+
$database = $this->getDatabase();
5808+
$collection = 'invalid_date_attributes';
5809+
5810+
$database->createCollection($collection);
5811+
$this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false));
5812+
5813+
$invalidDates = [
5814+
'not-a-date',
5815+
'2026-13-01T00:00:00.000+00:00',
5816+
];
5817+
5818+
$database->setPreserveDates(true);
5819+
5820+
try {
5821+
foreach ($invalidDates as $index => $invalidDate) {
5822+
foreach (['$createdAt', '$updatedAt'] as $attribute) {
5823+
try {
5824+
if ($attribute === '$createdAt') {
5825+
$database->createDocument($collection, new Document([
5826+
'$id' => $attribute . '-' . $index,
5827+
'$permissions' => [
5828+
Permission::read(Role::any()),
5829+
Permission::write(Role::any()),
5830+
Permission::update(Role::any()),
5831+
],
5832+
'string' => 'invalid-date',
5833+
$attribute => $invalidDate,
5834+
]));
5835+
} else {
5836+
$document = $database->createDocument($collection, new Document([
5837+
'$id' => 'doc-' . $index,
5838+
'$permissions' => [
5839+
Permission::read(Role::any()),
5840+
Permission::write(Role::any()),
5841+
Permission::update(Role::any()),
5842+
],
5843+
'string' => 'valid-date',
5844+
]));
5845+
$document->setAttribute($attribute, $invalidDate);
5846+
$database->updateDocument($collection, $document->getId(), $document);
5847+
}
5848+
5849+
$this->fail('Expected StructureException for invalid ' . $attribute);
5850+
} catch (Throwable $e) {
5851+
$this->assertInstanceOf(StructureException::class, $e);
5852+
}
5853+
}
5854+
}
5855+
} finally {
5856+
$database->setPreserveDates(false);
5857+
$database->deleteCollection($collection);
5858+
}
5859+
}
5860+
58045861
public function testSingleDocumentDateOperations(): void
58055862
{
58065863
/** @var Database $database */

0 commit comments

Comments
 (0)