JSON Schema Composition

Definition
Definition and Usage
Real schemas grow: nested objects, optional branches, and shared pieces. Composition keywords combine subschemas; $ref reuses them by URI.
Combine
Boolean Combination
- allOf Must satisfy every subschema
- anyOf Must satisfy at least one
- oneOf Must satisfy exactly one
- not Must fail the given subschema
Reuse
$ref and External Schemas
Point a property at another schema document—common for shared address or geo types:
{
"warehouseLocation": {
"description": "Coordinates of the warehouse",
"$ref": "https://example.com/geographical-location.schema.json"
}
}
Code
Examples
Use View Output for a quick result, or Try It Yourself to edit and run in the browser.
Example 1 — Nested dimensions object
Embed an object schema inside properties.
const schema = {
type: "object",
properties: {
dimensions: {
type: "object",
properties: {
length: { type: "number" },
width: { type: "number" },
height: { type: "number" }
},
required: ["length", "width", "height"]
}
}
};
const instance = { dimensions: { length: 1, width: 2, height: 3 } };
const d = instance.dimensions;
const keys = schema.properties.dimensions.required;
console.log(keys.every(function (k) { return k in d; }));
console.log(d.length, d.width, d.height);
How It Works
- Nested
properties describe inner objects. - Inner
required only applies inside that object. - This matches the official dimensions example.
Example 2 — anyOf for string or number id
Accept either representation.
const schema = {
anyOf: [
{ type: "string", minLength: 1 },
{ type: "integer" }
]
};
function anyOfOk(value) {
const asString = typeof value === "string" && value.length >= 1;
const asInt = typeof value === "number" && Number.isInteger(value);
return asString || asInt;
}
console.log(anyOfOk("A-1"));
console.log(anyOfOk(42));
console.log(anyOfOk(true));
How It Works
anyOf passes if any branch matches.oneOf would fail if two branches matched.- Use this for flexible ID formats.
Example 3 — Simulate $ref lookup
Resolve a local map of schemas by $id.
const registry = {
"https://example.com/geo.schema.json": {
type: "object",
required: ["latitude", "longitude"],
properties: {
latitude: { type: "number" },
longitude: { type: "number" }
}
}
};
const productProperty = {
$ref: "https://example.com/geo.schema.json"
};
const geo = registry[productProperty.$ref];
console.log(geo.required.join(", "));
console.log(geo.type);
latitude, longitude
object
How It Works
$ref is a pointer to another schema URI.- Validators load that document and apply it.
- Reuse keeps shared shapes in one place.
Frequently Asked Questions
The instance must be valid against every subschema in the allOf array.
anyOf needs at least one match. oneOf needs exactly one match.
$ref points at another schema by URI (often an $id) so you can reuse shared definitions.
Yes. Put another type: object with properties inside a property—nesting is normal.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
10 people found this page helpful