objectid-utils.ts 432 B

1234567891011121314
  1. import ObjectId from 'bson-objectid';
  2. export function isValidObjectId(id: string | ObjectId | null | undefined): boolean {
  3. if (id == null) {
  4. return false;
  5. }
  6. // implement according to https://www.geeksforgeeks.org/how-to-check-if-a-string-is-valid-mongodb-objectid-in-node-js/
  7. if (typeof id === 'string') {
  8. return ObjectId.isValid(id) && new ObjectId(id).toHexString() === id;
  9. }
  10. return ObjectId.isValid(id);
  11. }