How to add rules to enum types

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 by balaji.s]

[Nanda]

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

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.

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

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

[balaji.s]

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

You can use this schema

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

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

[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)

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.

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

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

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

[Nanda]

Thanks @sdirix(sdirix)

[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.

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

{
  "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 and there is nothing special going on in regards of JSON Forms.