JSON and data guides
JSON Schema local definitions: reuse constraints with $defs
Build self-contained schemas with local references and diagnose unresolved paths without remote requests.
Put shared constraints in a definition
A definition keeps one constraint in one place. Properties can refer to it rather than duplicating a pattern or range in several locations.
The example applies the same nonnegative integer rule to both counters. Use Schema Validator with a small valid object first.
{"$defs":{"count":{"type":"integer","minimum":0}},
"type":"object","properties":{
"sent":{"$ref":"#/$defs/count"},
"received":{"$ref":"#/$defs/count"}}}A definition does not require a property
The properties keyword constrains fields when present. A definition and a reference do not make those fields mandatory.
Add required at the enclosing object if missing counters must fail. Test a negative counter separately from a missing counter to distinguish the rules.
Resolve paths inside the document
A fragment beginning with # points inside this schema document. A misspelled definition name cannot be resolved by examining the data.
Nav Tools rejects remote schema references. Bundle referenced definitions into the document rather than expecting the validator to download another schema.
Version the schema contract
Changing a shared definition changes every referencing property. Recheck all fixtures when tightening a constraint.
Use the 2020-12 $defs vocabulary here; older drafts commonly use definitions. Verify the target validator dialect before sharing the document.