Skip to content

.tip

· one of the small things

Validate once at the edge, then trust the types

Parsing the same payload with a schema in three layers is slow, and it means nobody trusts the layer above them. Parse where data comes in (request bodies, the environment, external APIs) and pass typed values inward.

const body = CreateOrder.parse(await req.json()); // the only parse
await createOrder(body);                          // typed from here on

z.infer<typeof CreateOrder> is the type the inner code takes. If a value has that type, it was validated, by construction.

typescriptvalidation