JSON JSON Schema

Validate JSON data with schemas, types, required fields, formats, composition, and API contracts.

Lessons

Sign in to save progress
1 JSON Schema Tutorial 2 JSON Schema HOME 3 JSON Schema Introduction 4 JSON Basics 5 JSON Data Types 6 JSON Syntax Rules 7 JSON vs JSON Schema 8 What Is JSON Schema 9 Your First Schema 10 The $schema Keyword 11 JSON Schema Drafts 12 JSON Schema Basics 13 The type Keyword 14 Multiple Types 15 Validating Strings 16 String Length 17 Pattern (Regex) 18 Validating Numbers 19 Integers 20 Number Ranges 21 multipleOf 22 Booleans & Null 23 Nullable Values 24 Enumerated Values (enum) 25 Constant Values (const) 26 Validating Objects 27 The properties Keyword 28 Required Properties 29 additionalProperties 30 patternProperties 31 propertyNames 32 minProperties & maxProperties 33 dependentRequired 34 dependentSchemas 35 Validating Arrays 36 Array items 37 Tuples with prefixItems 38 minItems & maxItems 39 uniqueItems 40 Nested Objects & Arrays 41 contains, minContains & maxContains 42 Validation Keywords 43 The format Keyword 44 Date & Time Formats 45 Email & URI Formats 46 contentEncoding & contentMediaType 47 Annotations: title & description 48 The default Keyword 49 The examples Keyword 50 readOnly & writeOnly 51 The deprecated Keyword 52 Composition with oneOf, anyOf and allOf 53 allOf 54 anyOf 55 oneOf 56 The not Keyword 57 Conditionals: if, then, else 58 Tagged Unions 59 Combining Keywords 60 Boolean Schemas 61 $ref, $defs and Reuse 62 The $ref Keyword 63 Local Definitions with $defs 64 The $id Keyword 65 The $anchor Keyword 66 External References 67 Recursive Schemas 68 Bundling Schemas 69 JSON Pointer 70 JSON Schema for APIs 71 JSON Schema in OpenAPI 72 Validating API Requests 73 Validating API Responses 74 Validating Config Files 75 Generating Forms from Schema 76 Generating Types from Schema 77 Human-Friendly Validation Errors 78 Custom Error Messages 79 Validating with Ajv 80 Ajv Formats & Keywords 81 Validating in Python 82 Validating in PHP 83 Validating from the CLI 84 Schemas in Your Editor 85 SchemaStore & Common Schemas 86 The Meta-Schema 87 Custom Keywords 88 Vocabularies 89 JSON Schema Best Practices 90 Common JSON Schema Mistakes 91 Testing Your Schemas 92 Versioning Schemas 93 JSON Schema Cheatsheet 94 JSON Schema Interview Questions 95 JSON Schema Quiz 96 JSON Schema Exercises 97 JSON Schema Practice Problems 98 JSON Schema Project Ideas 99 JSON Schema Syllabus 100 JSON Schema Study Plan 101 JSON Schema Bootcamp

Generating Types from Schema

JSON Schema Lesson 76 of 101 ~1 min read

Overview

Produce TypeScript or other types from schemas.

Generating Types from Schema is easiest to learn by reading the example, changing it, and observing the result.

Core Ideas

  • Understand what Generating Types from Schema changes.
  • Run the example.
  • Change one value.
  • Explain the result.

Step by Step

  1. Read the Generating Types from Schema example.
  2. Run it.
  3. Change it.
  4. Explain it.

Code Example

// Generate TypeScript types from a JSON Schema
// npx json-schema-to-typescript schema.json > types.ts

export interface Lesson {
  slug: string;
  title: string;
  minutes?: number;
  tags?: string[];
}

Checklist

  • Read the example and change one value.
  • Check the result in the browser.
  • Write down the rule you learned.

Common Mistakes

  • Skipping the example.
  • Changing many things at once.
  • Not checking the result.

Practice Challenge

Practice the Generating Types from Schema example in a small scratch file, then explain what changed and why.

Mastery Check

  • You can explain Generating Types from Schema.
  • You can change the example.
  • You can debug the result.
Create a free account to save which lessons you've finished. Save my progress