ProgramGuru

Runtime Data Validation

Runtime Data Validation — why it matters

External JSON, form input, and environment variables enter the program as runtime values whose shapes cannot be trusted merely by assigning a TypeScript type. Validate the data and narrow from unknown into a domain type. Keep error messages useful without leaking sensitive input.

What you’ll learn

  • Explain runtime data validation and the problem it solves.
  • Identify the relevant compile-time checks and runtime responsibilities.
  • Run the example, predict its output, and try a realistic modification.

Understand the mechanism

The type guard checks both required properties at runtime. The first payload is accepted and the second rejected because its id is a string even though it looks numeric.

JSON.parse(...) as User only instructs the compiler to trust you; it never validates the parsed object.

Follow the concept flow

Runtime Data Validation — concept flowunknown payload → Runtime type guard → Trusted User shapeRuntime Data Validation1unknown payload2Runtime type guard3Trusted User shapeTests exercise behavior, while types check static contracts.
Follow the responsibilities from input to checked result.

Run the primary TypeScript example

Save as runtime-data-validation.ts. The following program passed strict TypeScript compilation, and the displayed output was captured by executing its emitted JavaScript with Node.js 22.16.0.

TypeScript source

type User = { id: number; name: string };
function isUser(value: unknown): value is User {
  return typeof value === "object" && value !== null && "id" in value && typeof value.id === "number" && "name" in value && typeof value.name === "string";
}
const payloads: unknown[] = [{ id: 3, name: "Ada" }, { id: "3", name: "Ada" }];
console.log(payloads.map(value => isUser(value) ? value.name : "Invalid").join(", "));

Verified output

Ada, Invalid

Compile: npx tsc --strict --target es2022 --module commonjs runtime-data-validation.ts; run: node runtime-data-validation.js. For Node.js API examples, install @types/node first. For browser projects, use the browser-specific instructions below.

The example demonstrates the code-level behavior; the related configuration or test commands are shown separately when needed.

Trace the result

The type guard checks both required properties at runtime. The first payload is accepted and the second rejected because its id is a string even though it looks numeric.

Predict what changes if you alter one input. Then run the code to compare your reasoning with the real output.

Common mistake and correction

JSON.parse(...) as User only instructs the compiler to trust you; it never validates the parsed object.

Identify whether the failure is a type-checking issue, a configuration issue, or a runtime behavior issue before choosing a fix.

Practice the concept

Add an email property and validate it; try missing properties, null, and arrays.

Write down your expected result first. For configuration examples, compare compiler output and diagnostics; for test examples, make the assertion fail deliberately to verify that the tool reports it.

Check your understanding

Which statement makes a trustworthy TypeScript testing practice?
Choose one answer. You can try again.

Answer and explanation

A test must exercise behavior and assert an expected outcome; compiler checks and test results provide different evidence.

The type guard checks both required properties at runtime. The first payload is accepted and the second rejected because its id is a string even though it looks numeric.

Official reference

For the exact current options and platform behavior, consult the official documentation. These examples were verified using the tool versions described above.

What to remember

External JSON, form input, and environment variables enter the program as runtime values whose shapes cannot be trusted merely by assigning a TypeScript type.

Run the primary example, then complete the practice task or environment-specific lab.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Refactoring TypeScript Code →