StoicBuilder

HandbookIII the CastraInput validation

IIIthe Castra, Stay in control

Input validation

1 min readUpdated 28 November

In plain words

Input validation means your server checks every piece of incoming data, like a quantity, an email or a note, before it saves or acts on it.

Where you see it

QuickBite's order form only offers quantities from 1 to 10. A visitor can skip the form and send quantity: -5 straight to your API, and if the server believes it, the order total goes negative.

app/api/cart/route.tsserver
import { z } from "zod"; const CartItem = z.object({ itemId: z.string().uuid(), quantity: z.number().int().min(1).max(10),}); const item = CartItem.parse(await req.json());
The server checks types and limits, whatever the form allowed.

What goes wrong

  1. Validating only in the form

    Browser checks help honest users fix typos. Requests can be sent without your form, so the server has to check again.

  2. Saving whatever fields the request contains

    Passing the whole request body to an update lets someone add role: "admin" to their own profile. Pick the fields you expect and drop the rest.

    await supabase.from("profiles").update(body).eq("id", user.id);
  3. No limit on length or size

    A note field with no cap lets someone save 50 MB of text and slow every page that loads it. Set a maximum for every text field.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Add server-side validation with zod to every API route and server action that accepts input, checking types, lengths and ranges, and reject anything else with a 400 error. Save only the fields you expect, never the whole request body.

Quick check

Sign in to earn +10 XP for it

Chirp's post box has maxlength="280", yet someone publishes a 20,000-character post. How?

Got a prompt that touches input validation?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.