diff --git a/src/Database/Document.php b/src/Database/Document.php index f605f615f..9d41f171b 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -4,6 +4,7 @@ use ArrayObject; use Utopia\Database\Exception as DatabaseException; +use Utopia\Database\Exception\Structure as StructureException; /** * @extends ArrayObject @@ -26,8 +27,12 @@ class Document extends ArrayObject */ public function __construct(array $input = []) { + if (isset($input['$id']) && !\is_string($input['$id'])) { + throw new StructureException('$id must be of type string'); + } + if (isset($input['$permissions']) && !is_array($input['$permissions'])) { - throw new DatabaseException('$permissions must be of type array'); + throw new StructureException('$permissions must be of type array'); } foreach ($input as $key => &$value) { diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index e100ee69e..467410cc3 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -3122,6 +3122,14 @@ public function testFind(): array $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'with-dash', Database::VAR_STRING, 128, true)); $this->assertEquals(true, static::getDatabase()->createAttribute('movies', 'nullable', Database::VAR_STRING, 128, false)); + try { + static::getDatabase()->createDocument('movies', new Document(['$id' => ['id_as_array']])); + $this->fail('Failed to throw exception'); + } catch (Throwable $e) { + $this->assertEquals('$id must be of type string', $e->getMessage()); + $this->assertInstanceOf(StructureException::class, $e); + } + $document = static::getDatabase()->createDocument('movies', new Document([ '$id' => ID::custom('frozen'), '$permissions' => [