What this prompt does
This prompt asks the AI to build a Zod-based validation system for a [framework] API with [endpoint_count] endpoints, treating Zod as the single source of truth. It produces base schemas for your [entities], request schemas for create, update, query params, and pagination, response schemas using .transform() for output shaping, a custom error formatter producing [error_format] responses, middleware that validates body, params, and query automatically, schema sharing between a [frontend_framework] frontend and the backend via a shared package, OpenAPI 3.1 generation with zod-to-openapi, coercion rules for query parameters, and discriminated unions for polymorphic requests.
The structure works because defining validation once in Zod and deriving everything else from it keeps types, runtime validation, docs, and error shapes from drifting apart. Generating the OpenAPI spec straight from the schemas keeps documentation honest, and standardizing on [error_format] means you do not reinvent an error contract per project. The automatic middleware that validates body, params, and query also removes a whole category of per-route boilerplate, so a missing field is rejected consistently instead of slipping through on one endpoint and not another.
When to use it
- You want one schema definition shared between frontend and backend instead of duplicated types.
- Your API needs consistent validation across body, params, and query without hand-writing checks per route.
- You want an OpenAPI spec generated from the same schemas that validate requests, so docs cannot drift.
- You need a standard error response shape such as RFC 7807 Problem Details across all endpoints.
- You have polymorphic request types that need discriminated-union validation.
- You want query-parameter coercion (string to number or boolean) handled centrally.
Example output
Expect a set of schema modules and supporting code: base entity schemas, request and response schemas with transforms, the validation middleware, an error formatter producing [error_format] output, the OpenAPI generation setup, example discriminated unions, and tests. It is organized as a reusable validation package plus wiring into your [framework], not a single file.
Pro tips
- List
[entities]clearly so the base schemas line up with your real data model and can be reused for create/update variants. - Match
[framework]to your stack, since the middleware or plugin integration differs between Express, Fastify, and Nest. - Keep the shared schema package free of server-only imports so the
[frontend_framework]can consume it without bundling backend code. - Use
.transform()deliberately on response schemas — it is easy to leak internal fields if output shaping is an afterthought. - Confirm coercion rules for query params, as silent string-to-number conversion can mask malformed input if not bounded.
- Iterate by asking for a discriminated union for one specific polymorphic endpoint to see the pattern before applying it broadly.