ワンクリックで
zod-patterns
// MX Space project Zod schema patterns. Apply when creating DTOs, validation schemas, or handling request validation.
// MX Space project Zod schema patterns. Apply when creating DTOs, validation schemas, or handling request validation.
Use when releasing mx-core server (apps/core), @mx-space/api-client, or @mx-space/cli (mxs) — 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", "publish cli", "release mxs".
Use when an agent must inspect, draft, validate, edit, publish, unpublish, delete, or configure mx-space content through packages/cli or the mxs binary.
Author and review Drizzle SQL migrations safely for rolling deploys. Triggers when editing apps/core/src/database/schema/*.ts or apps/core/src/database/migrations/*.sql, when the user runs drizzle-kit generate, when "lint-migrations" reports a violation, or on prompts like "迁移", "改 schema", "alter table", "add a column", "drop column", "migration safety". Enforces the expand-contract pattern because mx-core ships rolling deploys (Dokploy, 2 replicas) where new and old pods coexist for tens of seconds during cutover.
MX Space API design conventions. Apply when writing controllers, API endpoints, or handling HTTP requests.
Create E2E test file for a specified module. Use when adding end-to-end tests for controllers or unit tests for services and repositories.
Create a new NestJS module with repository, service, controller, schema, and Drizzle table definition. Use when adding new feature modules, API endpoints, or business domains.
| name | zod-patterns |
| description | MX Space project Zod schema patterns. Apply when creating DTOs, validation schemas, or handling request validation. |
| user-invocable | false |
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()) {}
Location: apps/core/src/common/zod/
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'
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 }
// 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']),
})
z.boolean().default(true).optional()
z.number().default(0).optional()
z.array(z.string()).default([]).optional()
// 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()
)
z.union([z.string(), z.number()])
z.enum(['draft', 'published', 'archived'])
// Basic array
z.array(z.string())
// Length constraints
z.array(z.string()).min(1).max(10)
// Unique elements
zArrayUnique(z.string())
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(),
})
// refine for custom validation
z.object({
password: z.string(),
confirmPassword: z.string(),
}).refine(
(data) => data.password === data.confirmPassword,
{ message: 'Passwords must match' }
)
// 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)
}