Skip to main content
Use conditional keywords when required fields depend on other values.

Supported keywords

KeywordWhat it does
if / then / elseApply different schemas based on a condition
dependentRequiredRequire companion fields when a field is present

if / then / else

Validate one branch when a condition matches, and another branch otherwise.
Pydantic and Zod examples are omitted for this example because there is no direct keyword mapping for JSON Schema if/then/else.
JSON Schema
{
  "type": "object",
  "properties": {
    "delivery_method": { "type": "string", "enum": ["shipping", "pickup"] },
    "address": { "type": "string", "minLength": 10 },
    "pickup_location": { "type": "string", "enum": ["warehouse-a", "warehouse-b"] }
  },
  "required": ["delivery_method"],
  "if": {
    "properties": { "delivery_method": { "enum": ["shipping"] } },
    "required": ["delivery_method"]
  },
  "then": {
    "required": ["address"]
  },
  "else": {
    "required": ["pickup_location"]
  },
  "additionalProperties": false
}
In the JSON Schema above, if the value of the property delivery_method is “shipping”, then the property address is required. Otherwise, it’s the property pickup_location that is required.

Multiple independent conditions

Use allOf with multiple if/then blocks when separate rules should all apply.
JSON Schema
{
  "type": "object",
  "properties": {
    "account_type": { "type": "string", "enum": ["business", "personal"] },
    "country": { "type": "string", "enum": ["US", "CA"] },
    "company_name": { "type": "string" },
    "state": { "type": "string" }
  },
  "required": ["account_type", "country"],
  "allOf": [
    {
      "if": {
        "properties": { "account_type": { "enum": ["business"] } },
        "required": ["account_type"]
      },
      "then": {
        "required": ["company_name"]
      }
    },
    {
      "if": {
        "properties": { "country": { "enum": ["US"] } },
        "required": ["country"]
      },
      "then": {
        "required": ["state"]
      }
    }
  ],
  "additionalProperties": false
}
In the JSON Schema above, there are two different conditionals that both apply. If the property account_type is equal to “business” then the property company_name is required and, independently of it, if the property country is equal to “US”, then the property state is required. Thus, if both conditions evaluate to true, then both properties company_name and state are required.

dependentRequired

Require companion fields when a field is present.
JSON Schema
{
  "type": "object",
  "properties": {
    "email": { "type": "string", "format": "email" },
    "email_verified": { "type": "boolean" },
    "phone": { "type": "string" },
    "phone_verified": { "type": "boolean" }
  },
  "dependentRequired": {
    "email": ["email_verified"],
    "phone": ["phone_verified"]
  },
  "additionalProperties": false
}
In the JSON Schema above, if the property email is present, then the property email_verified must also be present. Similarly, if the property phone is present, then the property phone_verified must also be present. Use dependentRequired for presence-based dependencies, and if/then/else for value-based dependencies.