Dependent Required Properties in JSON Schema
Use dependentRequired in JSON Schema to enforce conditional property presence. If property A is present, property B must also be present. Draft 2020-12.
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"creditCard": {
"type": "string",
"pattern": "^\\d{13,19}$"
},
"billingAddress": {
"type": "string"
},
"couponCode": {
"type": "string"
},
"couponSource": {
"type": "string",
"enum": ["email", "social", "partner"]
}
},
"required": ["name"],
"dependentRequired": {
"creditCard": ["billingAddress"],
"couponCode": ["couponSource"]
}
}Test Data
{
"name": "Alice Chen",
"creditCard": "4111111111111111",
"billingAddress": "123 Main St, San Francisco, CA 94102",
"couponCode": "SAVE20",
"couponSource": "email"
}Detailed Explanation
Dependent Required Properties
The dependentRequired keyword creates conditional presence rules: if a certain property exists in the object, then one or more other properties must also exist. This is different from required, which always mandates presence.
Basic Syntax
{
"dependentRequired": {
"creditCard": ["billingAddress", "expiryDate"]
}
}
This means: if the creditCard property is present, then billingAddress and expiryDate must also be present. If creditCard is absent, the rule is not enforced — billingAddress and expiryDate can be absent too.
How the Example Schema Works
The schema models a checkout form with two dependency rules:
creditCard→billingAddress: If a credit card number is provided, the billing address becomes mandatory. You cannot submit a card without an address to charge it to.couponCode→couponSource: If a coupon code is used, the marketing source must be specified. This enables analytics tracking for promotional campaigns.
The test data passes because:
creditCardis present, and so isbillingAddress— dependency satisfied.couponCodeis present, and so iscouponSource— dependency satisfied.nameis present, satisfying therequiredconstraint.
One-Way Dependencies
Note that dependentRequired is one-directional. The rule "creditCard": ["billingAddress"] does NOT mean the reverse — a billingAddress can exist without a creditCard. If you need bidirectional dependencies, declare both directions:
{
"dependentRequired": {
"creditCard": ["billingAddress"],
"billingAddress": ["creditCard"]
}
}
Draft History
- Draft 2020-12: Uses
dependentRequired(present example). - Draft 7 and earlier: Used
dependencieskeyword, which combined both property dependencies and schema dependencies in a single keyword. The modern drafts split this intodependentRequired(property presence) anddependentSchemas(conditional schema application).
Comparison with if/then
dependentRequired only checks property presence. If you need to check property values (e.g., "if paymentMethod is credit_card, then require cardNumber"), use if/then/else instead. See the conditional-if-then-else example for that pattern.
When to Use
- Payment forms: card number requires billing address and expiry
- Shipping: domestic shipping requires state; international requires country code
- Analytics: campaign code requires campaign source and medium
Use Case
Use dependentRequired for checkout flows where payment details require billing information, marketing forms where campaign codes need source tracking, and any API where the presence of one field logically implies the need for another companion field.