Registry / storage / valid-objectid

valid-objectid

JSON →
library0.0.2jsnpmunverified

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-objectid
INSTALL
IMPORT
SIG · VALID-OBJECTID
V
valid-objectid
storagejavascriptv0.0.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isValid
import { isValid } from 'valid-objectid'
import validObjectid from 'valid-objectid'
The library does not export a default; only named export isValid. ESM imports require Node >= 12 or a bundler.
isValid (CommonJS)
const { isValid } = require('valid-objectid')
const validObjectid = require('valid-objectid')
Using require returns an object with isValid property. Destructure or use require().isValid.
isValid (via property access)
const isValid = require('valid-objectid').isValid;
const isValid = require('valid-objectid/isValid');
The package does not expose subpath exports. Only the main entry point works.

Basic usage of isValid to validate MongoDB ObjectId strings.

import { isValid } from 'valid-objectid'; console.log(isValid('507f1f77bcf86cd799439011')); // true console.log(isValid('invalid')); // false console.log(isValid('')); // false console.log(isValid('507f1f77bcf86cd79943901')); // false (only 23 chars) console.log(isValid('507f1f77bcf86cd7994390111')); // false (25 chars) console.log(isValid('507f1f77bcf86cd79943901z')); // false (non-hex) console.log(isValid('507f1f77bcf86cd799439011')); // true
Debug
Known issues
gotchaThe function only accepts strings; passing numbers or objects returns false silently.
fix
Always convert inputs to string before calling isValid, for example: isValid(String(id)).
affects: >=0.0.0
gotchaNo TypeScript definitions included. TypeScript users must create their own or use @types/valid-objectid if available.
fix
Add a declaration file: declare module 'valid-objectid' { export function isValid(str: string): boolean; }
affects: >=0.0.0
deprecatedPackage has not been updated since 2016. No ESM support, no modern features. Consider using mongoose.Types.ObjectId.isValid for full compatibility.
fix
Replace with mongoose.Types.ObjectId.isValid if mongoose is already in your project.
affects: >=0.0.0
gotchaisValid returns false for null, undefined, or non-string types without throwing an error, which may mask programming errors.
fix
Add explicit type checking before calling isValid: if (typeof id !== 'string') throw new Error('Invalid type');
affects: >=0.0.0
Errors
Common errors & fixes
const { isValid } = require('valid-objectid'); TypeError: isValid is not a function
The require call returns an object, but destructuring may fail if the package is imported incorrectly.
fix
Use: const validObjectid = require('valid-objectid'); const isValid = validObjectid.isValid;
import { isValid } from 'valid-objectid'; SyntaxError: Cannot use import statement outside a module
The package does not provide an ESM build; it's CommonJS only.
fix
Use require instead of import, or use a bundler that handles CJS interop.
isValid(507f1f77bcf86cd799439011) returns false even though the string is valid
The argument is a number, not a string. The function expects a string.
fix
Pass the argument as a string: isValid('507f1f77bcf86cd799439011')
Upgrade
Version history
0.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
1
Resources
valid-objectid — npm install valid-objectid · libregistry