# How to add conditional required validation on nested object controls?

**URL:** <https://jsonforms.discourse.group/t/how-to-add-conditional-required-validation-on-nested-object-controls/994>\
**Category:** General\
**Created:** [September 2, 2022, 8:41am UTC](https://jsonforms.discourse.group/t/how-to-add-conditional-required-validation-on-nested-object-controls/994 "2022-09-02T08:41:42Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [September 2, 2022, 11:05am UTC](https://jsonforms.discourse.group/t/how-to-add-conditional-required-validation-on-nested-object-controls/994/2 "2022-09-02T11:05:27Z")

</div>

Hi!

This can be expressed using JSON Schema’s `if/then/else` support, e.g.

```json
{
  "type": "object",
  "required": [
    "name",
    "married"
  ],
  "if": {
    "properties": {
      "married": {
        "const": "Married"
      }
    }
  },
  "then": {
    "properties": {
      "child": {
        "required": [
          "child_name",
          "child_married"
        ]
      }
    }
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "married": {
      "type": "string",
      "enum": [
        "Married",
        "Single"
      ]
    },
    "child": {
      "type": "object",
      "if": {
        "properties": {
          "child_married": {
            "const": "Married"
          }
        }
      },
      "then": {
        "properties": {
          "grandChild": {
            "required": [
              "grand_child_name",
              "grand_child_married"
            ]
          }
        }
      },
      "properties": {
        "child_name": {
          "type": "string"
        },
        "child_married": {
          "type": "string",
          "enum": [
            "Married",
            "Single"
          ]
        },
        "child_address": {
          "type": "string"
        },
        "grandChild": {
          "type": "object",
          "properties": {
            "grand_child_name": {
              "type": "string"
            },
            "grand_child_married": {
              "type": "string",
              "enum": [
                "Married",
                "Single"
              ]
            },
            "grand_child_city": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

```

Note that the `child` and `grandChild` objects need to exist as otherwise there will be no errors reported against them, e.g.

```json
{
  "name": "Foo",
  "married": "Married"
}

```

is valid while

```json
{
  "name": "Foo",
  "married": "Married",
  "child": { }
}

```

is not.

Of course you could also add them to the `then` blocks, e.g.

```json
{
  "then": {
    "required": ["child"],
    "properties": {
      "child": {
        "required": [
          "child_name",
          "child_married"
        ]
      }
    }
  }
}

```

However these errors will not show up in the JSON Forms UI by default. To show the errors on the actual field the objects need to exist as described above.

---

_[View the full topic](https://jsonforms.discourse.group/t/how-to-add-conditional-required-validation-on-nested-object-controls/994)._
