# Multiple conditions for making fields required by condition

**URL:** <https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541>\
**Category:** General\
**Created:** [June 19, 2023, 12:50pm UTC](https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541 "2023-06-19T12:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Natatashkin](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/natatashkin/32/735_2.png) [@Natatashkin](https://jsonforms.discourse.group/u/Natatashkin)\
**Post date:** [June 19, 2023, 12:50pm UTC](https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541/1 "2023-06-19T12:50:55Z")

</div>

Hi! I will be very grateful for your help  
I’ve read [topic](https://jsonforms.discourse.group/t/how-to-add-conditional-required-validation-on-nested-object-controls/994/2).  
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

```javascript
{
  "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

```javascript
"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

 ![Screenshot from 2023-06-19 15-42-00](https://global.discourse-cdn.com/free1/uploads/jsonforms/original/1X/60f6f0405ade3e3f9ccbdca47bb8510b0d157839.png)

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [June 19, 2023, 1:24pm UTC](https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541/2 "2023-06-19T13:24:39Z")

</div>

Hi @Natatashkin,

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

---

<div class="post-metadata">

**Author:** ![Natatashkin](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/natatashkin/32/735_2.png) [@Natatashkin](https://jsonforms.discourse.group/u/Natatashkin)\
**Post date:** [June 19, 2023, 5:11pm UTC](https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541/3 "2023-06-19T17:11:27Z")

</div>

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:

```javascript

    "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

---

<div class="post-metadata">

**Author:** ![sdirix](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/sdirix/32/1073_2.png) [@sdirix](https://jsonforms.discourse.group/u/sdirix)\
**Post date:** [June 20, 2023, 8:09am UTC](https://jsonforms.discourse.group/t/multiple-conditions-for-making-fields-required-by-condition/1541/4 "2023-06-20T08:09:57Z")

</div>

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.
