Search code examples
jsonjsonschema

`If` `then` `else` not working in case of list in json schema


Json Schema and Json
I am trying to put a required check on certain variables based on what value is present in another attribute.
If paymentType= Shopping_cards then mandate paymentRef1 and
if it is not then mandate other attributes. But when paymentType != Shopping_cards then also I am getting error for paymentRef1.
Not sure what I am doing wrong here.
Can somebody please help me out? Both JSON schema and the request are present in the above link.


Solution

  • The if is inside properties > payment > properties > paymentMethods > items, so why not specify the condition relative to items. Change:

    "if": {
      "properties": {
        "paymentMethods": {
          "items": {
            "properties": {
              "paymentType": {
                "const": "SHOPPING_CARDS"
              }
            }
          }
        }
      }
    },
    

    to:

    "if": {
      "properties": {
        "paymentType": {
          "const": "SHOPPING_CARDS"
        }
      }
    },
    

    It works. I have no idea why the condition is true in the original version though.