Add Multiple conditions in a single rule

Hello Team,

I’m facing some issue while adding multiple rule conditions to a field.
Lets take an example.

const schema = {
type: “object”,
properties: {
name: {
type: “string”,
},
age: {
type: “string”,
},
occupation: {
type: “string”,
},
location: {
type: “string”,
},
workMode: {
type: “string”,
},
},
};

const uiSchema = {
type: “VerticalLayout”,
elements: [
{
type: “Control”,
scope: “#/properties/name”,
},
{
type: “Control”,
scope: “#/properties/occupation”,
},
{
type: “Control”,
scope: “#/properties/location”,
},
{
type: “Control”,
scope: “#/properties/workMode”,
},
{
type: “Control”,
scope: “#/properties/age”,
rule: {
effect: “DISABLE”,
condition: {
type: “AND”,
conditions: [
{
type: “LEAF”,
scope: “#/properties/name”,
expectedValue: “John”,
},
{
type: “LEAF”,
scope: “#/properties/location”,
expectedValue: “Australia”,
},
],
},
},
},
],
};
Based on this uischema, whenever I type “John” in name field and “Australia” in location field, Age field is getting disabled.

lets say, I want to disable occupation field, if workMode is not equal to “wfh” and location is not equal to “test”. I have tried with below uiSchema. what it does is occupation field is always disabled, even if workMode is “wfh” and location is “test”,
I expected occupation field is supposed to be enabled if workMode is “wfh” and location is “test”.

how can we achieve this by changing rule of occupation field in uiSchema?

const uiSchema = {
type: “VerticalLayout”,
elements: [
{
type: “Control”,
scope: “#/properties/name”,
},
{
type: “Control”,
scope: “#/properties/occupation”,
rule: {
effect: “DISABLE”,
condition: {
type: “AND”,
conditions: [
{
type: “NOT”,
condition: {
type: “EQUALS”,
scope: “#/properties/workMode”,
expectedValue: “wfh”,
},
},
{
type: “NOT”,
condition: {
type: “EQUALS”,
scope: “#/properties/location”,
expectedValue: “test”,
},
},
],
},
},
},
{
type: “Control”,
scope: “#/properties/location”,
},
{
type: “Control”,
scope: “#/properties/workMode”,
},
{
type: “Control”,
scope: “#/properties/age”,
},
],
};

Thank you in Advance
Suseen

Hi @Suseendhiran,

Our old conditions are very simplistic and only support AND, OR, and LEAF as their type. There is no NOT type, so that condition will just default to always true.

For more complex use cases you can use our schema based rules, as illustrated in the documentation. There you have the full power of JSON Schema validation available for your use cases. The second example of yours can be written as

"rule": {
  "effect": "DISABLE",
  "condition": {
    "scope": "#",
    "schema": {
      "properties": {
        "workMode": {
          "not": { "const": "wfh" }
        },
        "location": {
          "not": { "const": "test" }
        }
      }
    }
  }
}

Depending on whether you want to apply the rule when workMode and/or location are empty, you need to add "required": ["workMode", "location"] next to "properties".

Thanks for your quick response @sdirix , I checked the solution provided by you, but I sense it works like an “OR”. So I tried some other solution provided by you in another post(How to apply composable condition in uischema? - #6 by sdirix) . I have made changes based on this using “anyOf”, Its working as expected. Attaching rule for reference.

  rule: {
    effect: "DISABLE",
    condition: {
      scope: "#",
      schema: {
        anyOf: [
          {
            type: "object",
            properties: {
              workMode: {
                not: { const: "wfh" },
              },
            },
            //required: ["name"],
          },
          {
            type: "object",
            properties: {
              location: {
                not: { const: "test" },
              },
            },
            // required: ["location"],
          },
        ],
      },
    },
  },

Kind regards,
Suseendhiran D