Zod Schemas
Only on Invokix
Your contract defines the shape of your API. Zod schemas enforce it at runtime. Invokix generates both from the same source so they never drift apart — your compile-time types and runtime validation always match exactly.
What gets generated
For every schema in your contract, Invokix generates a matching Zod schema. Every constraint in your OpenAPI spec — minimum values, string formats, enum members, required fields — maps directly to a Zod validator.
Generated: src/schemas/orders.ts
// Auto-generated by Invokix — do not edit manually
// Contract: Orders API v1.2 — published March 5, 2025
import { z } from "zod"
export const LineItemSchema = z.object({
id: z.string().uuid(),
productId: z.string().min(1),
quantity: z.number().int().positive(),
price: z.number().nonnegative(),
})
export const OrderSchema = z.object({
id: z.string().uuid(),
price: z.number().nonnegative(),
status: z.enum(["pending", "shipped", "delivered", "cancelled"]),
userId: z.string().uuid(),
createdAt: z.string().datetime(),
lineItems: z.array(LineItemSchema),
})
export const CreateOrderBodySchema = z.object({
price: z.number().positive(),
lineItems: z.array(z.object({
productId: z.string().min(1),
quantity: z.number().int().positive(),
price: z.number().positive(),
})).min(1),
})
export const UpdateOrderBodySchema = z.object({
status: z.enum(["shipped", "delivered", "cancelled"]).optional(),
})
// Inferred types — identical to the generated TypeScript types
export type Order = z.infer<typeof OrderSchema>
export type LineItem = z.infer<typeof LineItemSchema>
export type CreateOrderBody = z.infer<typeof CreateOrderBodySchema>
export type UpdateOrderBody = z.infer<typeof UpdateOrderBodySchema>Using schemas for request validation
Use the generated schemas to validate incoming request bodies in your API routes. If the body does not match the contract, Zod catches it before it reaches your database.
app/api/orders/route.ts
import { NextRequest, NextResponse } from "next/server"
import { CreateOrderBodySchema } from "@/schemas/orders"
export async function POST(req: NextRequest) {
const body = await req.json()
const parsed = CreateOrderBodySchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json(
{ error: "Invalid request body", issues: parsed.error.issues },
{ status: 400 }
)
}
// parsed.data is fully typed as CreateOrderBody
const { price, lineItems } = parsed.data
// ... create the order
}Using schemas for response validation
Validate API responses at the boundary — catch backend drift before it reaches your UI.
Validating a fetch response
import { OrderSchema } from "@/schemas/orders"
async function fetchOrder(id: string) {
const res = await fetch("/api/orders/" + id)
const data = await res.json()
// If the backend returns an unexpected shape — Zod throws immediately
const order = OrderSchema.parse(data)
return order // fully typed as Order
}How OpenAPI constraints map to Zod
| OpenAPI constraint | Zod validator |
|---|---|
format: uuid | .uuid() |
format: email | .email() |
format: date-time | .datetime() |
minimum: 0 | .nonnegative() |
minimum: 1 | .positive() |
minLength: 1 | .min(1) |
maxLength: 100 | .max(100) |
pattern: "..." | .regex(...) |
enum: [...] | z.enum([...]) |
nullable: true | .nullable() |
| Optional field | .optional() |
