Default hide for “array contains value” when array undefined

In reference to this issue: No rule validation for "array contains value" when array undefined

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:

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

And this ui-schema:

{
                      "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?

Hi!

JSON Schema supports 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 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.

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”:

"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?

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:

{
  "type": "object",
  "properties": {
    "stringArray": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "enum": ["Option1", "Option2", "Other"]
      }
    },
    "textOther": {
      "type": "string"
    }
  }
}
{
  "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:

{
  "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"]
          }
        }
      }
    }
  ]
}