Depending on your situation, there are a few different approaches. I can think of four different ways to conditionally require a field.

Dependencies

The dependentSchemas keyword is a conditional way to apply a schema. Foreach property in dependentSchemas, if the property is present in the JSON being validated, then the schema associated with that key must also be valid. If the "foo" property is present, then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "dependentSchemas": {
    "foo": { "required": ["bar"] }
  }
}

If all the dependent schema needs is the required keyword, you can use the dependentRequired keyword as a shorthand. The following has the same effect as the previous example.

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "dependentRequired": {
    "foo": ["bar"]
  }
}

NOTE: In draft-07 and below these were one keyword called dependencies. If the value is a schema it behaved like dependentSchemas. If the value is an array, it behaved like dependentRequired.

Implication

If your condition depends on the value of a field, you can use a boolean logic concept called implication. "A implies B" effectively means, if A is true then B must also be true. Implication can also be expressed as "!A or B". Either the "foo" property does not equal "bar", or the "bar" property is required. Or, in other words: If the "foo" property equals "bar", Then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "anyOf": [
    {
      "not": {
        "properties": {
          "foo": { "const": "bar" }
        },
        "required": ["foo"]
      }
    },
    { "required": ["bar"] }
  ]
}

If "foo" is not equal to "bar", #/anyOf/0 matches and validation succeeds. If "foo" equals "bar", #/anyOf/0 fails and #/anyOf/1 must be valid for the anyOf validation to be successful.

NOTE: The if/then keywords have the same behavior, but are easier to read and maintain. It's recommended to only use this approach if you are using an older version of JSON Schema that doesn't support if/then.

Enum

If your conditional is based on an enum, it's a little more straight forward. "foo" can be "bar" or "baz". If "foo" equals "bar", then "bar" is required. If "foo" equals "baz", then "baz" is required.

{
  "type": "object",
  "properties": {
    "foo": { "enum": ["bar", "baz"] },
    "bar": { "type": "string" },
    "baz": { "type": "string" }
  },
  "anyOf": [
    {
      "properties": {
        "foo": { "const": "bar" }
      },
      "required": ["bar"]
    },
    {
      "properties": {
        "foo": { "const": "baz" }
      },
      "required": ["baz"]
    }
  ]
}

NOTE: This approach is not recommended because it can produce confusing error messages. The if/then keywords are generally a better approach.

If-Then-Else

The if, then and else keywords are shorthand for the implication pattern described above. These keywords were added in draft-07. If the "foo" property equals "bar", Then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "if": {
    "properties": {
      "foo": { "const": "bar" }
    },
    "required": ["foo"]
  },
  "then": { "required": ["bar"] }
}

EDIT 12/23/2017: Implication section updated and If-Then-Else section added.

EDIT 06/04/2018: Bugfix for If-Then-Else and update singleton enums to use const.

EDIT 07/06/2022: Update Dependencies section to use the new dependentSchemas/dependentRequired keywords instead of dependencies.

Answer from Jason Desrosiers on Stack Overflow
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › conditionals
JSON Schema - Conditional schema validation
Even if "country" was a required field, it's still recommended to have the required keyword in each if schema. The validation result will be the same because required will fail, but not including it could add noise to error results because it will validate the "postal_code" against all three of the then schemas leading to irrelevant errors.
🌐
JSON Schema
json-schema.org › learn › miscellaneous-examples
JSON Schema - Miscellaneous Examples
{ "$id": "https://example.com/conditional-validation-if-else.schema.json", "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "Conditional Validation with If-Else", "type": "object", "properties": { "isMember": { "type": "boolean" }, "membershipNumber": { "type": "string" } }, "required": ["isMember"], "if": { "properties": { "isMember": { "const": true } } }, "then": { "properties": { "membershipNumber": { "type": "string", "minLength": 10, "maxLength": 10 } } }, "else": { "properties": { "membershipNumber": { "type": "string", "minLength": 15 } } }} 
Top answer
1 of 3
456

Depending on your situation, there are a few different approaches. I can think of four different ways to conditionally require a field.

Dependencies

The dependentSchemas keyword is a conditional way to apply a schema. Foreach property in dependentSchemas, if the property is present in the JSON being validated, then the schema associated with that key must also be valid. If the "foo" property is present, then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "dependentSchemas": {
    "foo": { "required": ["bar"] }
  }
}

If all the dependent schema needs is the required keyword, you can use the dependentRequired keyword as a shorthand. The following has the same effect as the previous example.

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "dependentRequired": {
    "foo": ["bar"]
  }
}

NOTE: In draft-07 and below these were one keyword called dependencies. If the value is a schema it behaved like dependentSchemas. If the value is an array, it behaved like dependentRequired.

Implication

If your condition depends on the value of a field, you can use a boolean logic concept called implication. "A implies B" effectively means, if A is true then B must also be true. Implication can also be expressed as "!A or B". Either the "foo" property does not equal "bar", or the "bar" property is required. Or, in other words: If the "foo" property equals "bar", Then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "anyOf": [
    {
      "not": {
        "properties": {
          "foo": { "const": "bar" }
        },
        "required": ["foo"]
      }
    },
    { "required": ["bar"] }
  ]
}

If "foo" is not equal to "bar", #/anyOf/0 matches and validation succeeds. If "foo" equals "bar", #/anyOf/0 fails and #/anyOf/1 must be valid for the anyOf validation to be successful.

NOTE: The if/then keywords have the same behavior, but are easier to read and maintain. It's recommended to only use this approach if you are using an older version of JSON Schema that doesn't support if/then.

Enum

If your conditional is based on an enum, it's a little more straight forward. "foo" can be "bar" or "baz". If "foo" equals "bar", then "bar" is required. If "foo" equals "baz", then "baz" is required.

{
  "type": "object",
  "properties": {
    "foo": { "enum": ["bar", "baz"] },
    "bar": { "type": "string" },
    "baz": { "type": "string" }
  },
  "anyOf": [
    {
      "properties": {
        "foo": { "const": "bar" }
      },
      "required": ["bar"]
    },
    {
      "properties": {
        "foo": { "const": "baz" }
      },
      "required": ["baz"]
    }
  ]
}

NOTE: This approach is not recommended because it can produce confusing error messages. The if/then keywords are generally a better approach.

If-Then-Else

The if, then and else keywords are shorthand for the implication pattern described above. These keywords were added in draft-07. If the "foo" property equals "bar", Then the "bar" property is required

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "if": {
    "properties": {
      "foo": { "const": "bar" }
    },
    "required": ["foo"]
  },
  "then": { "required": ["bar"] }
}

EDIT 12/23/2017: Implication section updated and If-Then-Else section added.

EDIT 06/04/2018: Bugfix for If-Then-Else and update singleton enums to use const.

EDIT 07/06/2022: Update Dependencies section to use the new dependentSchemas/dependentRequired keywords instead of dependencies.

2 of 3
6

As of 2022, dependencies has been deprecated, and split into dependentRequired (see e.g. this example) and dependentSchemas (see e.g. this example). Just using dependentRequired solves the issue:

{
  "type": "object",
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "string" }
  },
  "dependentRequired": {
    "foo": ["bar"]
  }
}
🌐
JSON Forms community
jsonforms.discourse.group › general
Json Schema validation using If-Else - "conditionally choosing required element" not working - General - JSON Forms community
February 10, 2023 - Below is the JSON schema: Schema { "type": "object", "properties": { "Product": { "type": "object", "if": { "properties": { "Retention": { "type": "object" } } }, "then": { "properties": { "Retention": { "required": [ "AllowedRetention" ] } } }, "else": { "properti...
🌐
Ajv
ajv.js.org › json-schema.html
JSON Schema | Ajv JSON schema validator
Each value in the map should be either an array of unique property names ("property dependency" - see dependentRequired keyword) or a JSON Schema ("schema dependency" - see dependentSchemas keyword). For property dependency, if the data object contains a property that is a key in the keyword value, then to be valid the data object should also contain all properties from the array of properties.
🌐
Liquid Technologies
liquid-technologies.com › Reference › XmlStudio › JsonEditorNotation_IfThenElse.html
If Then Else
The 3 properties If, Then, Else make it possible to specify conditional validation rules for a schema. Each of them contains a JSON Schema.
🌐
Json-schema
tour.json-schema.org › content › 05-Conditional-Validation › 01-Ensuring-Conditional-Property-Presence
Ensuring Conditional Property Presence: Conditional Validation | A Tour of JSON Schema
... To achieve this, you can use the dependentRequired keyword in JSON Schema. The dependentRequired keyword allows you to specify that a property is required if another property is present.
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › object
JSON Schema - object
{ "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" }, "type": { "enum": ["residential", "business"] } }, "required": ["street_address", "city", "state", "type"], "if": { "type": "object", "properties": { "type": { "const": "business" } }, "required": ["type"] }, "then": { "properties": { "department": { "type": "string" } } }, "unevaluatedProperties": false}
Find elsewhere
🌐
Opis
opis.io › json-schema › 2.x › conditional-subschemas.html
Applying subschemas conditionally | Opis JSON Schema
The value of this keyword must be a valid JSON schema (object or boolean). ... Please pay attention when using not! You could unintentionally write schemas that never validate! ... This is a conditional structure containing three keywords: if, then and else. Every keyword value must be a valid ...
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 539
Field required based on condition · Issue #539 · json-schema-org/json-schema-spec
January 23, 2018 - More in general, let's suppose to have numeric fields A, B and C and that field A is required only if B > C. How to accomplish that? My understanding about draft-07 specification is that this is not possible, however it could be very useful. I think that the best solution could be to change the current specification of the required property: "required": { "$ref": "#/definitions/stringArray" } to something more complex, supporting the specification of objects instead of a stringArray, giving the possibility to specify boolean conditions.
Author   brunodymios
🌐
Reddit
reddit.com › r/askprogramming › need help implementing conditional requirements in json schema?
r/AskProgramming on Reddit: Need help implementing conditional requirements in JSON schema?
January 29, 2023 -

I'm using AWS's API Gateway to expose an HTTP API. It allows me to use JSON schema's to map and validate the input body, and I'd like to implement conditional requirements, however, I'm not sure how. Also, I'm not sure whether I should be doing this, or creating a second JSON schema.

In this example, I have an API that can take in two completely different things, apples & oranges if you will. I have the below schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Apples2Oranges",
    "type": "object",
    "properties": {
      "apples": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "oranges": {
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "apple-property": { "type": "string" }
}

What I want to do is require that the body contains an array for either apples or oranges, but not both! Additionally, if the user supplies a value for apples, they are also required to submit a value for apples-property, they shouldn't be able to submit a value here if they chose oranges...

Is this sort of conditional logic something I should do with JSON schemas? Now that I'm writing this out, it sounds like this would be much easier if I simply had two separate models...

🌐
Hackolade
hackolade.com › help › Addachoiceconditionalorpatternfi.html
Add a choice, conditional, or pattern field
Multiple data types have been supported ... is when having to allow the application of a subschema depending on the outcome of another schema: If if is valid, then then must ......
🌐
json-everything
docs.json-everything.net › schema › schemagen › conditional-generation
Conditional JSON Schema Generation | json-everything
2 weeks ago - Draft 7 of JSON Schema introduced a nice way to include some conditional constraints into your schemas. The most common way that people use these is to apply different constraints to various properties based on the value of another property. This is similar to the discriminator keyword offered by Open API. The idea is that if myObj.X == "foo" then apply some constraints to myObj.Y, such as minLength or make it required.
🌐
GitHub
github.com › orgs › json-schema-org › discussions › 718
if - then - else in json schema · json-schema-org · Discussion #718
Notice that the if statement is evaluated as true (incorrectly), causing the then block to be validated when it should actually validate the else block. Where am I going wrong? Or is my theory completely off? Thanks in advance for your assistance! Beta Was this translation helpful? Give feedback. ... The problem you're having is that schemas can only look at what's inside them.
🌐
Oracle
docs.oracle.com › cd › E17952_01 › mysql-8.0-en › json-validation-functions.html
14.17.7 JSON Schema Validation Functions
October 25, 2025 - Both schema and document are required. The schema must be a valid JSON object; the document must be a valid JSON document. Provided that these conditions are met: If the document validates against the schema, the function returns true (1); otherwise, it returns false (0). In this example, we ...
🌐
Juha-Matti Santala
hamatti.org › posts › json-schema-require-properties-based-on-values-of-other-properties
JSON Schema: require properties based on values of other properties : Juha-Matti Santala
June 29, 2024 - To start with, here’s what our schema looks like (I’ve omitted metadata like id and schema): { "type": "object", "properties": { "ticketType": { "type": "integer" }, "seat": { "type": "integer" } }, "required": ["ticketType"] } Now we want to add constraint. that if ticketType == 2, seat is required.
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › if
if (2020-12) - Learn JSON Schema
// If `then` is missing bool valid = if_schema ? true : else_schema; // If `else` is missing bool valid = if_schema ? then_schema : true; A schema that constrains numeric instances to be positive when they are even numbers and negative otherwise Schema ... { "$schema": "https://json-schema...
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 846
"requiredProperties" keyword · Issue #846 · json-schema-org/json-schema-spec
September 29, 2019 - { "required": { "name": { "type": "string", "minLength":1 } } } Examples of confused users: https://stackoverflow.com/questions/61786293/json-schema-if-then-else-conditional-behaving-strangely · This issue is intended to restate and close #659 #681 #734 · 👍React with 👍12jamescrowley, dickson-tec, SqrtMinusOne, shabbyrobe, OnurGvnc and 7 more ·
Author   awwright
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 180
Validation: if/then/else · Issue #180 · json-schema-org/json-schema-spec
December 5, 2016 - This can be seen as the extension of #31 and #64. I see it as the syntax sugar for existing boolean/compound keywords. The validation process is very simple: if the schema in if is valid, than the schema in then should be validated and i...
Author   epoberezkin