Files
ghbot/.agents/skills/zod/references/compose-shared-schemas.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
Extract Shared Schemas into Reusable Modules MEDIUM Duplicating schemas across files leads to inconsistency; shared schemas ensure single source of truth compose, reuse, modules, organization

Extract Shared Schemas into Reusable Modules

When the same schema pattern appears in multiple places, extract it into a shared module. This ensures consistency, reduces duplication, and makes changes propagate automatically across your codebase.

Incorrect (duplicating schemas):

// api/users.ts
import { z } from "zod";

const userSchema = z.object({
  id: z.string().uuid(),
  email: z.string().email(),
  name: z.string().min(1),
  createdAt: z.date()
});

// api/orders.ts
import { z } from "zod";

const orderSchema = z.object({
  id: z.string().uuid(), // Duplicated
  userId: z.string().uuid(), // Same pattern
  items: z.array(
    z.object({
      productId: z.string().uuid(), // Duplicated
      quantity: z.number().int().positive()
    })
  ),
  createdAt: z.date() // Duplicated
});

// api/comments.ts
import { z } from "zod";

const commentSchema = z.object({
  id: z.string().uuid(), // Same duplication
  userId: z.string().uuid(),
  content: z.string().min(1),
  createdAt: z.date() // Inconsistency risk
});

Correct (shared schema modules):

// schemas/common.ts
import { z } from "zod";

// Reusable ID types
export const uuid = z.string().uuid();
export type UUID = z.infer<typeof uuid>;

// Timestamps
export const timestamps = z.object({
  createdAt: z.date(),
  updatedAt: z.date()
});

// Base entity with ID
export const baseEntity = z
  .object({
    id: uuid
  })
  .merge(timestamps);

export type BaseEntity = z.infer<typeof baseEntity>;

// Pagination
export const paginationParams = z.object({
  page: z.coerce.number().int().positive().default(1),
  limit: z.coerce.number().int().min(1).max(100).default(20)
});
// schemas/user.ts
import { z } from "zod";
import { baseEntity, uuid } from "./common";

export const userSchema = baseEntity.extend({
  email: z.string().email(),
  name: z.string().min(1)
});

export type User = z.infer<typeof userSchema>;
// schemas/order.ts
import { z } from "zod";
import { baseEntity, uuid } from "./common";

const orderItemSchema = z.object({
  productId: uuid,
  quantity: z.number().int().positive()
});

export const orderSchema = baseEntity.extend({
  userId: uuid,
  items: z.array(orderItemSchema).min(1),
  total: z.number().positive()
});

export type Order = z.infer<typeof orderSchema>;

Organizing schema modules:

schemas/
├── common.ts       # Shared primitives and base schemas
├── user.ts         # User-related schemas
├── order.ts        # Order-related schemas
├── product.ts      # Product-related schemas
└── index.ts        # Re-exports for convenience
// schemas/index.ts
export * from "./common";
export * from "./user";
export * from "./order";
export * from "./product";

// Usage
import { userSchema, orderSchema, uuid, type User } from "@/schemas";

When NOT to use this pattern:

  • One-off schemas used only in a single file
  • When schemas look similar but have different semantics (don't over-abstract)

Reference: Zod - Type Inference