liveBook · Manning

prefixItems

As explained in Data-Oriented Programming: Reduce software complexity

prefixItems is a JSON Schema keyword that specifies schemas for the positional elements of an array.

When a function accepts an ordered mix of values, ordinary homogeneous-array validation is not enough. prefixItems assigns a separate schema to each positional element, turning an array into a tuple. section 12.1

Validate a catalog search call

A catalog search takes two arguments: catalog data first and a query string second. Its argument array can therefore use prefixItems to require the first position to match catalogSchema and the second to be a string. AJV can validate this array during development and testing, while production skips the repeated internal check. section 12.1

Listing 12.7 The schema for a tuple made of a string and a number
{
  "type": "array",
  "prefixItems": [
    { "type": "string" },
    { "type": "number" }
  ]
}

Compare the compact tuple schema: the first position accepts a string and the second accepts a number. section 12.1

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Demonstrates tuple syntax with two positional schemas and applies it to search arguments, without developing broader tuple constraints.
Examples
Uses a string-number tuple and a catalog-plus-query argument array for searchBooksByTitle.
What it assumes
Assumes basic JSON Schema arrays and objects, while introducing tuples through a concrete string-and-number example.
What you can do after
Apply it via AJV to validate ordered function arguments outside production execution.
How it connects
Connects tuples and arrays with JSON Schema, function argument validation, system boundaries, and development-time assertions.

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.