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 therefore apply the then schema, which requires "bar".

Second, you want "propertyNames":false, which prevents having any keys in the schema, unlike if you were to set "else": false which would make anything always fail validation.

{
  "type": "object",
  "properties": {
    "foo": {
      "type": "string"
    },
    "bar": {
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "foo": {
        "enum": [
          "bar"
        ]
      }
    },
    "required": [
      "foo"
    ]
  },
  "then": {
    "required": [
      "bar"
    ]
  },
  "else": false
}
Answer from Relequestual on Stack Overflow
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › conditionals
JSON Schema - Conditional schema validation
If if is valid, then must also be valid (and else is ignored.) If if is invalid, else must also be valid (and then is ignored).
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
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
JSON schema form does not support "If Then Else"
Just as demo react-jsonschema-form playground shows I copied json form schema to JSON schema form component settings { "type": "object", "properties": { "animal": { "enum": [ "Cat", "Fish" ] } }, "allOf": [ { "if": { "properties": { "animal": { "const": "Cat" } } }, "then": { "properties": ... More on community.retool.com
🌐 community.retool.com
1
0
April 7, 2022
Is “if-then-else” supported in schema validation?
Hi, Is “if-then-else” introduced in Draft 07 supported by MongoDB? My first attempt failed. https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else Thank you, PJ More on mongodb.com
🌐 mongodb.com
0
1
March 9, 2023
🌐
GitHub
github.com › orgs › json-schema-org › discussions › 718
if - then - else in json schema · json-schema-org · Discussion #718
The problem you're having is that schemas can only look at what's inside them. They can't look up the schema structure. That means that the if-then that you have inside your neighborhoods subschema can't see that there's a country property.
🌐
JSON Schema
json-schema.org › learn › miscellaneous-examples
JSON Schema - Miscellaneous Examples
The then block is applied, which specifies that the membershipNumber property should be a string with a minimum length of 10 and a maximum length of 10. If the value of isMember is anything other than true: The else block is applied, which specifies that the membershipNumber property should ...
🌐
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...
🌐
Liquid Technologies
liquid-technologies.com › Reference › XmlStudio › JsonEditorNotation_IfThenElse.html
If Then Else
The schema described within the If clause is evaluated against the value in the JSON Instance Document. If the JSON Instance value passes the validation rules in the If, then the value MUST validate against rules described in the Then property · If the JSON Instance value fails to validate ...
🌐
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...

Find elsewhere
🌐
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...
🌐
Retool
community.retool.com › 💬 feature requests
JSON schema form does not support "If Then Else" - 💬 Feature Requests - Retool Forum
April 7, 2022 - Just as demo react-jsonschema-form ... "Fish" ] } }, "allOf": [ { "if": { "properties": { "animal": { "const": "Cat" } } }, "then": { "properties": { "food": { "type": "string", "enum": [ ......
🌐
MongoDB
mongodb.com › working with data
Is “if-then-else” supported in schema validation? - Working with Data - MongoDB Community Hub
March 9, 2023 - Hi, Is “if-then-else” introduced in Draft 07 supported by MongoDB? My first attempt failed. https://json-schema.org/understanding-json-schema/reference/conditionals.html#if-then-else Thank you, PJ
🌐
Json-schema
tour.json-schema.org › content › 05-Conditional-Validation › 06-Expanding-If-then-else
Expanding If-Then-Else: Conditional Validation | A Tour of JSON Schema
If grade is greater than or equal to 8, then the recommendationLetter field must be present, and the personalStatement field must NOT be present. Else if grade is lower than 8, then the personalStatement field must be present, and the recommendationLetter field must NOT be present..
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › else
else (2020-12) - Learn JSON Schema
The if, then, and else keywords can be thought of as imperative variants of the anyOf keyword, and both approaches are equally capable of describing arbitrary conditions. Choose the one that more elegantly describes your desired constraints. A schema that constrains numeric instances to be positive when they are odd numbers Schema ... { "$schema": "https://json-schema.org/draft/2020-12/schema", "if": { "multipleOf": 2 }, "else": { "minimum": 0 } }
🌐
JSON Schema
json-schema.org › learn › getting-started-step-by-step
Creating your first schema
After creating your JSON Schema, you can then validate example data against your schema by using a validator in a language of your choice. Please, visit Tools and select the validator that better suit your needs. If you already know how to create JSON Schemas and you are looking for different ...
🌐
Json-schema
tour.json-schema.org › content › 05-Conditional-Validation › 04-if-then-keyword
If-Then Keyword: Conditional Validation | A Tour of JSON Schema
Learn how to use the if-then keyword in JSON Schema to enforce conditional validation and implication, ensuring fields like salary and age are present based on conditions like isFullTime and isStudent.
🌐
JSON Schema
json-schema.org › draft › 2020-12 › json-schema-validation
JSON Schema Validation: A Vocabulary for Structural Validation of JSON
"allOf", "anyOf", "oneOf", "not", "if", "then", "else", "items", "additionalItems", "contains", "propertyNames", "properties", "patternProperties", "additionalProperties"
🌐
GitHub
github.com › orgs › json-schema-org › discussions › 529
If-Then for various forms of an Address · json-schema-org · Discussion #529
That means the schema only fails if the if passes and the then fails. So, assuming the JSON instance has "IsoCountryCode": "CA" and a valid Canadian address, the first schema will pass because if = pass and then = pass, the second and third ...
🌐
Learnjsonschema
learnjsonschema.com › draft7 › validation › else
else (Draft 7) - Learn JSON Schema
The if, then, and else keywords can be thought of as imperative variants of the anyOf keyword, and both approaches are equally capable of describing arbitrary conditions. Choose the one that more elegantly describes your desired constraints. A schema that constrains numeric instances to be positive when they are odd numbers Schema · Copy · { "$schema": "http://json-schema.org/draft-07/schema#", "if": { "multipleOf": 2 }, "else": { "minimum": 0 } } Valid An even number value that is positive is valid Instance ·