Skip to content

Installation

Install mongoose-guard from npm next to mongoose. It needs Node.js 20 or newer and Mongoose 8 or 9. It has no runtime dependencies of its own.

Terminal window
npm install mongoose-guard
RequirementSupportedNotes
Node.js20, 22, 24Older versions are not tested.
Mongoose8.x, 9.xEvery test runs against both. Mongoose 7 and older are not supported.
Express4.x, 5.xOnly needed if you use mongoose-guard/express.
TypeScripttested with 5.9 and 6.0Optional. Types ship with the package.

mongoose is a peer dependency, so mongoose-guard uses the copy your app already has. express and @types/express are optional peers: you only need them for the Express adapter.

The package has three entry points. Import only the one you need.

ImportUse it when
mongoose-guard/expressYou use Express. Gives you a middleware.
mongoose-guard/webYour framework hands you a standard Request: Next.js route handlers, Hono, Remix, React Router, SvelteKit.
mongoose-guardAnything else, or tests. Gives you a plain function that takes a body and returns a result.
import { guard } from "mongoose-guard/express";
import { guard as webGuard, invalidResponse } from "mongoose-guard/web";
import { createGuard, validateBody, GuardConfigError } from "mongoose-guard";

Importing mongoose-guard/express doesn’t load the web adapter and vice versa.

Both work. Use whichever your project already uses.

// ESM
import { guard } from "mongoose-guard/express";
// CommonJS
const { guard } = require("mongoose-guard/express");

TypeScript projects work with every moduleResolution setting, including the older node (node10) setting that many CommonJS Express projects still have.

mongoose-guard is tested on Node.js. Bun and Deno may work wherever Mongoose works on them, but they aren’t tested. It does not run on edge runtimes such as Cloudflare Workers or Vercel Edge functions, because Mongoose needs a TCP connection to MongoDB that those runtimes don’t provide.