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
22 changes: 22 additions & 0 deletions src/Database/Validator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public function getDescription(): string
return $this->message;
}

/**
* @param Document $collection
* @return bool
*/
public function checkAttributesNotFound(Document $collection): bool
{
foreach ($collection->getAttribute('indexes', []) as $index) {
foreach ($index->getAttribute('attributes', []) as $attributeName) {
if (!isset($this->attributes[$attributeName])) {
$this->message = 'Invalid index attribute "' . $attributeName . '" not found';
return false;
}
}
}

return true;
}

/**
* @param Document $collection
* @return bool
Expand Down Expand Up @@ -165,6 +183,10 @@ public function isValid($value): bool
$this->attributes[$attribute->getAttribute('$id')] = $attribute;
}

if (!$this->checkAttributesNotFound($value)) {
return false;
}

if (!$this->checkEmptyIndexAttributes($value)) {
return false;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Database/Validator/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,44 @@ public function tearDown(): void
{
}

/**
* @throws Exception
*/
public function testAttributeNotFound(): void
{
$validator = new Index(768);

$collection = new Document([
'$id' => ID::custom('test'),
'name' => 'test',
'attributes' => [
new Document([
'$id' => ID::custom('title'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
])
],
'indexes' => [
new Document([
'$id' => ID::custom('index1'),
'type' => Database::INDEX_KEY,
'attributes' => ['not_exist'],
'lengths' => [],
'orders' => [],
]),
],
]);

$this->assertFalse($validator->isValid($collection));
$this->assertEquals('Invalid index attribute "not_exist" not found', $validator->getDescription());
}

/**
* @throws Exception
*/
Expand Down