A lightweight security library (v2.2.0) that sanitizes JavaScript objects to prevent MongoDB query selector injection attacks by stripping or replacing dangerous keys starting with '$' (e.g., $gt, $ne, $where). It provides an Express middleware (sanitizes req.body, req.params, req.query by default) and standalone sanitize/isSanitized functions. Ships with TypeScript definitions, supports both ESM and CJS, and has zero runtime dependencies. Updates are infrequent but stable. Key differentiator: focused solely on MongoDB operator stripping with a simple API, unlike broader sanitization libraries.
npm install mongodb-sanitizeNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows ESM imports, middleware usage, standalone sanitize, and isSanitized check.
Use a recursive sanitizer or manually sanitize nested structures if needed.
Use the default (no options) or pass an array of field names like ['body', 'query'].
Switch to named imports: import { sanitizeMiddleWare } from 'mongodb-sanitize'.Combine with other validation libraries (e.g., joi, express-validator) for comprehensive security.
Cast the result: const clean: MyType = sanitize(input) as MyType;
Use named import: import { sanitizeMiddleWare } from 'mongodb-sanitize'Ensure mongodb-sanitize is in dependencies and your tsconfig.json has 'moduleResolution': 'node'.
Switch to: import { sanitize } from 'mongodb-sanitize'No dependency data recorded yet.