pmogollons/zod-schema(opens in a new tab)
Language: TypeScript
- stars
- 16
- watchers
- 2
- forks
- 0
- open issues
- 2
- Last updated
- 09/22/2026
Language: TypeScript
zod-schema provides a powerful and flexible way to implement schema validation for Meteor collections using the popular zod library. This package allows developers to define schemas for their collections, ensuring that the data being inserted or updated adheres to specified rules and types.
withSoftDelete method, allowing you to mark documents as deleted without removing them from the database.withDates method.withUser method, enhancing accountability and traceability.To add this package to your Meteor project, run:
How to install:
meteor add pmogollons:zod-schema
To use the zod-schema package in your Meteor project, follow these steps:
Define Your Schema: Use the zod library to create a schema for your collection. For example:
import { z } from "zod"; const userSchema = z.object({ name: z.string(), age: z.number(), });
Create a Collection: Create a new Mongo collection and apply the schema using the withSchema method.
const UserCollection = new Mongo.Collection("users"); UserCollection.withSchema(userSchema);
Insert Documents: When inserting documents, the package will automatically validate them against the defined schema.
await UserCollection.insertAsync({ name: "Alice", age: 30 }); // Valid await UserCollection.insertAsync({ name: "Bob", age: "thirty" }); // Throws ValidationError
Update Documents: Similarly, updates will also be validated.
const userId = await UserCollection.insertAsync({ name: "Charlie", age: 25 }); await UserCollection.updateAsync(userId, { $set: { age: 26 } }); // Valid await UserCollection.updateAsync(userId, { $set: { age: "twenty-six" } }); // Throws ValidationError
Optional Features: You can also use additional features like soft deletes and date tracking:
UserCollection.withSoftDelete(); UserCollection.withDates(); UserCollection.withUser();
Skip Validation: If you need to skip validation for specific operations, you can do so by passing the skipSchema option:
await UserCollection.insertAsync({ name: "David", age: "35" }, { skipSchema: true });
Soft Delete: You can also use the recoverAsync method to recover a soft deleted document.
await UserCollection.recoverAsync({ _id: deletedUserId });
pmogollons:nova any collection that uses soft delete will automatically add a isDeleted filter to the root collection of your query.