Skip to content

Commit 1fcc7c7

Browse files
chore: make field validation logic easier to follow
1 parent 7a4644f commit 1fcc7c7

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

src/common/search/vectorSearchEmbeddingsManager.ts

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,26 @@ export class VectorSearchEmbeddingsManager {
185185
error: details.error ?? "not-a-vector",
186186
});
187187

188+
const extractUnderlyingVector = (fieldRef: unknown): ArrayLike<unknown> | undefined => {
189+
if (fieldRef instanceof BSON.Binary) {
190+
try {
191+
return fieldRef.toFloat32Array();
192+
} catch {
193+
try {
194+
return fieldRef.toBits();
195+
} catch {
196+
return undefined;
197+
}
198+
}
199+
}
200+
201+
if (Array.isArray(fieldRef)) {
202+
return fieldRef as Array<unknown>;
203+
}
204+
205+
return undefined;
206+
};
207+
188208
for (const field of fieldPath) {
189209
if (fieldRef && typeof fieldRef === "object" && field in fieldRef) {
190210
fieldRef = (fieldRef as Record<string, unknown>)[field];
@@ -193,55 +213,25 @@ export class VectorSearchEmbeddingsManager {
193213
}
194214
}
195215

196-
if (fieldRef instanceof BSON.Binary) {
197-
try {
198-
const elements = fieldRef.toFloat32Array();
199-
if (elements.length !== definition.numDimensions) {
200-
return constructError({
201-
actualNumDimensions: elements.length,
202-
error: "dimension-mismatch",
203-
});
204-
}
205-
206-
return undefined;
207-
} catch {
208-
// bits are also supported
209-
try {
210-
const bits = fieldRef.toBits();
211-
if (bits.length !== definition.numDimensions) {
212-
return constructError({
213-
actualNumDimensions: bits.length,
214-
error: "dimension-mismatch",
215-
});
216-
}
217-
218-
return undefined;
219-
} catch {
220-
return constructError({
221-
error: "not-a-vector",
222-
});
223-
}
224-
}
225-
} else {
226-
if (!Array.isArray(fieldRef)) {
227-
return constructError({
228-
error: "not-a-vector",
229-
});
230-
}
216+
const maybeVector = extractUnderlyingVector(fieldRef);
217+
if (!maybeVector) {
218+
return constructError({
219+
error: "not-a-vector",
220+
});
221+
}
231222

232-
if (fieldRef.length !== definition.numDimensions) {
233-
return constructError({
234-
actualNumDimensions: fieldRef.length,
235-
error: "dimension-mismatch",
236-
});
237-
}
223+
if (maybeVector.length !== definition.numDimensions) {
224+
return constructError({
225+
actualNumDimensions: maybeVector.length,
226+
error: "dimension-mismatch",
227+
});
228+
}
238229

239-
if (fieldRef.some((e) => !this.isANumber(e))) {
240-
return constructError({
241-
actualNumDimensions: fieldRef.length,
242-
error: "not-numeric",
243-
});
244-
}
230+
if (Array.isArray(maybeVector) && maybeVector.some((e) => !this.isANumber(e))) {
231+
return constructError({
232+
actualNumDimensions: maybeVector.length,
233+
error: "not-numeric",
234+
});
245235
}
246236

247237
return undefined;

tests/integration/tools/mongodb/create/insertMany.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describeWithMongoDB(
170170
expect(docCount).toBe(1);
171171
});
172172

173-
it("returns an error when there is a search index and quantisation is wrong", async () => {
173+
it("returns an error when there is a search index and embeddings parameter are wrong", async () => {
174174
await createVectorSearchIndexAndWait(integration.mongoClient(), database, "test", [
175175
{
176176
type: "vector",

0 commit comments

Comments
 (0)