In client-server architectures, relying solely on TypeScript type declarations is insufficient because TypeScript types are completely erased at runtime. When receiving JSON from untrusted external APIs, third-party webhooks, or user forms, runtime JSON Schema Validation (using libraries like Ajv or Zod) guarantees data integrity and security.
JSON Schema is a formal, language-agnostic IETF standard for annotating and validating JSON documents:
Ajv is the industry standard in JavaScript and Node.js for high-speed schema compilation:
"additionalProperties": false is Critical for SecurityIn REST endpoints and webhooks, attackers often submit unexpected payload keys (e.g. isAdmin: true or balance: 999999). This vulnerability is known as Mass Assignment.
Setting "additionalProperties": false ensures that any payload containing undeclared fields is immediately rejected!
| Dimension | TypeScript Interface | JSON Schema / Runtime Validator |
|---|---|---|
| Execution Phase | Compile time only | Runtime (evaluates live JSON over network) |
| Runtime Footprint | 0 bytes (Erased from JS output) | Validates live API responses and webhooks |
| Constraint Depth | Types only (string, number) | Constraints (minLength, regex pattern, minimum) |
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Circular Reference Handling & Custom Serialization | Streaming JSON & NDJSON |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.