0
Challenge · 27Mutations & Auth

Zod validation (client UX + server trust)

Mutationsused in ~85% of apps

A Server Action is a public POST endpoint (concept 25): anyone with curl can hit it with a body your form would never send. Client-side validation is a UX nicety; it is not a security boundary. The senior move is one Zod schema used in two places — on the client for instant feedback, and re-run inside the action for trust.

Core idea. Define the shape once with a Zod schema. Use it on the client for fast inline errors, and call `schema.safeParse()` again at the top of the Server Action — because the action runs on the server and the client check can be bypassed. One schema, two call sites: UX and trust.
Playground, this isn't a screenshot
ONE SOURCE OF TRUTH — schema.ts
const PostSchema = z.object({
  title: z.string().min(1, "Title is required").max(60),
  body:  z.string().min(10, "Write at least 10 characters"),
  tag:   z.enum(["react", "css", "ts"]),
})
LIVE INPUT — validated on every keystroke
invalid

Title is required

valid
valid
PostSchema.safeParse(input) → { success: false }
result.error.issues = [
{ path: ["title"], message: "Title is required" },
]
type Post = z.infer<typeof PostSchema>
type Post = {
  title: string
  body: string
  tag: "react" | "css" | "ts"
}

Edit the schema once — both the validator and the type update together.

↑ play a little first, then dig in