Files
ghbot/.agents/skills/zod/references/schema-avoid-optional-abuse.md
T
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

title, impact, impactDescription, tags
title impact impactDescription tags
Avoid Overusing Optional Fields CRITICAL Excessive optional fields create schemas that accept almost anything; forces null checks throughout codebase schema, optional, nullable, required

Avoid Overusing Optional Fields

Making too many fields optional creates overly permissive schemas that validate almost any input. This pushes validation downstream into business logic, requiring defensive null checks everywhere instead of guaranteeing data shape at the boundary.

Incorrect (optional abuse):

import { z } from "zod";

// Every field optional - almost anything passes
const userSchema = z.object({
  id: z.string().optional(),
  name: z.string().optional(),
  email: z.string().optional(),
  role: z.string().optional()
});

type User = z.infer<typeof userSchema>;
// { id?: string; name?: string; email?: string; role?: string }

// Empty object passes validation!
userSchema.parse({}); // ✓ Valid: {}

function greetUser(user: User) {
  // Forced to add null checks everywhere
  if (user.name) {
    console.log(`Hello, ${user.name}`);
  } else {
    console.log("Hello, stranger"); // Shouldn't happen if data is clean
  }
}

Correct (explicit required vs optional):

import { z } from "zod";

// Required fields are required, optional fields are intentional
const userSchema = z.object({
  id: z.string().uuid(), // Required
  name: z.string().min(1), // Required, non-empty
  email: z.string().email(), // Required
  role: z.enum(["admin", "user", "guest"]), // Required
  nickname: z.string().optional(), // Intentionally optional
  bio: z.string().nullable() // Can be explicitly null
});

type User = z.infer<typeof userSchema>;

// Empty object fails validation
userSchema.parse({}); // ✗ Throws ZodError

function greetUser(user: User) {
  // user.name is guaranteed to exist
  console.log(`Hello, ${user.name}`);

  // Only optional fields need checks
  if (user.nickname) {
    console.log(`Also known as: ${user.nickname}`);
  }
}

Use .partial() for update schemas:

// Base schema with required fields
const userSchema = z.object({
  id: z.string().uuid(),
  name: z.string().min(1),
  email: z.string().email()
});

// All fields optional for PATCH updates
const updateUserSchema = userSchema.partial();

// Only specific fields optional
const createUserSchema = userSchema.partial({ id: true });

When NOT to use this pattern:

  • When modeling partial updates (PATCH endpoints)
  • When fields genuinely may not exist (legacy data, external APIs)

Reference: Zod API - optional