oneOf
, anyOf
, and allOf
are keywords in the JSON Schema specification used to define composite data structures. They are essentially logical operators applied to data validation. Apidog fully supports these JSON Schema features, helping you build more accurate API documentation and data validation rules.Keyword | Logic Operation | Condition Requirement | Typical Use Case |
---|---|---|---|
allOf | AND | Must satisfy all conditions simultaneously | Inheritance, extension |
anyOf | OR | Must satisfy at least one condition | Optional combinations |
oneOf | XOR | Must satisfy exactly one condition | Mutually exclusive choices |
oneOf
, anyOf
, or allOf
. Define specific data structures for each subschema.allOf
allows you to combine multiple data schemas together, requiring data to satisfy all conditions simultaneously. This is like saying "user information must include basic information AND contact information":{
"allOf": [
{
"description": "Basic user information",
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" }
},
"required": ["id", "name"]
},
{
"description": "Contact information",
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" },
"phone": { "type": "string" }
},
"required": ["email"]
}
]
}
anyOf
provides multiple optional validation paths - users can choose any one or multiple methods. This is like saying "users can log in through username/password OR email/password OR phone/verification code":{
"type": "object",
"properties": {
"login": {
"anyOf": [
{
"description": "Username/password login",
"properties": {
"username": { "type": "string" },
"password": { "type": "string" }
},
"required": ["username", "password"]
},
{
"description": "Email/password login",
"properties": {
"email": { "type": "string", "format": "email" },
"password": { "type": "string" }
},
"required": ["email", "password"]
},
{
"description": "Phone/verification code login",
"properties": {
"phone": { "type": "string" },
"verifyCode": { "type": "string" }
},
"required": ["phone", "verifyCode"]
}
]
}
}
}
oneOf
ensures that users can only choose one payment method and cannot provide multiple methods simultaneously. This is like saying "payment method must be credit card XOR PayPal XOR bank transfer - exactly one of these options":{
"type": "object",
"properties": {
"payment": {
"oneOf": [
{
"description": "Credit card payment",
"type": "object",
"properties": {
"type": { "const": "credit_card" },
"cardNumber": { "type": "string" },
"expiryDate": { "type": "string" }
},
"required": ["type", "cardNumber", "expiryDate"],
"additionalProperties": false
},
{
"description": "PayPal payment",
"type": "object",
"properties": {
"type": { "const": "paypal" },
"email": { "type": "string", "format": "email" }
},
"required": ["type", "email"],
"additionalProperties": false
},
{
"description": "Bank transfer",
"type": "object",
"properties": {
"type": { "const": "bank_transfer" },
"accountNumber": { "type": "string" },
"routingNumber": { "type": "string" }
},
"required": ["type", "accountNumber", "routingNumber"],
"additionalProperties": false
}
]
}
}
}
allOf
(AND operation)anyOf
(OR operation)oneOf
(XOR operation)