481
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill mx-space/core --skill zod-patterns- SKILL.md3.2 KB
Overview
This skill provides a collection of Zod schema patterns and helpers tailored for building DTOs, validation schemas, and request validation in NestJS/TypeScript projects. It captures common patterns such as base schema extensions, custom validators for MongoDB, preprocessing, unions, arrays, nested objects, and mapping schemas to TypeGoose models. Use it to standardize input validation and infer TypeScript types directly from Zod schemas.
How this skill works
The skill exposes reusable Zod patterns and helper validators (e.g., MongoDB ObjectId, non-empty string, unique arrays, pin date) and example schema compositions. You create Zod objects, extend base schemas, or produce DTO classes from schemas to use in controllers and services. It also demonstrates preprocessing, conditional validation, defaults, and mapping between Zod schemas and TypeGoose models for consistent runtime validation and compile-time types.
When to use it
- Creating request DTOs and validation schemas for NestJS endpoints
- Defining and reusing project-wide field rules (ObjectId, non-empty strings, unique arrays)
- Building update DTOs using partial schemas for PATCH/PUT handlers
- Converting validated data into TypeScript types for services and database models
- Mapping validation rules to TypeGoose models for Mongoose integration
Best practices
- Define shared base schemas (write/read bases) and extend them for domain entities
- Use createZodDto to generate DTO classes for tight NestJS integration
- Prefer preprocessors to normalize incoming data (empty string -> null, string -> number) before validation
- Use .partial() to create update DTOs and avoid duplicating field definitions
- Keep custom validators (zMongoId, zNonEmptyString, zArrayUnique) centralized for consistency
Example use cases
- Define a PostSchema by extending a WriteBaseSchema to enforce title, slug, category, and optional tags
- Create MyDto from a Zod schema and derive MyType via z.infer for service method signatures
- Validate nested user addresses with AddressSchema and an optional addresses array on the UserSchema
- Preprocess query params converting numeric strings to numbers before validating as numbers
- Map a Zod schema to a TypeGoose model so required/optional semantics align with Mongoose properties
FAQ
Use createZodDto(MySchema) to produce a DTO class compatible with NestJS controllers.
How can I handle partial updates?
Call MySchema.partial() and wrap it with createZodDto to produce an update DTO where all fields are optional.