MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
JSON Schema Validation0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
12.1JSON.stringify Replacer & JSON.parse Reviver12.2Circular Reference Handling & Custom Serialization12.3JSON Schema Validation12.4Streaming JSON & NDJSON
HomeTutorialsJavaScript for AdvancedJSON Schema Validation
Streaming JSON & NDJSON

JSON Schema Validation

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

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.


1. What is JSON Schema?

JSON Schema is a formal, language-agnostic IETF standard for annotating and validating JSON documents:

1{
2 "$schema": "http://json-schema.org/draft-07/schema#",
3 "title": "UserRegistrationPayload",
4 "type": "object",
5 "properties": {
6 "username": { "type": "string", "minLength": 3, "maxLength": 30 },
7 "email": { "type": "string", "format": "email" },
8 "age": { "type": "integer", "minimum": 18 },
9 "roles": {
10 "type": "array",
11 "items": { "type": "string", "enum": ["user", "editor", "admin"] }
12 }
13 },
14 "required": ["username", "email"],
15 "additionalProperties": false
16}

2. Validating with Ajv (Another JSON Schema Validator)

Ajv is the industry standard in JavaScript and Node.js for high-speed schema compilation:

1import Ajv from 'ajv';
2import addFormats from 'ajv-formats';
3 
4const ajv = new Ajv({ allErrors: true });
5addFormats(ajv); // Enable email, uri, date-time formats
6 
7const userSchema = {
8 type: 'object',
9 properties: {
10 name: { type: 'string', minLength: 2 },
11 email: { type: 'string', format: 'email' },
12 role: { type: 'string', enum: ['admin', 'member'] }
13 },
14 required: ['name', 'email', 'role'],
15 additionalProperties: false
16};
17 
18// Compile schema into high-speed validator function
19const validateUser = ajv.compile(userSchema);
20 
21// Validating data
22const incomingData = {
23 name: 'A', // Too short!
24 email: 'not-an-email', // Invalid format!
25 role: 'superadmin', // Not in enum!
26 hackedField: true // Not allowed by additionalProperties: false!
27};
28 
29const isValid = validateUser(incomingData);
30 
31if (!isValid) {
32 console.error('Validation Errors:', validateUser.errors);
33 // Prints detailed structured error list!
34} else {
35 console.log('Payload is safe and valid.');
36}

3. Why "additionalProperties": false is Critical for Security

In 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!

1Incoming Request ──► Contains { id: 1, role: 'admin' }
2 │
3 Schema: additionalProperties: false
4 │
5 ▼
6 REJECTED! Unknown property 'role' blocked!

4. JSON Schema vs. TypeScript

DimensionTypeScript InterfaceJSON Schema / Runtime Validator
Execution PhaseCompile time onlyRuntime (evaluates live JSON over network)
Runtime Footprint0 bytes (Erased from JS output)Validates live API responses and webhooks
Constraint DepthTypes only (string, number)Constraints (minLength, regex pattern, minimum)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why is TypeScript alone insufficient for validating incoming JSON API responses?
Question 2
What does additionalProperties: false enforce in a JSON Schema?
Question 3
What library is the industry benchmark for high-performance JSON Schema compilation and validation in Node.js and the browser?
Question 4
Which schema constraint ensures a string matches an email format in standard JSON Schema?
Question 5
In an Ajv validation instance, what property contains the list of validation failure descriptions when validate(data) returns false?
Next Lesson

Streaming JSON & NDJSON

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Circular Reference Handling & Custom SerializationStreaming JSON & NDJSON

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext