Skip to content

Commit cc6538e

Browse files
authored
Merge pull request #582 from ArnabChatterjee20k/feat/index-length
Feat/index length
2 parents 92f067f + 86c8f17 commit cc6538e

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/Database/Validator/Index.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,12 @@ public function checkIndexLength(Document $index): bool
176176

177177
$total = 0;
178178
$lengths = $index->getAttribute('lengths', []);
179-
180-
foreach ($index->getAttribute('attributes', []) as $attributePosition => $attributeName) {
179+
$attributes = $index->getAttribute('attributes', []);
180+
if (count($lengths) > count($attributes)) {
181+
$this->message = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.';
182+
return false;
183+
}
184+
foreach ($attributes as $attributePosition => $attributeName) {
181185
$attribute = $this->attributes[\strtolower($attributeName)];
182186

183187
switch ($attribute->getAttribute('type')) {
@@ -194,6 +198,10 @@ public function checkIndexLength(Document $index): bool
194198
$indexLength = 1;
195199
break;
196200
}
201+
if ($indexLength < 0) {
202+
$this->message = 'Negative index length provided for ' . $attributeName;
203+
return false;
204+
}
197205

198206
if ($attribute->getAttribute('array', false)) {
199207
$attributeSize = Database::ARRAY_INDEX_LENGTH;

tests/e2e/Adapter/Scopes/IndexTests.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,49 @@ public function testIndexValidation(): void
243243
} catch (Exception $e) {
244244
$this->assertEquals($errorMessage, $e->getMessage());
245245
}
246+
247+
248+
$indexes = [
249+
new Document([
250+
'$id' => ID::custom('index_negative_length'),
251+
'type' => Database::INDEX_KEY,
252+
'attributes' => ['title1'],
253+
'lengths' => [-1],
254+
'orders' => [],
255+
]),
256+
];
257+
258+
$errorMessage = 'Negative index length provided for title1';
259+
$this->assertFalse($validator->isValid($indexes[0]));
260+
$this->assertEquals($errorMessage, $validator->getDescription());
261+
262+
try {
263+
static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes);
264+
$this->fail('Failed to throw exception');
265+
} catch (Exception $e) {
266+
$this->assertEquals($errorMessage, $e->getMessage());
267+
}
268+
269+
$indexes = [
270+
new Document([
271+
'$id' => ID::custom('index_extra_lengths'),
272+
'type' => Database::INDEX_KEY,
273+
'attributes' => ['title1', 'title2'],
274+
'lengths' => [100, 100, 100],
275+
'orders' => [],
276+
]),
277+
];
278+
279+
$errorMessage = 'Invalid index lengths. Count of lengths must be equal or less than the number of attributes.';
280+
$this->assertFalse($validator->isValid($indexes[0]));
281+
$this->assertEquals($errorMessage, $validator->getDescription());
282+
283+
try {
284+
static::getDatabase()->createCollection(ID::unique(), $attributes, $indexes);
285+
$this->fail('Failed to throw exception');
286+
} catch (Exception $e) {
287+
$this->assertEquals($errorMessage, $e->getMessage());
288+
}
246289
}
247290

248291
public function testRenameIndex(): void

0 commit comments

Comments
 (0)