# How to give multiple rule effects on the element?

**URL:** <https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244>\
**Category:** General\
**Created:** [May 12, 2020, 10:49am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244 "2020-05-12T10:49:06Z")\
**Posts on this page:** 9\
**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:** [May 12, 2020, 10:49am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/1 "2020-05-12T10:49:06Z")

</div>

I want to give 2 different rule effects on the element. Let’s say in the following UI schema subCategory will be visible when the category length is more than 3 letters, once this condition is satisfied and subCategory is visible than I want to apply DISABLE effect on subCategory.

{  
“type”: “Control”,  
“scope”: “#/properties/subCategory”,  
“rule”: {  
“effect”: “SHOW”,  
“condition”: {  
“scope”: “#/properties/category”,  
“schema”: {  
“type”: “string”,  
“minLength”: 3  
}  
}  
}  
}

_[[original thread](https://spectrum.chat/jsonforms/general/how-to-give-multiple-rule-effects-on-the-element~3369d36e-9b11-4739-8b54-dc27e7667666) by Harshita Jaiswal]_

---

<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 12, 2020, 11:08am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/2 "2020-05-12T11:08:21Z")

</div>

Hi @jharshita, thanks for your question! Currently you can only apply a single rule to each element. As a workaround you could wrap the control into `VerticalLayout` and apply the second rule to this element. Visually this should look the same.

---

<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:** [May 12, 2020, 11:48am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/3 "2020-05-12T11:48:28Z")

</div>

_[shivgarg5676]_

e.g.

```auto
{
    "type": "Group",
    elements: [{
        type: "Control",
        scope: "#/properties/a",
        rule: {
        }
    }],
    rule:{
    }
}

```

---

<div class="post-metadata">

**Author:** ![nikola](https://avatars.discourse-cdn.com/v4/letter/n/a88e57/32.png) [@nikola](https://jsonforms.discourse.group/u/nikola)\
**Post date:** [June 12, 2023, 8:44am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/4 "2023-06-12T08:44:41Z")

</div>

Hi, I assume this would work for AND conditions, but is there any similar solution for OR conditions?

Like, I want to show field when “#/properties/subCategory” has certain value or “#/properties/category” has certain value

---

<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 12, 2023, 3:30pm UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/5 "2023-06-12T15:30:52Z")

</div>

Hi @nikola,

The [rule support can handle arbitrary schemas](https://jsonforms.io/docs/uischema/rules), so you could for example use a `oneOf` construct for an or condition.

---

<div class="post-metadata">

**Author:** ![nikola](https://avatars.discourse-cdn.com/v4/letter/n/a88e57/32.png) [@nikola](https://jsonforms.discourse.group/u/nikola)\
**Post date:** [June 13, 2023, 5:30am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/6 "2023-06-13T05:30:36Z")

</div>

Hi @sdirix thanks, I managed to get it working with

```auto
 "scope": "#",
 "schema": {
   "anyOf": [
     {
       "type": "object",
       "properties": {
         "hasPromoCode": {
           "const": "yes"
          }
        },
        "required": ["hasPromoCode"]
       },
       ...[second element condition]

```

But I’m having problems with nested structures, where 2 relevant elements don’t have same parent, like for example `#/properties/firstContainer/properties/hasPromoCode` and `#/properties/secondContainer/properties/hasSomethingElse`. I tried to get it working with

```auto
          "scope": "#",
          "schema": {
            "anyOf": [
              {
                "type": "object",
                "properties": {
                  "type": "object",
                  "firstContainer": {
                    "type": "object",
                    "properties": {
                      "hasPromoCode": {
                        "const": "yes"
                      }
                    },
                    "required": ["hasPromoCode"]
                  }
                }
              }
            ]

```

but I keep getting `Error: strict mode: unknown keyword: "0"` in the console

---

<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 13, 2023, 7:34am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/7 "2023-06-13T07:34:02Z")

</div>

Hi @nikola,

Glad that you managed to get it working!

> [@nikola](#):
>
> but I keep getting `Error: strict mode: unknown keyword: "0"` in the console

I’m not sure where this is coming from. We use the same AJV instance form-wide and there we [disable strict mode](https://github.com/eclipsesource/jsonforms/blob/a5fdca6fb6afa9820eabe8051401785675f53ad7/packages/core/src/util/validator.ts#L33). Do you hand in a custom AJV instance?

Note that your schema seems to be malformed as there is one `"type": "object"` too many. As `"object"` is not a valid schema for the property `type` maybe the error stems from that.

---

<div class="post-metadata">

**Author:** ![nikola](https://avatars.discourse-cdn.com/v4/letter/n/a88e57/32.png) [@nikola](https://jsonforms.discourse.group/u/nikola)\
**Post date:** [June 15, 2023, 10:46am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/8 "2023-06-15T10:46:56Z")

</div>

Hi @sdirix thanks! It was indeed my instance of ajv which was causing issues. It was set to

```auto
Ajv({ allErrors: true, strictSchema: true, strict: true })

```

By removing `strict: true` , everything works as it should and some other weird schema-structure related errors are gone.  
This is my final working solution:

```auto
      "rule": {
        "effect": "SHOW",
        "condition": {
          "scope": "#",
          "schema": {
            "anyOf": [
              {
                "properties": {
                  "firstContainer": {
                    "properties": {
                      "hasPromoCode": {
                        "const": "yes"
                      }
                    },
                    "required": [
                      "hasPromoCode"
                    ]
                  }
                }
              },
              {
                "properties": {
                  "secondContainer": {
                    "properties": {
                      "hasSomethingElse": {
                        "const": "yes"
                      }
                    },
                    "required": [
                      "hasSomethingElse"
                    ]
                  }
                }
              }
            ]
          }
        }
      }

```

---

<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 19, 2023, 7:43am UTC](https://jsonforms.discourse.group/t/how-to-give-multiple-rule-effects-on-the-element/244/9 "2023-06-19T07:43:08Z")

</div>

When you create a custom AJV instance you might want to use `createAjv` exported from `@jsonforms/core` as it already sets the AJV options as we need them in JSON Forms. The method also takes in a parameter to set additional options if you need them.
