This is a package for generating Mongoose schemas from Zod schemas. The goal is to improve type safety and reduce the amount of code that needs to be written when working with Mongoose.
yarn add @mirite/zod-to-mongoose
Passing in a Zod schema will return a Mongoose schema.
import { createSchema } from "@mirite/zod-to-mongoose";
const schema = createSchema(
z.object({
name: z.string().optional().default("Bob"),
age: z.number().default(3).optional(),
isHappy: z.boolean().optional(),
birthday: z.date().default(new Date()),
}),
);
const model = mongoose.model("modelName", schema);
const result = await model.create({
isHappy: true,
birthday: new Date("1980-01-01"),
});
If you pass a connection, the model will be created, registered with the connection, and returned for use.
import { createSchema } from "@mirite/zod-to-mongoose";
const model = createSchema(
z.object({
name: z.string().optional().default("Bob"),
age: z.number().default(3).optional(),
isHappy: z.boolean().optional(),
birthday: z.date().default(new Date()),
}),
"modelName", // The unique name to give the model
connection, // Mongoose connection
);
const result = await model.create({
isHappy: true,
birthday: new Date("1980-01-01"),
});
Only a subset of Zod types are supported. The following types are supported:
z.string()
z.number()
z.boolean()
z.date()
-
z.object()
(Nested objects are supported) -
z.union()
(Type safety is not guaranteed)
Another major caveat is that no additional validation is performed on the Mongoose schema. It is recommended to use Zod for validation before saving to the database, as well as on retrieval if there are data integrity concerns.