{"id":44098,"library":"valid-objectid","title":"valid-objectid","description":"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.","status":"maintenance","version":"0.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/cnkdynamics/valid-objectid","tags":["javascript","mongoose","ObjectId","_id","validation"],"install":[{"cmd":"npm install valid-objectid","lang":"bash","label":"npm"},{"cmd":"yarn add valid-objectid","lang":"bash","label":"yarn"},{"cmd":"pnpm add valid-objectid","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library does not export a default; only named export isValid. ESM imports require Node >= 12 or a bundler.","wrong":"import validObjectid from 'valid-objectid'","symbol":"isValid","correct":"import { isValid } from 'valid-objectid'"},{"note":"Using require returns an object with isValid property. Destructure or use require().isValid.","wrong":"const validObjectid = require('valid-objectid')","symbol":"isValid (CommonJS)","correct":"const { isValid } = require('valid-objectid')"},{"note":"The package does not expose subpath exports. Only the main entry point works.","wrong":"const isValid = require('valid-objectid/isValid');","symbol":"isValid (via property access)","correct":"const isValid = require('valid-objectid').isValid;"}],"quickstart":{"code":"import { isValid } from 'valid-objectid';\n\nconsole.log(isValid('507f1f77bcf86cd799439011')); // true\nconsole.log(isValid('invalid')); // false\nconsole.log(isValid('')); // false\nconsole.log(isValid('507f1f77bcf86cd79943901')); // false (only 23 chars)\nconsole.log(isValid('507f1f77bcf86cd7994390111')); // false (25 chars)\nconsole.log(isValid('507f1f77bcf86cd79943901z')); // false (non-hex)\nconsole.log(isValid('507f1f77bcf86cd799439011')); // true","lang":"javascript","description":"Basic usage of isValid to validate MongoDB ObjectId strings."},"warnings":[{"fix":"Always convert inputs to string before calling isValid, for example: isValid(String(id)).","message":"The function only accepts strings; passing numbers or objects returns false silently.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Add a declaration file: declare module 'valid-objectid' { export function isValid(str: string): boolean; }","message":"No TypeScript definitions included. TypeScript users must create their own or use @types/valid-objectid if available.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Replace with mongoose.Types.ObjectId.isValid if mongoose is already in your project.","message":"Package has not been updated since 2016. No ESM support, no modern features. Consider using mongoose.Types.ObjectId.isValid for full compatibility.","severity":"deprecated","affected_versions":">=0.0.0"},{"fix":"Add explicit type checking before calling isValid: if (typeof id !== 'string') throw new Error('Invalid type');","message":"isValid returns false for null, undefined, or non-string types without throwing an error, which may mask programming errors.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'0.0.2':19 '24':30 'accept':39 'altern':59 'argument':48 'buffer':40 'charact':32 'check':25 'es5/cjs':51 'esm':56 'etc':42 'expect':45 'hex':31 'id':70 'isvalid':36 'javascript':67 'lightweight':58 'limit':49 'minim':5 'mongodb':15 'mongoos':34,68 'mongoose.types.objectid.isvalid':61 'objectid':3,16,41,69 'packag':6,44 'recent':21 'represent':17 'simpli':24 'string':11,28,47,64 'string-on':63 'support':54 'test':8 'typescript':53 'unlik':33 'updat':22 'valid':2,14,66,71 'valid-objectid':1 'version':18 'whether':9","created_at":"2026-06-05T17:02:58.419466+00:00","updated_at":"2026-06-05T17:02:58.419466+00:00","problems":[{"fix":"Use: const validObjectid = require('valid-objectid'); const isValid = validObjectid.isValid;","cause":"The require call returns an object, but destructuring may fail if the package is imported incorrectly.","error":"const { isValid } = require('valid-objectid'); TypeError: isValid is not a function"},{"fix":"Use require instead of import, or use a bundler that handles CJS interop.","cause":"The package does not provide an ESM build; it's CommonJS only.","error":"import { isValid } from 'valid-objectid'; SyntaxError: Cannot use import statement outside a module"},{"fix":"Pass the argument as a string: isValid('507f1f77bcf86cd799439011')","cause":"The argument is a number, not a string. The function expects a string.","error":"isValid(507f1f77bcf86cd799439011) returns false even though the string is valid"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/cnkdynamics/valid-objectid#readme","github":"https://github.com/cnkdynamics/valid-objectid","docs":null,"changelog":null,"pypi":null,"npm":"valid-objectid","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}