← All Exercises

JSON Schema Validation

intermediatejavascript~15min

Background

Runtime data validation is critical for API input processing, configuration loading, and defensive programming.

Problem

Write a function that takes an object and a schema (mapping field names to expected types) and returns whether the object matches the schema.

Input

An object to validate and a schema object mapping field names to type strings.

Output

A boolean indicating whether the object matches all schema requirements.

Sample tests

Sample 1

Input: {name:"John", age:30}, {name:"string", age:"number"}
Expected: true

Your solution

Learning objective

Learn runtime type checking and schema-based data validation patterns.