# How to add rules to enum types

**URL:** <https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22>\
**Category:** General\
**Created:** [October 29, 2020, 11:32am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22 "2020-10-29T11:32:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [October 29, 2020, 11:32am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/1 "2020-10-29T11:32:58Z")

</div>

e.g i have a enum in the json schema with values [“a,”, “b”,“c”, “d”] and i have textbox defined in schema. When i write uischema, , the enum value becomes html multiselect and i want the textbox to be disabled if “d” value is chosen in the multiselect. How do i apply this in “rules”? any example link to the documentation would be helpful, thanks, have a great day

_[[original thread](https://spectrum.chat/jsonforms/general/how-to-add-rules-to-enum-types~c518b7e8-be35-4d3b-8375-ba23d7359942) by balaji.s]_

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [October 29, 2020, 11:49am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/2 "2020-10-29T11:49:29Z")

</div>

_[Nanda]_

“rule”: {  
“effect”: “DISABLE”,  
“condition”: {  
“scope”: “#/properties/itemSubmissionType”,  
“schema”: {  
“enum”: [  
“d”  
]  
}  
}  
}

---

<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:** [October 29, 2020, 12:02pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/3 "2020-10-29T12:02:49Z")

</div>

Hi @balaji-s(balaji-s), the rule looks fine so this should already work. All you have to do is to add this rule to the text control in the ui schema.

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [October 29, 2020, 12:18pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/4 "2020-10-29T12:18:00Z")

</div>

_[balaji.s]_

If i have schema like this below, how should i write the rules, for example i want to have textbox disabled, when the user selects Apple or mango “fruits”: {  
“type”: “array”,  
“items”: {  
“type”: “object”,  
“properties”: {  
“fruit”: {  
“type”: “string”,  
“enum”: [  
“NONE”,  
“APPLE”,  
“ORANGE”,  
“mango”,  
“guava”

```auto
        ]
      }
    }
  }
} i tried from the above rule, and it doesn't work as expected

```

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [October 29, 2020, 12:21pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/5 "2020-10-29T12:21:40Z")

</div>

_[balaji.s]_

“rule”: {  
“effect”: “DISABLE”,  
“condition”: {  
“scope”: “#/properties/fruits”,  
“schema”: {  
“enum”: [  
“mango”,"apple  
]  
}  
}  
the above rule doesn’t work

---

<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:** [October 29, 2020, 12:55pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/6 "2020-10-29T12:55:06Z")

</div>

You can use this schema

```auto
{
  "type": "object",
  "properties": {
    "fruits": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "none",
              "apple",
              "orange",
              "mango",
              "guava"
            ]
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

```

combined with this ui schema

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/fruits",
      "options": {
        "detail": {
          "type": "HorizontalLayout",
          "elements": [
            {
              "type": "Control",
              "scope": "#/properties/type"
            },
            {
              "type": "Control",
              "scope": "#/properties/name",
              "rule": {
                "effect": "DISABLE",
                "condition": {
                  "scope": "#/properties/type",
                  "schema": {
                    "enum": [
                      "mango",
                      "apple"
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [October 29, 2020, 2:30pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/7 "2020-10-29T14:30:38Z")

</div>

_[Nanda]_

Thanks, @sdirix(sdirix). I have a scenario like the “name” property is a standalone property and not a part of array objects. What is the “scope” to disable the control in the rule? (like #/properties/fruits/items/properties/type)

---

<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:** [October 29, 2020, 2:50pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/8 "2020-10-29T14:50:19Z")

</div>

Depends on what you want to validate against. For example you could validate against the array in general with the scope `#/properties/fruits` and specify a rule-schema with properties for the array, e.g. containing at least 3 items and one of them being `mango`.

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [November 2, 2020, 7:02am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/9 "2020-11-02T07:02:32Z")

</div>

_[Nanda]_

Hi @sdirix(sdirix), This is my schema `{ “type”: “object”, “properties”: { “fruits”: { “type”: “array”, “items”: { “type”: “object”, “properties”: { “type”: { “type”: “string”, “enum”: [“none”, “apple”, “orange”, “mango”, “guava”] } } } }, “name”: { “type”: “string” } } }` How can I disable the “name” property in the UI schema, based on the “type” property inside the array fruits?

---

<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:** [November 5, 2020, 8:53am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/10 "2020-11-05T08:53:05Z")

</div>

For example you could use the following ui schema to disable the `name` input whenever there are only mango elements in the array:

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/name",
      "rule": {
        "effect": "DISABLE",
        "condition": {
          "scope": "#/properties/fruits",
          "schema": {
            "type": "array",
            "items": {
              "properties": {
                "type": {
                  "const": "mango"
                }
              },
              "required": ['type']
            }
          }
        }
      }
    },
    {
      "type": "Control",
      "scope": "#/properties/fruits"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [November 5, 2020, 9:12am UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/11 "2020-11-05T09:12:05Z")

</div>

_[Nanda]_

Thanks @sdirix(sdirix)

---

<div class="post-metadata">

**Author:** ![discobot](https://yyz2.discourse-cdn.com/free1/user_avatar/jsonforms.discourse.group/discobot/32/3_2.png) [@discobot](https://jsonforms.discourse.group/u/discobot)\
**Post date:** [November 9, 2020, 2:15pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/12 "2020-11-09T14:15:45Z")

</div>

_[Nanda]_

Hi @sdirix, How can I disable the “name” control, if the array **contains** “mango” type. The enum is not working fo for me. The enum is expecting all the matching values in the array. Also, the rule is not supporting multiple conditions. I am using “oneOf” to implement multiple conditions. But it’s not working for me.

---

<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:** [November 9, 2020, 4:18pm UTC](https://jsonforms.discourse.group/t/how-to-add-rules-to-enum-types/22/13 "2020-11-09T16:18:05Z")

</div>

With this ui schema the “name” control will be disabled when there is at least one “mango” in the array

```auto
{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/name",
      "rule": {
        "effect": "DISABLE",
        "condition": {
          "scope": "#/properties/fruits",
          "schema": {
            "type": "array",
            "contains": {
              "properties": {
                "type": {
                  "const": "mango"
                }
              },
              "required": [
                "type"
              ]
            }
          }
        }
      }
    },
    {
      "type": "Control",
      "scope": "#/properties/fruits"
    }
  ]
}

```

Note that this is just regular [JSON Schema validation](https://tools.ietf.org/html/draft-handrews-json-schema-validation-02) and there is nothing special going on in regards of JSON Forms.
