Skip to content
tezvyn:

Runtime Response Validation with Schema Libraries

Source: zod.devHardHow cards are made

Runtime Response Validation with Schema Libraries

TypeScript types evaporate at runtime, so a fetch response typed as User[] can be null or malformed. Schema libraries like Zod give static types and runtime guards from one contract. The footgun is using as instead of parse, silently reintroducing crashes.

Why it exists

TypeScript's type system only operates at compile time. Once your code is bundled and shipped, every interface, type alias, and generic evaporates into plain JavaScript. This means a function parameter typed as User[] will happily accept null, undefined, or an object with the wrong keys at runtime. The language gives you no built-in way to enforce those shapes when data crosses a wire from an external API, a webhook, or a user upload. Runtime validation libraries were invented to close this exact gap: they turn a type definition into an executable guard that runs in production.

The mental model

Think of a schema as a contract that is written once and enforced twice. First, it generates a static TypeScript type so your editor can autocomplete and catch errors before you deploy. Second, it compiles into a runtime function that refuses to let malformed data pass. If the static type is the blueprint, the schema is the building inspector who checks the foundation after the earthquake.

How it works

You describe data using a declarative API. In Zod, z.object({ name: z.string() }) creates a validator that expects an object with a string name property. Calling parse on untrusted input either returns a value narrowed to that shape or throws a detailed error. Because the library is TypeScript-first, the inferred type and the runtime check share a single source of truth. Zod also ships with no external dependencies, weighs roughly two kilobytes gzipped, and offers an immutable API where each method returns a new instance rather than mutating state.

When to use it

Apply schema validation at every boundary where your application consumes data it does not control. Common examples include parsing JSON from an HTTP response, validating webhook payloads, sanitizing environment variables, and checking file uploads. It is especially valuable when the upstream service has no formal schema of its own, or when multiple teams own different parts of a distributed system and contracts drift over time.

When not to use it

Do not use runtime schemas for purely internal data that never leaves your process, such as temporary variables or private state that you initialized yourself. Adding parse overhead here is unnecessary ceremony. Also avoid treating schema validation as a replacement for application-level error handling; a schema tells you that data has the right shape, not that the business logic is valid.

One canonical example

A frontend application fetches a user profile from a REST endpoint and assumes the response matches an interface User { name: string }. Without validation, a backend bug that returns { name: null } causes a runtime exception when the UI tries to call string methods. With Zod, you define const User = z.object({ name: z.string() }) and call User.parse(response). If the backend misbehaves, the parse throws immediately at the boundary, giving you a clear stack trace instead of a downstream mystery crash.

Interview question

A team validates every API response with Zod, yet their application crashes when data that passes schema checks violates a business invariant. What does this reveal?

  • a.TypeScript interfaces alone would have prevented the crash since they persist in the compiled JavaScript.
  • b.Schema validation should be applied to every variable, including temporary internal state, to ensure complete coverage.
  • c.Schema libraries enforce data shape at runtime but cannot replace application-level business logic validation.Correct
  • d.The crashes prove that Zod's static types, like TypeScript interfaces, evaporate during compilation.
Why?

The card explicitly states that a schema confirms structural shape, not that business logic is valid. Distractor D confuses TypeScript's compile-time type erasure with Zod's runtime guards, which the card says compile into executable production checks.

Just read this? Test yourself on what you have been reading.

Read the original → zod.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles