liveBook · Manning

JSON Schema

As explained in Data-Oriented Programming: Reduce software complexity

JSON Schema is a language and vocabulary for describing constraints on JSON data and validating JSON instances against those constraints.

A client request arrives as a plain map. Should the application trust its shape, or surround every access with defensive checks? JSON Schema offers a cleaner boundary: describe the data separately, then validate the representation before it enters the system. section 7.0 section 7.2

A schema is itself a JSON object, commonly beginning with type and properties. Objects describe maps, arrays describe collections, items describe array elements, and required names make selected fields mandatory. Map fields remain optional by default. section 7.2 section 7.3

Validating a search request

A search request can require title and fields while restricting each field name to an allowed enumeration. The request {"title":"habit","fields":["title","weight"]} validates successfully; a misspelled "tilte" does not. section 7.2 section 7.2

Listing 7.6 Schema of a search request
var searchBooksRequestSchema = {
  "type": "object",
  "properties": {
    "title": {"type": "string"},
    "fields": {
      "type": "array",
      "items": {
        "enum": [
          "publishers",
          "number_of_pages",
          "weight",
          "physical_format",
          "subjects",
          "publish_date",
          "physical_dimensions"
        ]
      }
    }
  },
  "required": ["title", "fields"]
};

Inspect the complete search-request schema, including required fields and permitted field names. section 7.2

JSON Schema is deliberately more composable than a class-shaped contract. Store nested schemas in variables, reuse them inside larger schemas, and combine constraints with compositions such as allOf. This matters when an external response requires title plus at least one of isbn_10 or isbn_13. section 7.3 section 7.4

Flexible input, strict output

A database response may contain an unexpected dummy_property. Accepting it can make received data more tolerant; setting additionalProperties to false rejects it. The practical rule is simple: be flexible when receiving data and strict when sending it. section 7.3 section 7.6

Validation need not stop at primitive types. Number ranges constrain publication years, pattern constrains strings such as UUIDs, and format can describe dates. Runtime validation therefore checks properties that static types alone do not express. section 12.3

Use JSON Schema for two distinct jobs:

  • Guard system boundaries by validating client requests, service responses, database results, and other external data. section 7.0
  • Clarify development-time function contracts by validating argument shapes and producing detailed errors; disable this internal validation in production. section 12.1 section 12.7
  • Treat schemas as ordinary maps that can be stored, composed, diagrammed, and used to generate schema-based unit tests. section 7.3 section 12.7
Listing 7.15 Disallowing properties not mentioned in the schema
var booksFromDBSchema = {
  "type": "array",
  "items": {
    "type": "object",
    "required": ["title", "isbn", "available"],
    "additionalProperties": false,
    "properties": {
      "title": {"type": "string"},
      "available": {"type": "boolean"},
      "isbn": {"type": "string"}
    }
  }
};

Compare a database-response schema with additionalProperties enabled or disabled to see how strictness changes acceptance. section 7.3

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Descends from object and array schemas through composition, strictness, runtime ranges, patterns, formats, and function-argument validation.
Examples
Demonstrates book-search requests, database responses, Open Library responses, catalog data, UUIDs, dates, and function arguments.
What it assumes
Assumes JSON maps and arrays while teaching schema vocabulary, required fields, composition, and constraints through concrete data.
What you can do after
Build it from scratch by defining schemas, composing constraints, validating data with Ajv, and interpreting validation failures.
How it connects
Connects data validation with data representation, system boundaries, external APIs, static types, runtime validation, and unit testing.

Also explained in

Compare how 4 books explain JSON Schema →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.