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 } } }} 
Discussions

Json Schema validation using If-Else - "conditionally choosing required element" not working - General - JSON Forms community
Below is the JSON schema: Schema { "type": "object", "properties": { "Product": { "type": "object", "if": { "properties": { "Retention": { "type": "object" } } }, "then": { "properties": { "Retention": { "required": [ "AllowedRetention" ] } } }, "else": { "properti... More on jsonforms.discourse.group
🌐 jsonforms.discourse.group
0
February 10, 2023
Field required based on condition
It is very common to have data structures in which a field becomes required only if a particular condition holds. For example, having a field A being required only if field B has been specified is ... More on github.com
🌐 github.com
7
January 23, 2018
validation - How do I use the if-then-else condition in json schema? - Stack Overflow
The if keyword means that, if the result of the value schema passes validation, apply the then schema, otherwise apply the else schema. Your schema didn't work because you needed to require "foo" in your if schema, otherwise an empty JSON instance would pass validation of the if schema, and ... More on stackoverflow.com
🌐 stackoverflow.com
Need help implementing conditional requirements in JSON schema?
It sounds like you should have seperate endpoints of apples and oranges. Why are you trying to handle both through one endpoint? More on reddit.com
🌐 r/AskProgramming
9
4
January 29, 2023
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.
🌐
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 ...
Find elsewhere
🌐
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.
🌐
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
🌐
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}
🌐
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...

🌐
json-everything
docs.json-everything.net › schema › schemagen › conditional-generation
Conditional JSON Schema Generation | json-everything
3 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.
🌐
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.
🌐
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 ...
🌐
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.
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 846
"requiredProperties" keyword · Issue #846 · json-schema-org/json-schema-spec
September 24, 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
Top answer
1 of 3
21

There are several ways to achieve required effect even not using JSON Schema draft-07 if-then-else.

logical operator and implication (draft-04 and above)

A logical implication here: if "medium" present then "bulky" is required can be translated to "medium" not present OR "bulky" is "required" (the latter implicates "medium" is present) which can be further elaborated to "medium" not required OR "bulky" is "required" (since if "medium" is present, it will satisfy condition of being required). See below schema:

"properties": {
  "smaller": {"type": "number"},
  "larger": { "type": "number" },
  "medium":{"type":"string"},
  "bulky":{"type":"string"}
},
"required":["smaller","larger"],
"anyOf" : [ 
  { 
    "not" : { "required" : ["medium"] }
  },
  {
    "required" : ["bulky"]
  }
],
"additionalProperties" : false

Check here for reference:

JSON schema - valid if object does *not* contain a particular property

http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7

"anyOf" - logical OR, "oneOf" - XOR, "allOf" - AND, "not" - negation, yet pay attention to spec:

An instance is valid against this keyword if it fails to validate successfully against the schema defined by this keyword.

draft-06 - dependencies + propertyNames

Most obvious. I am not sure if you excluded this one in your question, so putting here just in case. Please note, that instead of "additionalProperties", if you wan't simply to limit valid keys, "propertyNames" could be used (and is actually what it was added for).

"properties": {
  "smaller": {"type": "number"},
  "larger": { "type": "number" },
  "medium":{"type":"string"},
  "bulky":{"type":"string"}
},
"required":["smaller","larger"],
"dependencies" : {
  "medium" : ["bulky"]
},
"propertyNames" : {
  "enum" : [
    "smaller",
    "larger",
    "medium",
    "bulky"
  ]
}

Check here for reference: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.7

Update

After clarification in comment:

for draft-6 - here "not require" means that if "medium" dont exist then bulky " must not be present"

"must not" means preventing bulky being present.

I will rephrase your condition:

1. if "medium" exists "bulky" must be present -> both keys must be present at the same time

2. if "medium" does not exist "bulky" must not be present as well -> both keys must not be present at the same time

Can "bulky" exist and "medium" does not exist?

No. See 2. And vice versa (see 1.). Boolean equality (complementary to logical XOR).

Thus if "bulky" exists - it means "medium" must be always there... It implies that both are required or both must not be required (or even allowed).

Since it's draft-06, you can use also "propertyNames" for defining allowed property names (kind of shortcut to this logic).

logical operator and implication (draft-06 and above)

The proper logical operation translated to JSOn Schema would look like:

"oneOf" : [
  { "required" : ["medium","bulky"] }, <== this schema is satisfied if both keys appear in validated instance
  {
    "allOf" : [   <== !medium ^ !bulky - due to how "not" works in schema context
      {"not" : { "required" : ["medium"] } },  
      {"not" : { "required" : ["bulky"] } },
    ]
  }
]

An XOR - EITHER (both required) OR (medium not required AND bulky not required).

Please note I am not doing "not" : { "required" : ["medium","bulky"] } as when just one of those keys is present, "required" schema would fail which would mean "not" would return successfull validation result. One needs to rephrase it using de Morgans laws:

"oneOf" : [
  { "required" : ["medium","bulky"] },
  {
    "not" : {   <=== !medium ^ !bulky = !(medium v bulky)
      "anyOf" : [
        { "required" : ["medium"] },
        { "required" : ["bulky"]  },
      ]
    }
  }
]

However using "propertyNames" will also do the trick. See following schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "smaller": {"type": "number"},
    "larger": { "type": "number" },
    "medium":{"type":"string"},
    "bulky":{"type":"string"}
  },
  "required":["smaller","larger"],
  "anyOf" : [ 
    { 
       "required" : ["medium","bulky"]
    },
    {
      "propertyNames" : {
        "enum" : [
          "smaller",
          "larger"
        ]
      },
    }
  ],
  "examples" : [
    {
      "smaller" : 1,
      "larger" : 2,


    },
    {
      "smaller" : 1,
      "larger" : 2,
      "bulky" : "test",
      "medium" : ""
    },
    {
      "smaller" : 1,
      "larger" : 2,

      "medium" : ""
    },
    {
      "smaller" : 1,
      "larger" : 2,
      "bulky" : "test",

    },
  ]
}

Does it answer your question?

2 of 3
5

JSON Schema Draft-07 has included these new keywords if, then and else which allow you to have conditional schemas.

In this example:

  • Only the foo property is required
  • However if foo is set to "bar" then the bar property also becomes required

var ajv = new Ajv({
  allErrors: true
});

var schema = {
  "properties": {
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    },

  },
  "required": ["foo"],
  "if": {
    "properties": {
      "foo": {
        "enum": ["bar"]
      }
    }
  },
  "then": {
    "required": ["bar"]
  }
}

var validate = ajv.compile(schema);

test({
  "foo": "bar",
  "bar": "baz"
}); // VALID

test({
  "foo": "xyz"
}); // VALID

test({
  "foo": "bar",
}); // NOT VALID


function test(data) {
  var valid = validate(data);
  if (valid) console.log('VALID', data);
  else console.log('NOT VALID', data);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.5.5/ajv.min.js"></script>

Hopefully this makes sense and you can adapt your code accordingly.

PS: in your schema you have the require property which I'm not sure is a valid JSON Schema keyword. You probably meant required instead.

🌐
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 › 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