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
🌐
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 ...
Discussions

if - then - else in json schema
Hello everyone, I hope you're all doing well! I have a question for the more experienced members here. I'm trying to achieve something with a JSON Schema draft-07, but I'm having troubl... More on github.com
🌐 github.com
1
1
Json Schema validation using If-Else - "conditionally choosing required element" not working - General - JSON Forms community
Hi All, I am new to validating JSON file via JSON schema. Below is the JSON schema: Schema { "type": "object", "properties": { "Product": { "type": "object", "if": { "properties": { "Retention": { "type": "object" } } }, "then": { "properties": { "Retention": { "required": [ "AllowedRetention" ... More on jsonforms.discourse.group
🌐 jsonforms.discourse.group
0
February 10, 2023
If-Then-Else Statements Where One or More Items in an Array Match
If I understand the semantics of the if-then-else validation currently supported, when evaluating objects within an array, all items in the array must match that condition for it to be evaluated as true. Consider this example: When prope... More on github.com
🌐 github.com
1
November 21, 2019
Generate schema from if-then-else condition - General - JSON Forms community
Hello, I am working on implementing complex json forms for registration page. The problem I have right now is that we need to handle conditional cases in both schema.json and uischema.json (field needs to be marked as required but also shown or hidden in uischema), and it is becoming increasingly ... More on jsonforms.discourse.group
🌐 jsonforms.discourse.group
0
August 21, 2023
🌐
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
🌐
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 ...
🌐
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.
🌐
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 - Hi All, I am new to validating JSON file via JSON schema. Below is the JSON schema: Schema { "type": "object", "properties": { "Product": { "type": "object", "if": { "properties": { "Retention": { "type": "object" } } }, "then": { "properties": { "Retention": { "required": [ "AllowedRetention" ] } } }, "else": { "properti...
🌐
GitHub
github.com › json-schema-org › json-schema-spec › issues › 827
If-Then-Else Statements Where One or More Items in an Array Match · Issue #827 · json-schema-org/json-schema-spec
November 21, 2019 - { "additionalProperties": false, "properties": { "property_abc": { "type": "array", "items": { "type": "object", "properties": { "value": { "enum": [ "Value1", "Value2", "Value3" ], "type": "string" } }, "required": [ "value" ] } }, "property_xyz": { "type": "number" } }, "allOf": [ { "if": { "properties": { "property_abc": { "items": { "properties": { "value": { "const": "Value1" } } } } } }, "then": { "required": [ "property_xyz" ] } } ], "type": "object" }
Author   chapmanjw
Find elsewhere
🌐
YouTube
youtube.com › watch
JSON Schema Validation - If.Then.Else - YouTube
In this video, I have demonstrated a code example about defining a json schema and validating json data against that schema to check if it is valid or not.►►...
Published   September 21, 2018
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360007665720-JSON-Schema-autocompletion-if-then-else-array-of-objects-properties
JSON Schema autocompletion if/then/else array of objects properties – IDEs Support (IntelliJ Platform) | JetBrains
March 4, 2020 - { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "strategies": { "type": "array", "items": { "allOf": [ { "if": { "properties": { "type": { "const": "line" } } }, "then": { "$ref": "strategy-line.json" } }, { "if": { "properties": { "type": { "const": "node" } } }, "then": { "$ref": "strategy-node.json" } } ] } } } }
🌐
Json-schema
tour.json-schema.org › content › 05-Conditional-Validation › 05-if-then-else
If-Then-Else: Conditional Validation | A Tour of JSON Schema
Learn how to use the if-then-else keyword in JSON Schema to apply conditional validation based on the value of a property.
🌐
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..
🌐
Eclipse Vert.x
vertx.io › docs › vertx-json-schema › java
JSON Schema | Eclipse Vert.x
Given the dynamic nature of json-schema and the conditional if-then-else it is not possible to validate in a streaming scenario. Validation is for this reason a blocking operation. If you are aware that validation will be a very expensive process, then it is advisable to run the validation ...
🌐
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).
🌐
tutorialpedia
tutorialpedia.org › blog › how-do-i-use-the-if-then-else-condition-in-json-schema
How to Use If-Then-Else in JSON Schema: Handling Missing or Empty Properties (Draft-07) — tutorialpedia.org
The schema will ignore then when if is false. Avoid Overly Complex if Conditions: Use allOf/anyOf sparingly. Break large conditions into reusable schemas with $ref for readability. Handle Non-Object Instances: If your schema allows non-object types (e.g., type: ["object", "string"]), ensure if conditions are compatible. For example, a string instance will fail an if that checks for object properties, triggering else. Test with Validators: Use tools like JSON Schema Validator to test edge cases (e.g., missing AND empty properties).
🌐
GitHub
github.com › orgs › json-schema-org › discussions › 503
Is multiple "if" "then" valid in JSON schema? · json-schema-org · Discussion #503
Example: 'The users were not included in any control group participation for the last 6 days and the UK non-roamers campaign.' here UK non-roamers is a campaign info NOT domain_field" }, "if":{ "properties":{ "drillDown":{ "enum":[ "true" ] } }, "required":[ "drillDown" ] }, "then":{ "required":[ "domainFields" ] }, "else":false, "domainFields":{ "type":"array", "description":"populate only if 'drillDown' option is true", "anyOf":[ { "number_domain_field_name":{ "type":"string", "description":"name of the domain field mentioned in user input if domain field type is of number" }, "condition":{
🌐
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 Forms community
jsonforms.discourse.group › general
Generate schema from if-then-else condition - General - JSON Forms community
August 21, 2023 - Hello, I am working on implementing complex json forms for registration page. The problem I have right now is that we need to handle conditional cases in both schema.json and uischema.json (field needs to be marked as required but also shown or hidden in uischema), and it is becoming increasingly ...
🌐
Learnjsonschema
learnjsonschema.com › 2020-12 › applicator › if
if (2020-12) - Learn JSON Schema
bool valid = if_schema ? then_schema : else_schema; JSON Schema is a constraint-driven language. Therefore, omitting either the then or the else keywords is equivalent to setting the corresponding part of the ternary conditional operation to the boolean true.
🌐
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...

🌐
Medium
medium.com › @nitishmaan567 › utilizing-if-else-to-validate-json-schema-14d1636d80b6
Utilizing if-else to validate JSON schema | Nitish Man Shrestha | Medium | Medium
August 7, 2024 - ... then: If the if condition is true, the data property must be an array of objects with specific properties (courseId, courseName, summary). else: If the if condition is false, the data property must be an array of objects with different ...