Skip to content

Commit 5f7b051

Browse files
authored
Merge pull request #331 from utopia-php/index-validation-no-attibute
Index validation no attibute
2 parents cabdd02 + 50e716c commit 5f7b051

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/Database/Validator/Index.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function getDescription(): string
3434
return $this->message;
3535
}
3636

37+
/**
38+
* @param Document $collection
39+
* @return bool
40+
*/
41+
public function checkAttributesNotFound(Document $collection): bool
42+
{
43+
foreach ($collection->getAttribute('indexes', []) as $index) {
44+
foreach ($index->getAttribute('attributes', []) as $attributeName) {
45+
if (!isset($this->attributes[$attributeName])) {
46+
$this->message = 'Invalid index attribute "' . $attributeName . '" not found';
47+
return false;
48+
}
49+
}
50+
}
51+
52+
return true;
53+
}
54+
3755
/**
3856
* @param Document $collection
3957
* @return bool
@@ -165,6 +183,10 @@ public function isValid($value): bool
165183
$this->attributes[$attribute->getAttribute('$id')] = $attribute;
166184
}
167185

186+
if (!$this->checkAttributesNotFound($value)) {
187+
return false;
188+
}
189+
168190
if (!$this->checkEmptyIndexAttributes($value)) {
169191
return false;
170192
}

tests/Database/Validator/IndexTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@ public function tearDown(): void
1919
{
2020
}
2121

22+
/**
23+
* @throws Exception
24+
*/
25+
public function testAttributeNotFound(): void
26+
{
27+
$validator = new Index(768);
28+
29+
$collection = new Document([
30+
'$id' => ID::custom('test'),
31+
'name' => 'test',
32+
'attributes' => [
33+
new Document([
34+
'$id' => ID::custom('title'),
35+
'type' => Database::VAR_STRING,
36+
'format' => '',
37+
'size' => 255,
38+
'signed' => true,
39+
'required' => false,
40+
'default' => null,
41+
'array' => false,
42+
'filters' => [],
43+
])
44+
],
45+
'indexes' => [
46+
new Document([
47+
'$id' => ID::custom('index1'),
48+
'type' => Database::INDEX_KEY,
49+
'attributes' => ['not_exist'],
50+
'lengths' => [],
51+
'orders' => [],
52+
]),
53+
],
54+
]);
55+
56+
$this->assertFalse($validator->isValid($collection));
57+
$this->assertEquals('Invalid index attribute "not_exist" not found', $validator->getDescription());
58+
}
59+
2260
/**
2361
* @throws Exception
2462
*/

0 commit comments

Comments
 (0)