Skip to content
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
9 changes: 9 additions & 0 deletions src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ protected function checkForAllRequiredValues(array $structure, array $attributes
$keys[$name] = $attribute; // List of allowed attributes to help find unknown ones

if ($required && !isset($structure[$name])) {
// Documents stored before the attribute became required hold null, and may keep it
if ($this->currentDocument !== null && $this->currentDocument->getAttribute($name) === null) {
continue;
}

$this->message = 'Missing required attribute "'.$name.'"';
return false;
}
Expand Down Expand Up @@ -333,6 +338,10 @@ protected function checkForInvalidAttributeValues(Document $document, array $str
continue;
}

if (is_null($value) && $this->currentDocument !== null && $this->currentDocument->getAttribute($key) === null) {
continue;
}

if ($type === Database::VAR_RELATIONSHIP) {
continue;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Validator/StructureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ public function testRequiredKeys(): void
$this->assertEquals('Invalid document structure: Missing required attribute "title"', $validator->getDescription());
}

public function testRequiredKeyStoredAsNull(): void
{
$document = [
'$collection' => ID::custom('posts'),
'description' => 'Demo description',
'rating' => 5,
'price' => 1.99,
'published' => true,
'feedback' => 'team@appwrite.io',
'$createdAt' => '2000-04-01T12:00:00.000+00:00',
'$updatedAt' => '2000-04-01T12:00:00.000+00:00'
];

$validator = new Structure(
new Document($this->collection),
Database::VAR_INTEGER,
currentDocument: new Document(['title' => null])
);

$this->assertTrue($validator->isValid(new Document($document)), $validator->getDescription());
$this->assertTrue($validator->isValid(new Document([...$document, 'title' => null])), $validator->getDescription());

$validator = new Structure(
new Document($this->collection),
Database::VAR_INTEGER,
currentDocument: new Document(['title' => 'My Title'])
);

$this->assertFalse($validator->isValid(new Document([...$document, 'title' => null])));
$this->assertEquals('Invalid document structure: Missing required attribute "title"', $validator->getDescription());
}

public function testNullValues(): void
{
$validator = new Structure(
Expand Down
Loading