Skip to content

Commit 8d8691f

Browse files
BugFix: Validate and cast ids array for collections as strings
1 parent 1d7466c commit 8d8691f

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
php: [ 8.1, 8.2, 8.3 ]
1313
dependency-version: [ prefer-lowest, prefer-stable ]
1414

15-
name: Tests on PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
15+
name: Tests on PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
1616

1717
services:
1818
chroma:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "codewithkyrian/chromadb-php",
33
"description": "A PHP client for the Chroma Open Source Embedding Database",
4+
"keywords": ["chromadb", "php", "embedding", "database", "vectors", "semantic", "search", "chroma", "open-source"],
45
"type": "library",
56
"license": "MIT",
67
"require": {

src/Generated/Exceptions/ChromaException.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@ class ChromaException extends \Exception
1010

1111
public static function throwSpecific(string $message, string $type, int $code)
1212
{
13-
switch ($type) {
14-
case 'NotFoundError':
15-
throw new ChromaNotFoundException($message, $code);
16-
case 'ValueError':
17-
throw new ChromaValueException($message, $code);
18-
case 'UniqueConstraintError':
19-
throw new ChromaUniqueConstraintException($message, $code);
20-
case 'DimensionalityError':
21-
throw new ChromaDimensionalityException($message, $code);
22-
case 'TypeError':
23-
throw new ChromaTypeException($message, $code);
24-
default:
25-
throw new self($message, $code);
26-
}
13+
throw match ($type) {
14+
'NotFoundError' => new ChromaNotFoundException($message, $code),
15+
'ValueError' => new ChromaValueException($message, $code),
16+
'UniqueConstraintError' => new ChromaUniqueConstraintException($message, $code),
17+
'DimensionalityError' => new ChromaDimensionalityException($message, $code),
18+
'InvalidCollection' => new ChromaInvalidCollectionException($message, $code),
19+
'TypeError' => new ChromaTypeException($message, $code),
20+
default => new self($message, $code),
21+
};
2722
}
2823

2924
public static function inferTypeFromMessage(string $message): string
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace Codewithkyrian\ChromaDB\Generated\Exceptions;
7+
8+
class ChromaInvalidCollectionException extends ChromaException
9+
{
10+
11+
}

src/Resources/CollectionResource.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ function validate(
353353
bool $requireEmbeddingsOrDocuments
354354
): array
355355
{
356-
$finalEmbeddings = [];
357356

358357
if ($requireEmbeddingsOrDocuments) {
359358
if ($embeddings === null && $documents === null && $images === null) {
@@ -392,6 +391,14 @@ function validate(
392391
$finalEmbeddings = $embeddings;
393392
}
394393

394+
$ids = array_map(function ($id) {
395+
$id = (string)$id;
396+
if ($id === '') {
397+
throw new \InvalidArgumentException('Expected IDs to be non-empty strings');
398+
}
399+
return $id;
400+
}, $ids);
401+
395402
$uniqueIds = array_unique($ids);
396403
if (count($uniqueIds) !== count($ids)) {
397404
$duplicateIds = array_filter($ids, function ($id) use ($ids) {
@@ -400,6 +407,7 @@ function validate(
400407
throw new \InvalidArgumentException('Expected IDs to be unique, found duplicates for: ' . implode(', ', $duplicateIds));
401408
}
402409

410+
403411
return [
404412
'ids' => $ids,
405413
'embeddings' => $finalEmbeddings,

0 commit comments

Comments
 (0)