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.

databasedevops
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.

ESM-only since v3; CJS require throws ERR_REQUIRE_ESM.

import { zodMongoSchema } from 'zod-mongodb-schema'

Named export, not default.

import { zodMongoSchema } from 'zod-mongodb-schema'

Both syntaxes work; type import is fine.

import { MongoSchema } from 'zod-mongodb-schema'

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 footguns
breakingESM-only: require() throws ERR_REQUIRE_ESM; use dynamic import() or migrate to ESM.
breakingRequires Node.js >=24.10 and npm >=11; older runtimes will fail on install.
gotchaOnly z.object() schemas are supported; z.string(), z.number(), etc. standalone will throw.
gotchaPeer dependency zod must be ^3.25.64 or ^4.0.0; older versions incompatible.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
7 hits · last 30 days
gptbot
3
ahrefsbot
1
Resources