# anyOf not working as wrapper for if/then

**URL:** <https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072>\
**Category:** General\
**Created:** [April 8, 2024, 7:49pm UTC](https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072 "2024-04-08T19:49:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![roblesterjr](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/roblesterjr/32/999_2.png) [@roblesterjr](https://jsonforms.discourse.group/u/roblesterjr)\
**Post date:** [April 8, 2024, 7:49pm UTC](https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072/1 "2024-04-08T19:49:28Z")

</div>

I’m trying to do a multi-conditional required array. It works when I use `allOf` but of course that means _all_ of my conditions need to be met. Rather, I need certain conditions to be met for certain fields. I do not want to make a custom renderer because this ought to be table-stakes.

```json
"anyOf": [
                    {
                        "if": {
                            "properties": {
                                "veteran": {
                                    "const": true
                                }
                            }
                        },
                        "then": {
                            "required": [
                                "veteran_unemployed"
                            ]
                        }
                    },
                    {
                        "if": {
                            "properties": {
                                "snap": {
                                    "const": true
                                }
                            }
                        },
                        "then": {
                            "required": [
                                "snap_benefits_extras_name",
                                "snap_benefits_before",
                                "snap_benefits_extras_city_state"
                            ]
                        }
                    }
                ]

```

---

<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:** [April 9, 2024, 1:51pm UTC](https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072/2 "2024-04-09T13:51:07Z")

</div>

Hi @roblesterjr,

Can you explain what the issue is exactly? An `allOf` over `if` entries does not require all of the `if` to be `true`.

---

<div class="post-metadata">

**Author:** ![roblesterjr](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/roblesterjr/32/999_2.png) [@roblesterjr](https://jsonforms.discourse.group/u/roblesterjr)\
**Post date:** [April 10, 2024, 1:25pm UTC](https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072/3 "2024-04-10T13:25:23Z")

</div>

In my testing, it did. Fields specified in the `then` result were not required until both `if` conditions evaluated to true.

---

<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:** [April 10, 2024, 3:48pm UTC](https://jsonforms.discourse.group/t/anyof-not-working-as-wrapper-for-if-then/2072/4 "2024-04-10T15:48:04Z")

</div>

`allOf` is what you want to apply all restrictions. It does not mean that all of the `if`s need to be `true` for the `then` to apply.

You can easily test this with online JSON Schema validators like [this one](https://www.jsonschemavalidator.net/).

Here you can see it applied in JSON Forms

Using this schema

```json
{
  "type": "object",
  "properties": {
    "veteran": {
      "type": "boolean"
    },
    "veteran_unemployed": {
      "type": "boolean"
    },
    "snap": {
      "type": "boolean"
    },
    "snap_benefits_extras_name": {
      "type": "string"
    },
    "snap_benefits_before": {
      "type": "boolean"
    },
    "snap_benefits_extras_city_state": {
      "type": "string"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "veteran": {
            "const": true
          }
        },
        "required": ["veteran"]
      },
      "then": {
        "required": ["veteran_unemployed"]
      }
    },
    {
      "if": {
        "properties": {
          "snap": {
            "const": true
          }
        },
        "required": ["snap"]
      },
      "then": {
        "required": [
          "snap_benefits_extras_name",
          "snap_benefits_before",
          "snap_benefits_extras_city_state"
        ]
      }
    }
  ]
}

```

and this UI Schema

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/veteran"
    },
    {
      "type": "Control",
      "scope": "#/properties/veteran_unemployed"
    },
    {
      "type": "Control",
      "scope": "#/properties/snap"
    },
    {
      "type": "Control",
      "scope": "#/properties/snap_benefits_extras_name"
    },
    {
      "type": "Control",
      "scope": "#/properties/snap_benefits_before"
    },
    {
      "type": "Control",
      "scope": "#/properties/snap_benefits_extras_city_state"
    }
  ]
}

```

then this is the result

![AllOfRequired](https://global.discourse-cdn.com/free1/uploads/jsonforms/original/1X/636d3d477c293c0c4cd231b5287b2fa00b8f9367.gif)
