Multiple conditions for making fields required by condition

Hi! I will be very grateful for your help
I’ve read topic.
I realized that I can create only one if/then/else condition to set required fields by condition, but I need several unrelated ones.

This is my example scheme.json

{
  "type": "object",
  "properties": {
      "realEstateMarket": {
        "title": "Real Estate Market",
        "type": "string",
        "enum": ["primary", "secondary"],
        "default": "primary"
      },
      "primaryMarket": {
        "type": "object",
        "properties": {
          "housingComplexClass": {
            "type": "string",
            "enum": ["premium", "econom", "comfort", "business"]
          },
          "comissioning": {
            "type": "string",
            "format": "year",
            "stringLength": 4
          }
        }
      },
      "realEstateCategory": {
        "type": "string",
        "title": "Категорія нерухомості",
        "enum": ["housing_stock", "commerce", "land", "garages"]
      },
     "plotOfLand": {
        "type": "string",
        "title": "Ділянка, соток",
        "customRender": "numeric",
        "deximal": true
      },
   },
  "if": {
      "properties": {
        "realEstateMarket": {
          "const": "primary"
        }
      }
    },
    "then": {
      "properties": {
        "primaryMarket": {
          "required": ["housingComplexClass", "comissioning"]
        }
      }
    },
    "else": {
      "required": ["yearOfConstruction"]
    }
}

I need add more such conditions, for example:
if property “realEstateCategory” === “land”, then property “plotOfLand” must be required. It’s very important for our project.
I changed the condition if/then/else/ in my schema to allOf, to set several if/then/else conditions

"allOf": [
      {
        "if": {
          "properties": {
            "realEstateMarket": {
              "const": "primary"
            }
          }
        },
        "then": {
          "properties": {
            "primaryMarket": {
              "required": ["housingComplexClass", "comissioning"]
            }
          }
        },
        "else": {
          "required": ["yearOfConstruction"]
        }
      },
      {
        "if": {
          "properties": {
            "realEstateCategory": {
              "const": "land"
            }
          }
        },
        "then": {
          "required": ["plotOfLand"]
        }
      }
    ]

And it works, how I expect !!!
BUT typescript is very angry for me ))) I have no idea how to get around this problem

Hi @Natatashkin,

For a quick fix you can simply type or cast your schema to any, e.g. schema as any.

Thanks for the quick response!!!
We found another way for a quick fix, because our team lead is against setting the schema type any, so it looks like that:


    "allOf": [
      {
        "if": {
          "properties": {
            "realEstateMarket": {
              "const": "primary"
            },
            "realEstateCategory": {},
            "realEstateType": {}
          }
        },
        "then": {
          "properties": {
            "primaryMarket": {
              "required": ["housingComplexClass", "comissioning"]
            }
          }
        },
        "else": {
          "required": ["yearOfConstruction"]
        }
      },
      {
        "if": {
          "properties": {
            "realEstateCategory": {
              "const": "land"
            },
            "realEstateMarket": {},
          }
        },
        "then": {
          "required": ["plotOfLand", "cadastralNumber"]
        }
      },

We use “realEstateMarket”: {}, for allOf scheme does not expect strict execution of all declared conditions in each case. Works great, but if there are a lot of if/else conditions, we need to be very careful to miss anything

Instead of casting to any you can also cast to JsonSchema or add a type guard for JsonSchema, if that’s required by your project.