skip to Main Content

Following is the jsonSchema.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "isMerchant": {
          "type": "boolean"
        },
        "isAgent": {
          "type": "boolean"
        },
        "cashLoan": {
          "type": "boolean"
        },
        "personalDetail": {
          "type": "string"
        },
        "contact": {
          "type": "object",
          "properties": {
            "email": {
              "type": "string"
            },
            "mobile": {
              "type": "integer"
            },
            "area": {
              "type": "string"
            },
            "state": {
              "type": "string"
            }
          },
          "required": ["state"]}
      },
      "required": ["isMerchant","cashLoan","contact"],
      "allOf": [
        {
          "if": {
            "properties": {
              "isMerchant": {"enum": [true]}
            }
          },
          "then": {
            "required": [ "isAgent","email","mobile"]
          }
        },
        {
          "if": {
            "properties": {
              "cashLoan": {"enum": [true]}
            }
          },
          "then": {
            "required": ["personalDetail"]
          }
        }
      ]
    }
  ]
}

Expectation is to validate the corresponding JSON data with this JSON schema. where conditions are as follows –

  1. If ‘isMerchant’ = true then the params ‘isAgent’, ’email’, ‘mobile’ should be present in the json file.
  2. If ‘cashLoan’ = true then param ‘personalDetail’ should be present.

2

Answers


  1. With draft 2020-12, the type of the items keyword can only be object or boolean.

    enter image description here

    Login or Signup to reply.
  2. You need to add a "requires": [...] to your if conditions, because the properties keyword will evaluate to true if the property is not present at all, therefore you are entering the then conditionals when you don’t intend to.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search