# How to conditionally set required on nested property of array type

**URL:** <https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236>\
**Category:** General\
**Created:** [January 17, 2023, 1:59pm UTC](https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236 "2023-01-17T13:59:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Icenwharth](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@Icenwharth](https://jsonforms.discourse.group/u/Icenwharth)\
**Post date:** [January 17, 2023, 1:59pm UTC](https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236/1 "2023-01-17T13:59:38Z")

</div>

I have this `schema` that is used by jsonforms.

```auto
const schema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    other_ubos: {
      type: 'boolean',
    },
    persons: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
      },
    },
  },
  required: ['other_ubos'],
  if: {
    properties: {
      other_ubos: {
        const: true,
      },
    },
  },
  then: {
    required: ['persons'],
  },
};

```

I want to set a condition where if `other_ubos` is `true` each array item should have `name``function` and `email` required.

so basically something like this

```auto
const schema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    other_ubos: {
      type: 'boolean',
    },
    persons: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
      },
    },
  },
  required: ['other_ubos'],
  if: {
    properties: {
      other_ubos: {
        const: true,
      },
    },
  },
  then: {
    required: ['persons[number].name'],
  },
};

```

setting required directly on

```auto
{
        type: 'object',
        properties: {
          name: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          function: {
            type: 'string',
            pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
          },
          email: {
            type: 'string',
            pattern: '^(.+)@(.+)$',
          },
        },
}

```

won’t have the desired effect since it will validate even if `other_ubos` is `false`

---

<div class="post-metadata">

**Author:** ![Icenwharth](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@Icenwharth](https://jsonforms.discourse.group/u/Icenwharth)\
**Post date:** [January 17, 2023, 2:39pm UTC](https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236/2 "2023-01-17T14:39:04Z")

</div>

What i ended up doing is conditionally setting the whole properties object:

```auto
const schema = {
  $schema: 'http://json-schema.org/draft-07/schema#',
  type: 'object',
  properties: {
    other_ubos: {
      type: 'boolean',
    },
  },
  if: {
    properties: {
      other_ubos: {
        const: true,
      },
    },
  },
  then: {
    properties: {
      other_ubos: {
        type: 'boolean',
      },
      persons: {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            name: {
              type: 'string',
              pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
            },
            function: {
              type: 'string',
              pattern: "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$",
            },
            email: {
              type: 'string',
              pattern: '^(.+)@(.+)$',
            },
          },
          required: ['name', 'function', 'email'],
        },
      },
    },
  },
  required: ['other_ubos'],
};

```

---

<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:** [January 17, 2023, 4:15pm UTC](https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236/3 "2023-01-17T16:15:12Z")

</div>

Hi @Icenwharth,

You can partially repeat yourself within JSON Schema. It doesn’t really matter as the end validation is just a union of everything specified.

So a JSON Schema for your use case might look like this:

```auto
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "other_ubos": {
      "type": "boolean"
    },
    "persons": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "pattern": "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$"
          },
          "function": {
            "type": "string",
            "pattern": "^[-'a-zA-ZÀ-ÖØ-öø-ÿ]+$"
          },
          "email": {
            "type": "string",
            "pattern": "^(.+)@(.+)$"
          }
        }
      }
    }
  },
  "required": [
    "other_ubos"
  ],
  "if": {
    "properties": {
      "other_ubos": {
        "const": true
      }
    }
  },
  "then": {
    "required": [
      "persons"
    ],
    "properties": {
      "persons": {
        "items": {
          "required": [
            "name",
            "function",
            "email"
          ]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Icenwharth](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@Icenwharth](https://jsonforms.discourse.group/u/Icenwharth)\
**Post date:** [January 18, 2023, 7:21am UTC](https://jsonforms.discourse.group/t/how-to-conditionally-set-required-on-nested-property-of-array-type/1236/4 "2023-01-18T07:21:46Z")

</div>

Thank you! This is much better than mine solution!
