v3.3.1
Playground

Define Mongoose schemas with Zod.

Create Mongoose schemas from Zod objects. Start with a model you can validate in memory, then use the guides when you need nested objects, references, or shared schemas.

example.ts
import { z } from 'zod/v4';
import { toMongooseSchema } from '@nullix/zod-mongoose';
import type { InferDocument } from '@nullix/zod-mongoose';

const UserZodSchema = z.object({
  username: z.string().min(3),
  email: z.string().email(),
});

const UserSchema = toMongooseSchema(UserZodSchema);

// The stored document type includes Mongoose's generated _id.
type User = InferDocument<typeof UserZodSchema>;

What do you need to do?

    Create a model

    Install the packages, convert a Zod object, and validate your first Mongoose document.

    Validate documents

    See which Zod rules run during Mongoose validation and when to parse input yourself.

    Control nested IDs

    Keep or remove _id on embedded objects, including schemas imported from other files.

    Reference another model

    Define an ObjectId reference and validate populated or unpopulated data.

    Reuse schemas across files

    Share a Zod object while choosing different Mongoose options at each use.

    Try the playground

    Inspect the generated Mongoose schema before adding it to your project.

Ready to build a model?

Follow a small example from installation to validation, then connect to MongoDB when you need to save.

:::