Skip to content

Commit ee2d7d4

Browse files
authored
Merge pull request #854 from utopia-php/datetime-exception
feat: add StructureException handling for invalid datetime values in …
2 parents fad8e6b + 81d3b85 commit ee2d7d4

2 files changed

Lines changed: 64 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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5801,6 +5801,64 @@ 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+
5809+
if (!$database->getAdapter()->getSupportForAttributes()) {
5810+
$this->expectNotToPerformAssertions();
5811+
return;
5812+
}
5813+
5814+
$collection = 'invalid_date_attributes';
5815+
5816+
$database->createCollection($collection);
5817+
$this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false));
5818+
5819+
$database->setPreserveDates(true);
5820+
5821+
try {
5822+
// Outside allowed year range (Structure uses DatetimeValidator min/max, e.g. 0000–9999).
5823+
$invalidDate = '10000-01-01T00:00:00.000+00:00';
5824+
5825+
try {
5826+
$database->createDocument($collection, new Document([
5827+
'$id' => 'doc1',
5828+
'$permissions' => [
5829+
Permission::read(Role::any()),
5830+
Permission::update(Role::any()),
5831+
],
5832+
'$createdAt' => $invalidDate,
5833+
]));
5834+
$this->fail('Expected StructureException for invalid $createdAt');
5835+
} catch (Throwable $e) {
5836+
$this->assertInstanceOf(StructureException::class, $e);
5837+
}
5838+
5839+
$database->createDocument($collection, new Document([
5840+
'$id' => 'doc2',
5841+
'$permissions' => [
5842+
Permission::read(Role::any()),
5843+
Permission::update(Role::any()),
5844+
],
5845+
'string' => 'x',
5846+
]));
5847+
5848+
try {
5849+
$database->updateDocument($collection, 'doc2', new Document([
5850+
'$updatedAt' => $invalidDate,
5851+
]));
5852+
$this->fail('Expected StructureException for invalid $updatedAt');
5853+
} catch (Throwable $e) {
5854+
$this->assertInstanceOf(StructureException::class, $e);
5855+
}
5856+
} finally {
5857+
$database->setPreserveDates(false);
5858+
$database->deleteCollection($collection);
5859+
}
5860+
}
5861+
58045862
public function testSingleDocumentDateOperations(): void
58055863
{
58065864
/** @var Database $database */

0 commit comments

Comments
 (0)