Request parsing and validation
What you need to know
A request body is untrusted data even when TypeScript describes its intended shape. Parse it, limit its size, validate its structure and only then hand it to application logic.
By the end, you can: validate JSON and query fields; reject unsupported input.
Validate a decoded mutation object
export function parseQuantity(value: unknown): number | null { if (!value || typeof value !== 'object' || Array.isArray(value)) return null; const row = value as Record<string, unknown>; if (Object.keys(row).length !== 1 || !Object.hasOwn(row, 'quantity') || typeof row.quantity !== 'number') return null; return Number.isInteger(row.quantity) && row.quantity >= 1 && row.quantity <= 5 ? row.quantity : null;}Expected behavior (not framework-tested): {quantity: 2} returns 2; {quantity: "2"}, an array or an extra key returns null.
How it works
Separate transport validation from business rules: a JSON syntax error is different from an invalid quantity, and both differ from a valid request that the current user is not allowed to perform. A body stream is normally consumed once; reading JSON twice is not a way to validate it. Check media type, enforce an actual byte bound during streaming or at a trusted ingress, then parse. Content-Length alone can be missing or unreliable. The V6 fixture implements a bounded JSON reader; do not blindly call request.json() for arbitrary large inputs. Decode UTF-8 strictly and reject arrays or null when an object is required.
For query parameters, use URL or NextRequest.nextUrl rather than string splitting. Values are strings; repeated values require a defined policy. The examples reject duplicate page parameters, decimal notation, blanks, out-of-range values and silent coercion. Unknown keys are rejected by the mutation contract. These choices make tests and clients predictable; other policies can be appropriate if documented.
Validate page query input
export function parsePage(url: string): number | null { const values = new URL(url).searchParams.getAll('page'); if (values.length === 0) return 1; if (values.length !== 1 || !/^[1-9][0-9]{0,2}$/.test(values[0])) return null; const page = Number(values[0]); return page <= 100 ? page : null;}Expected behavior (not framework-tested): No page gives 1; page=3 gives 3; duplicate page, page=0 and page=1.5 are invalid.
Common mistake
request.json() as { quantity: number } only changes TypeScript’s view of the value. It does not check the runtime object. Never use a client-supplied owner ID or role as proof that the caller may modify a resource.
Practice and verification
Test absent quantity, null, boolean, string, fraction, 0, 5, 6, extra key and duplicate page. Add a malformed UTF-8 or over-limit body to the integration matrix. Separate 415 media-type failure, 413 size failure and 400 invalid JSON or fields.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.