Skip to main content

Combine constraints

Use composition when an output must satisfy several requirements, match an alternative, or exclude a particular combination. Each keyword combines schemas that constrain the same value.

KeywordRequirement
allOfEvery member schema matches.
anyOfAt least one member matches; overlap is allowed.
oneOfExactly one member matches; overlap is rejected.
notIts schema does not match.

These are validation rules, not an ordered list of instructions. anyOf does not select the first match, and oneOf does not ask the model to choose a branch identifier. See the JSON Schema composition reference.

Require every rule with allOf​

Send a notification when the request is declined, when it is urgent, or both. Each condition applies independently.

composition-allof.schema.json
{
"type": "object",
"properties": {
"status": {
"enum": [
"approved",
"declined"
],
"description": "The outcome of the request."
},
"urgent": {
"type": "boolean",
"description": "Whether the request needs urgent attention."
},
"notify": {
"type": "boolean",
"description": "Whether a notification should be sent.",
"enum": [
false,
true
]
}
},
"required": [
"status",
"urgent",
"notify"
],
"additionalProperties": false,
"allOf": [
{
"if": {
"properties": {
"status": {
"const": "declined"
}
},
"required": [
"status"
]
},
"then": {
"properties": {
"notify": {
"const": true
}
}
}
},
{
"if": {
"properties": {
"urgent": {
"const": true
}
},
"required": [
"urgent"
]
},
"then": {
"properties": {
"notify": {
"const": true
}
}
}
}
]
}

If either trigger is true, notify must be true. When the request is approved and not urgent, these rules leave notify unconstrained beyond its boolean type. Add another rule if that case must force false.

allOf does not merge object definitions or override a conflicting constraint. Declare the example’s fields together and close the object with additionalProperties: false at that level. An allOf member cannot add a field that another member’s closed-object rule forbids.

Allow overlapping alternatives with anyOf​

Choose an integer from 1 through 6 that is divisible by 2 or 3.

composition-anyof.schema.json
{
"type": "integer",
"minimum": 1,
"maximum": 6,
"description": "Choose a value satisfying the divisibility rule.",
"anyOf": [
{
"multipleOf": 2
},
{
"multipleOf": 3
}
]
}

Allowed values: 2, 3, 4, 6. The value 6 matches both branches, which is valid for anyOf.

Require one matching alternative with oneOf​

Changing the same rule to oneOf excludes values that satisfy both alternatives.

composition-oneof.schema.json
{
"type": "integer",
"minimum": 1,
"maximum": 6,
"description": "Choose a value satisfying the divisibility rule.",
"oneOf": [
{
"multipleOf": 2
},
{
"multipleOf": 3
}
]
}

Allowed values: 2, 3, 4. The value 6 is rejected because it matches twice. Use distinct const tags when alternative object shapes should be unambiguous.

Exclude combinations with not​

Allow email or SMS notifications, or neither, but never both together.

composition-not.schema.json
{
"type": "object",
"properties": {
"send_email": {
"type": "boolean",
"description": "Whether to notify by email."
},
"send_sms": {
"type": "boolean",
"description": "Whether to notify by SMS."
}
},
"required": [
"send_email",
"send_sms"
],
"additionalProperties": false,
"not": {
"properties": {
"send_email": {
"const": true
},
"send_sms": {
"const": true
}
},
"required": [
"send_email",
"send_sms"
]
}
}

false/false, true/false and false/true are valid. Only true/true is rejected. not excludes a matching value; it does not remove or rewrite fields.

Send the schema​

All examples above are complete downloadable schema documents. Save the first as composition-allof.schema.json and run this client to use it directly:

Before you run this example

Call https://api.blatent.ai from your server. Set your API key in the same terminal where you run the example:

export BLATENT_API_KEY='YOUR_SERVICE_KEY'

Python requires Python 3.10+ and HTTPX:

python -m pip install "httpx>=0.28,<1"

TypeScript requires Node.js 24+ and uses its built-in fetch. The cURL examples require curl and jq.

Save the displayed files and the selected client in one directory. Run the client there with python example.py, node example.ts or bash example.sh. Downloads use the example's name in place of example.

import json
import os
from pathlib import Path
import httpx

request = {
"input": "The request is declined and urgent. Notify the requester.",
"schema": json.loads(Path("composition-allof.schema.json").read_text(encoding="utf-8")),
}
with httpx.Client(
base_url="https://api.blatent.ai",
headers={"Authorization": f"Bearer {os.environ['BLATENT_API_KEY']}"},
timeout=195,
) as client:
response = client.post("/v1/resolve", json=request)
response.raise_for_status()
print(json.dumps(response.json()["output"]))

Illustrative output: {"status":"declined","urgent":true,"notify":true}. To use another schema above, change the loaded filename and the input. You can also register a schema and use its returned identifier.

Support depends on the value domains as well as the keywords. These finite examples do not imply arbitrary anyOf, oneOf or not over open strings and numbers. Complicated combinations may also exceed compilation limits. Consult the support boundary.

To define a common schema once and use it in several fields, see Reuse schema definitions.