Registry / database / zod-mongodb-schema

zod-mongodb-schema

JSON →
library3.0.2jsnpmunverified

Converts Zod object schemas into MongoDB validation schemas (JSON Schema compatible). Version 3.0.2 targets Node.js >=24.10 and npm >=11, with experimental Yarn support dropped. Requires Zod ^3.25.64 or ^4.0.0 and BSON ^7.0.0 as peer dependencies. Validates Zod object() schemas only; other Zod types are not supported. Ships TypeScript types. Notable breaking change in v3: switched from CJS to ESM-only, requiring dynamic import or native ESM.

npm install zod-mongodb-schema
INSTALL
IMPORT
SIG · ZOD-MONGODB-SCHEMA
Z
zod-mongodb-schema
databasejavascriptv3.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.

zodMongoSchema
import { zodMongoSchema } from 'zod-mongodb-schema'
const zodMongoSchema = require('zod-mongodb-schema')
ESM-only since v3; CJS require throws ERR_REQUIRE_ESM.
zodMongoSchema
import { zodMongoSchema } from 'zod-mongodb-schema'
import zodMongoSchema from 'zod-mongodb-schema'
Named export, not default.
type MongoSchema
import { MongoSchema } from 'zod-mongodb-schema'
import type { MongoSchema } from 'zod-mongodb-schema'
Both syntaxes work; type import is fine.

Converts a Zod object schema into a MongoDB $jsonSchema validation object.

import { z } from 'zod'; import { zodMongoSchema } from 'zod-mongodb-schema'; const UserSchema = z.object({ name: z.string(), age: z.number().min(0), email: z.string().email(), }); const mongoSchema = zodMongoSchema(UserSchema); console.log(mongoSchema); // $jsonSchema object with bsonType, properties, required, etc.
Debug
Known issues
breakingESM-only: require() throws ERR_REQUIRE_ESM; use dynamic import() or migrate to ESM.
fix
Replace require('zod-mongodb-schema') with await import('zod-mongodb-schema') or set type: module in package.json.
affects: >=3.0.0
breakingRequires Node.js >=24.10 and npm >=11; older runtimes will fail on install.
fix
Upgrade Node.js to 24.10+ and npm to 11+.
affects: >=3.0.0
gotchaOnly z.object() schemas are supported; z.string(), z.number(), etc. standalone will throw.
fix
Wrap standalone types in z.object({ key: type }).
affects: >=0.0.0
gotchaPeer dependency zod must be ^3.25.64 or ^4.0.0; older versions incompatible.
fix
Install zod@^3.25.64 or zod@^4.0.0.
affects: >=3.0.0
Errors
Common errors & fixes
Must use import to load ES Module: require() of /path/to/node_modules/zod-mongodb-schema/index.js from /path/to/project/app.js not supported.
Package is ESM-only; CJS require() fails.
fix
Use import { zodMongoSchema } from 'zod-mongodb-schema' or dynamic import().
Unsupported schema type. Only ZodObject is supported.
Passed a non-object Zod type (e.g., z.string()).
fix
Wrap the schema in z.object({ ... }).
Cannot find module 'zod' or 'bson'
Missing peer dependencies.
fix
Run npm install zod@^3.25.64 bson@^7.0.0
Invalid input: Expected Zod schema but received [object Object]
Passed a plain JavaScript object instead of a Zod schema.
fix
Use z.object({ ... }) to create the schema.
Upgrade
Version history
3.0.2latest on npm
Audit
Dependencies
zodrequiredPeer dependency — core input schema library
bsonrequiredPeer dependency — BSON type support for MongoDB extended JSON
Agent activity
40 hits · last 30 days
node
34
OpenAI (training)
1
Resources