본문 바로가기
코딩 · 개발원문 · prompts.chat

Zod 입력값 검증 규칙 수립

Zod로 외부 입력값을 검증하고 TypeScript 타입과 런타임 검사를 동기화하는 규칙을 제공합니다.

원제 · Zod Schema Validation taste

추천 툴
코딩 에이전트형식 기준 추천

프롬프트 원문

# Zod Schema Validation taste of a TypeScript developer who trusts nothing from the outside
- Validate every input that crosses a trust boundary with Zod: request bodies, query strings, env vars, webhooks and third-party API responses. Confidence: 0.95
- Define the schema first and infer the TypeScript type with `z.infer`, instead of writing the type and the schema twice. Confidence: 0.90
- Use `safeParse` at API boundaries and turn errors into a clean 400 response; use `parse` only where a failure is truly a bug. Confidence: 0.85
- Validate environment variables once at startup and exit early with a clear message when something is missing. Confidence: 0.90
- Keep schemas in a shared module so the client and server use the same contract. Confidence: 0.80
- Use `z.coerce` only for query strings and form data, where every value arrives as a string. Confidence: 0.80
- Prefer `z.discriminatedUnion` over plain unions for tagged objects; errors are clearer and parsing is faster. Confidence: 0.80
- Use `.strict()` on input schemas where unknown keys usually mean a client bug. Confidence: 0.70
- Keep transforms small and pure; heavy business logic does not belong inside a schema. Confidence: 0.80
- Put refinements that check more than one field on the object with `.superRefine` and a clear `path`. Confidence: 0.75
- Write error messages for humans in schemas used by forms, and keep the default messages for internal APIs. Confidence: 0.70
- Use branded types like `z.string().uuid().brand<"UserId">()` for IDs that must never be mixed up. Confidence: 0.65
- Do not validate the same data again deep inside the app; parse once at the edge and pass typed values inward. Confidence: 0.85
- Make dates explicit: accept ISO strings at the boundary and convert to `Date` objects in one place. Confidence: 0.75
- Generate OpenAPI or JSON Schema from Zod schemas when the API is public, instead of keeping docs by hand. Confidence: 0.70
- Avoid `z.any()` and `z.unknown()` in public schemas except for fields that are truly free-form. Confidence: 0.80
- Use `.default()` for optional settings so the code after parsing never needs undefined checks. Confidence: 0.75
- Test schemas with both valid and invalid fixtures, especially for edge cases like empty strings and nulls. Confidence: 0.80
- Log validation failures from third-party APIs with the failing path, because they warn you about silent contract changes. Confidence: 0.80
- Keep schema names consistent, like `CreateUserInput` and `UserResponse`, so the direction of data is clear. Confidence: 0.65

출처 · prompts.chat 공개 프롬프트 모음 · 작성 Fatih Kadir Akın — CC0(저작권 포기)로 공개된 원문을 그대로 싣습니다. 한국어 제목 · 설명은 AI 번역이며, 실행 결과는 아직 없습니다.

같은 분야 프롬프트 코딩 · 개발

더 보기 ›