# Default hide for “array contains value” when array undefined

**URL:** <https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152>\
**Category:** General\
**Created:** [May 15, 2024, 3:01pm UTC](https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152 "2024-05-15T15:01:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FedeTommi](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/fedetommi/32/1012_2.png) [@FedeTommi](https://jsonforms.discourse.group/u/FedeTommi)\
**Post date:** [May 15, 2024, 3:01pm UTC](https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152/1 "2024-05-15T15:01:10Z")

</div>

In reference to this issue: [No rule validation for "array contains value" when array undefined](https://jsonforms.discourse.group/t/no-rule-validation-for-array-contains-value-when-array-undefined/1308/1)

Is it possible to use the rule validation to display a control field only when a specific checkbox (from a string array) is selected and as long as no option is selected to hide it?

For example, with this schema:

```auto
{
      "type": "object",
      "properties": {
        "stringArray": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "type": "string",
            "enum": [
              "Option1",
              "Option2",
              "Other"
            ]
          }
        },
        "textOther": {
          "type": "string"
        }
      }
    },

```

And this ui-schema:

```auto
{
                      "type": "Control",
                      "scope": "#/properties/stringArray/properties/textOther",
                      "rule": {
                        "effect": "SHOW",
                        "condition": {
                          "scope": "#/properties/stringArray/properties/textOther",
                          "schema": {
                            "contains": {
                              "const": "Other"
                            }
                          }
                        }
                      }
                    }

```

the expected behaviour is that when the array is still undefined, the control for textOther is not displayed.

I thought one option would be to use a rule with hide and “does not contain”, though “Not” is not supported in rules.

How else is it possible to achieve the expected behaviour?

---

<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:** [May 16, 2024, 8:31am UTC](https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152/2 "2024-05-16T08:31:02Z")

</div>

Hi!

JSON Schema supports [“`not`”](https://json-schema.org/understanding-json-schema/reference/combining#not), therefore you can also use it in our schema-based rules.

One way of defining the behavior for `undefined` values is to scope against the (grand) parent object and then using `required` to define that the value must exist.

However [we added a shorthand](https://github.com/eclipsesource/jsonforms/commit/8fc90d3ee6221cd0e719557853a81f6609dd7d43) for this with the option `failWhenUndefined` which you can add as a sibling to `schema`. When this is set to `true` the condition will evaluate to `false` instead of `true` when the referenced value is `undefined`.

---

<div class="post-metadata">

**Author:** ![FedeTommi](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/fedetommi/32/1012_2.png) [@FedeTommi](https://jsonforms.discourse.group/u/FedeTommi)\
**Post date:** [May 16, 2024, 9:27am UTC](https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152/3 "2024-05-16T09:27:27Z")

</div>

Hi!

Thank you for the reply!

I tried adding “failWhenUndefined”: true as a sibling of schema in the rule, but there were no changes.

I also tried the option with “HIDE” and “not”:

```auto
"schema": {
     "not": {
          "contains": {
               "const": "Other"
           }
     }
}

```

also with no changes.

I am not sure I understand the second option you proposed

> One way of defining the behavior for `undefined` values is to scope against the (grand) parent object and then using `required` to define that the value must exist.

Is required needed inside of the rule itself?

---

<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 6, 2024, 9:45am UTC](https://jsonforms.discourse.group/t/default-hide-for-array-contains-value-when-array-undefined/2152/4 "2024-06-06T09:45:19Z")

</div>

I had a closer look at this issue again.

The error likely comes from your UI Schema being misconfigured.

I used the following `schema` and `uischema`:

```auto
{
  "type": "object",
  "properties": {
    "stringArray": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "enum": ["Option1", "Option2", "Other"]
      }
    },
    "textOther": {
      "type": "string"
    }
  }
}

```

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/stringArray"
    },
    {
      "type": "Control",
      "scope": "#/properties/textOther",
      "rule": {
        "effect": "SHOW",
        "condition": {
          "scope": "#/properties/stringArray",
          "failWhenUndefined": true,
          "schema": {
            "contains": {
              "const": "Other"
            }
          }
        }
      }
    }
  ]
}

```

With `failWhenUndefined: true`, the `textOther` will not show, even if the `stringArray` does not exist.

* * *

The alternative `required` solution without using `failWhenUndefined` looks like this:

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/stringArray"
    },
    {
      "type": "Control",
      "scope": "#/properties/textOther",
      "rule": {
        "effect": "SHOW",
        "condition": {
          "scope": "#",
          "schema": {
            "properties": {
              "stringArray": {
                "contains": {
                  "const": "Other"
                }
              }
            },
            "required": ["stringArray"]
          }
        }
      }
    }
  ]
}

```
