Files
ghbot/.agents/skills/zod/references/schema-use-enums.md
lezi-fun 9e47ab339e feat: sync ghbot-Nodeloc v1.2.0 capabilities into upstream
Merges all 33 commits from lza6/ghbot-Nodeloc (v1.0.0 through v1.2.0)
while keeping both sides' features in every conflicted file:

- EventRouter dispatch extracted from runReview.ts (7.1), with the
  runtime environment block preserved on the review-start comment
- Unified command ack messages (6.3) combined with the collapsed
  runtime environment block and bot signature on /recheck replies
- Status banner and collapsible unposted findings in review bodies (6.1)
- Centralized runtime directories behind GHBOT_RUNTIME_DIR (D-13);
  webhook knowledge cache now lives under tempRootDirectory()
- Production hardening for merge races, timeouts, webhook ops;
  atomic temp-file rename for R2 cache restores kept from upstream
- Webhook parity retained: triage events gated by WEBHOOK_TRIAGE_ENABLED,
  read-only command guidance, repository knowledge injection,
  runtime environment block on webhook chat replies
- Sanitization/secret redaction centralization, error-aware retry,
  duplicate merge prevention, in-process webhook retries, /metrics
- Infra: eslint/prettier gates with CI verify workflow, docs archives

typecheck, eslint, prettier format:check, and 164 tests all pass.
2026-08-24 11:47:28 +08:00

107 lines
2.7 KiB
Markdown

---
title: Use Enums for Fixed String Values
impact: CRITICAL
impactDescription: Plain strings accept any value including typos; enums restrict to valid values and enable autocomplete
tags: schema, enum, literal, union
---
## Use Enums for Fixed String Values
When a field should only accept specific values (status, role, type), use `z.enum()` or `z.literal()` instead of `z.string()`. Plain strings accept any value including typos, while enums provide validation, type safety, and IDE autocomplete.
**Incorrect (plain string for fixed values):**
```typescript
import { z } from "zod";
const orderSchema = z.object({
id: z.string(),
status: z.string(), // Accepts any string
priority: z.string() // No constraints
});
type Order = z.infer<typeof orderSchema>;
// { id: string; status: string; priority: string }
// Typos and invalid values pass validation
orderSchema.parse({
id: "123",
status: "pendng", // Typo passes
priority: "super-urgent" // Invalid value passes
});
function processOrder(order: Order) {
if (order.status === "pending") {
// Might never match due to typos
// ...
}
}
```
**Correct (using z.enum):**
```typescript
import { z } from "zod";
const OrderStatus = z.enum(["pending", "processing", "shipped", "delivered"]);
const Priority = z.enum(["low", "medium", "high"]);
const orderSchema = z.object({
id: z.string(),
status: OrderStatus,
priority: Priority
});
type Order = z.infer<typeof orderSchema>;
// { id: string; status: 'pending' | 'processing' | 'shipped' | 'delivered'; priority: 'low' | 'medium' | 'high' }
// Typos are caught at validation
orderSchema.parse({
id: "123",
status: "pendng", // ZodError: Invalid enum value
priority: "super-urgent" // ZodError: Invalid enum value
});
// Extract enum values for reuse
OrderStatus.options; // ['pending', 'processing', 'shipped', 'delivered']
type OrderStatusType = z.infer<typeof OrderStatus>; // 'pending' | 'processing' | ...
```
**For native TypeScript enums:**
```typescript
enum Role {
Admin = "admin",
User = "user",
Guest = "guest"
}
// Use z.nativeEnum for TS enums
const userSchema = z.object({
role: z.nativeEnum(Role)
});
```
**For single literal values (discriminated unions):**
```typescript
const successResponse = z.object({
status: z.literal("success"),
data: z.unknown()
});
const errorResponse = z.object({
status: z.literal("error"),
message: z.string()
});
const response = z.discriminatedUnion("status", [successResponse, errorResponse]);
```
**When NOT to use this pattern:**
- When the set of valid values is dynamic or user-defined
- When values come from a database that may have more options
Reference: [Zod API - Enums](https://zod.dev/api#enums)