zod-patterns
$
npx mdskill add mx-space/core/zod-patternsGenerate robust Zod schemas for NestJS DTOs and request validation.
- Creates type-safe schemas for data transfer objects and API inputs.
- Depends on the z library and nest-zod package for implementation.
- Selects validators based on project conventions in the core module.
- Outputs ready-to-use TypeScript classes for form and request handling.
SKILL.md
.github/skills/zod-patternsView on GitHub ↗
---
name: zod-patterns
description: MX Space project Zod schema patterns. Apply when creating DTOs, validation schemas, or handling request validation.
user-invocable: false
---
# Zod Schema Patterns
## Basic Pattern
```typescript
import { z } from 'zod'
import { createZodDto } from 'nestjs-zod'
// Define Schema
export const MySchema = z.object({
name: z.string().min(1),
email: z.string().email(),
age: z.number().int().positive().optional(),
})
// Create DTO class
export class MyDto extends createZodDto(MySchema) {}
// Partial DTO for updates
export class PartialMyDto extends createZodDto(MySchema.partial()) {}
```
## Project Custom Validators
Location: `apps/core/src/common/zod/`
```typescript
import {
// From primitives.ts:
zNonEmptyString, // Non-empty string (z.string().min(1))
zCoerceInt, // Coerced integer
zCoercePositiveInt, // Coerced positive integer
zCoerceBoolean, // Coerced boolean (handles 'true'/'1'/1/etc.)
zCoerceDate, // Coerced date
zOptionalDate, // Optional date (null/empty → undefined)
zOptionalBoolean, // Optional coerced boolean
zEmptyStringToNull, // Empty string → null, else string
zNilOrString, // string | null | undefined
zHexColor, // Hex color (#fff or #ffffff)
zAllowedUrl, // HTTP or HTTPS URL
zStrictUrl, // Strict URL validation
zHttpsUrl, // HTTPS-only URL
zPaginationPage, // Coerced int, min 1, default 1
zPaginationSize, // Coerced int, min 1, max 50, default 20
zSortOrder, // 1 | -1 | undefined (accepts 'asc'/'desc')
zArrayUnique, // Unique array elements (generic)
zUniqueStringArray, // Unique non-empty string array
// From custom.ts:
zBooleanOrString, // boolean | string union
zTransformEmptyNull, // Empty string → null (generic wrapper)
zTransformBoolean, // Transform to optional boolean
zPinDate, // Pin date (Date | null | undefined, true=now, false=null)
zSlug, // Slug string (trimmed)
zEmail, // Email with custom message
zUrl, // URL with custom message
zMaxLengthString, // Max length string factory
zRefTypeTransform, // Content ref type ('post'→'Post', etc.)
zPrefer, // 'lexical' enum optional
zLang, // 2-char language code
// From shared/id/entity-id.ts:
zEntityId, // Snowflake entity ID string validation
zEntityIdOrInt, // Entity ID or positive integer union
} from '~/common/zod'
```
## Entity ID Validation
```typescript
import { zEntityId } from '~/common/zod'
const Schema = z.object({
id: zEntityId, // Snowflake ID string
categoryId: zEntityId, // Foreign key reference
relatedIds: z.array(zEntityId), // Array of entity IDs
})
// For DTOs used in path params:
import { EntityIdDto } from '~/shared/dto/id.dto'
// EntityIdDto = { id: zEntityId }
```
## Extending Base Schemas
```typescript
// Compose schemas using .extend()
const PostSchema = z.object({
title: zNonEmptyString,
slug: zSlug,
categoryId: zEntityId,
tags: z.array(z.string()).optional(),
contentFormat: z.enum(['markdown', 'lexical']),
})
```
## Common Patterns
### Optional Fields with Defaults
```typescript
z.boolean().default(true).optional()
z.number().default(0).optional()
z.array(z.string()).default([]).optional()
```
### Preprocessing
```typescript
// Empty string to null
z.preprocess(
(val) => (val === '' ? null : val),
z.string().nullable()
).optional()
// String to number
z.preprocess(
(val) => (typeof val === 'string' ? parseInt(val, 10) : val),
z.number()
)
```
### Union Types
```typescript
z.union([z.string(), z.number()])
z.enum(['draft', 'published', 'archived'])
```
### Array Validation
```typescript
// Basic array
z.array(z.string())
// Length constraints
z.array(z.string()).min(1).max(10)
// Unique elements
zArrayUnique(z.string())
```
### Nested Objects
```typescript
const AddressSchema = z.object({
street: z.string(),
city: z.string(),
})
const UserSchema = z.object({
name: z.string(),
address: AddressSchema.optional(),
addresses: z.array(AddressSchema).optional(),
})
```
### Conditional Validation
```typescript
// refine for custom validation
z.object({
password: z.string(),
confirmPassword: z.string(),
}).refine(
(data) => data.password === data.confirmPassword,
{ message: 'Passwords must match' }
)
```
## Type Inference
```typescript
// Infer type from Schema
type MyType = z.infer<typeof MySchema>
// Use in Service
async create(data: z.infer<typeof MySchema>) {
return this.repository.create(data)
}
```
More from mx-space/core
- api-conventionsMX Space API design conventions. Apply when writing controllers, API endpoints, or handling HTTP requests.
- create-e2e-testCreate E2E test file for a specified module. Use when adding end-to-end tests for controllers or unit tests for services and repositories.
- create-moduleCreate a new NestJS module with repository, service, controller, schema, and Drizzle table definition. Use when adding new feature modules, API endpoints, or business domains.
- mx-pg-controller-migrationUse when verifying and porting an mx-core controller (Post/Note/Page/Comment/Category/etc.) after the MongoDB→PostgreSQL cutover, or when its data shape no longer matches what api-client and admin-vue3 expect. Triggers on "校验 controller"、"check controller"、"迁移 controller"、"修复迁移后的接口"、"data missing after PG migration"、"related/category 字段丢了" and similar.
- mx-reviewReview code for MX Space project conventions. Checks NestJS patterns, Drizzle ORM repositories, Zod schemas, API design, etc.
- release-coreUse when releasing mx-core server (apps/core) or @mx-space/api-client package — version bump, changelog, git tag, Docker build, GitHub Release, and Dokploy redeploy. Triggers on "发版", "release a new version", "cut a release", "bump version", "publish api-client".
- run-testRun tests. Supports running all tests, single file, or pattern-matched tests.