Skip to content

Commit 94bec5e

Browse files
committed
feat: Filter invalid ObjectId constructor types in getIds
1 parent b00d711 commit 94bec5e

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/__tests__/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ describe('utils', () => {
404404
['123456789012345678900014', new ObjectId('123456789012345678900088')],
405405
[new ObjectId('123456789012345678900014'), new ObjectId('123456789012345678900088')],
406406
],
407+
[
408+
'invalid',
409+
['123', '123456789012345678900021'],
410+
[new ObjectId('123456789012345678900021')]
411+
],
407412
])('%s', (_name, input, expected) => {
408413
expect.assertions(4);
409414

src/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ export type RequireAtLeastOne<TObj, Keys extends keyof TObj = keyof TObj> = {
196196

197197
export type ObjectIdConstructorParameter = ConstructorParameters<typeof ObjectId>[0];
198198
export function getIds(ids: Iterable<ObjectIdConstructorParameter>): ObjectId[] {
199-
return Array.from(ids).map((id) => new ObjectId(id));
199+
return Array.from(ids).flatMap((id) => {
200+
try {
201+
return new ObjectId(id);
202+
} catch {
203+
return [];
204+
}
205+
});
200206
}
201207

202208
/**

0 commit comments

Comments
 (0)