A minimal package to test whether a string is a valid MongoDB ObjectId representation. Version 0.0.2, no recent updates. It simply checks if the string is 24 hex characters. Unlike mongoose's isValid, which can accept Buffer, ObjectId, etc., this package expects a string argument. Limited to ES5/CJS, no TypeScript support, no ESM. A lightweight alternative to mongoose.Types.ObjectId.isValid for string-only validation.
npm install valid-objectidNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Basic usage of isValid to validate MongoDB ObjectId strings.
Always convert inputs to string before calling isValid, for example: isValid(String(id)).
Add a declaration file: declare module 'valid-objectid' { export function isValid(str: string): boolean; }Replace with mongoose.Types.ObjectId.isValid if mongoose is already in your project.
Add explicit type checking before calling isValid: if (typeof id !== 'string') throw new Error('Invalid type');Use: const validObjectid = require('valid-objectid'); const isValid = validObjectid.isValid;Use require instead of import, or use a bundler that handles CJS interop.
Pass the argument as a string: isValid('507f1f77bcf86cd799439011')No dependency data recorded yet.