liveBook · Manning

Schema strictness

As explained in Data-Oriented Programming: Reduce software complexity

Schema strictness is the degree to which a schema rejects data that contains unspecified or otherwise nonconforming structure.

Schema strictness is the choice between accepting unspecified fields and rejecting them. In JSON Schema, extra object fields are allowed by default; setting additionalProperties to false makes the schema strict. section 7.3

A harmless field—or a contract bug?

A database response containing dummy_property may look harmless because unused fields are ignored. But a response schema can reject it by setting additionalProperties to false. A practical rule is to accept extra fields in client requests while strictly controlling server responses: be liberal in what you accept and conservative in what you send. section 7.3

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"}
    }
  }
};

See the strict database-response schema that rejects properties not explicitly listed. section 7.3

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains default flexibility, the additionalProperties switch, and the rationale for different request and response policies.
Examples
Uses database and search responses containing an unwanted dummy_property field to demonstrate flexible and strict schemas.
What you can do after
Operate/configure JSON Schema validation by choosing whether unspecified object properties are accepted or rejected.
How it connects
Links JSON Schema validation with object properties, request and response schemas, and the robustness principle.

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.