diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 07c5b3688..4ebc4f98a 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -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 @@ -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; } diff --git a/tests/Database/Validator/IndexTest.php b/tests/Database/Validator/IndexTest.php index 7d4d86ee6..4247ecd32 100644 --- a/tests/Database/Validator/IndexTest.php +++ b/tests/Database/Validator/IndexTest.php @@ -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 */