json-rules-engine-simplified icon indicating copy to clipboard operation
json-rules-engine-simplified copied to clipboard

Bug: "includes" predicate doesn't work and causes exception

Open OleksiL opened this issue 5 years ago • 2 comments

I use this rule:

[{
	"conditions": {
		"not": {
			"and": [{
				"field_1": {
					"includes": ["opt1", "opt2"]
				}
			}]
		}
	},
	"event": {
		"type": "remove",
		"params": {
			"field": "field_2"
		}
	}
}]

The engine fails with the following error:

Uncaught ReferenceError: Rule contains invalid predicates 0,1 at toError (vendors.chunk.js:281881) at validatePredicates (vendors.chunk.js:282058) at Engine.validate (vendors.chunk.js:281566) at Engine.addRule (vendors.chunk.js:281580) at vendors.chunk.js:348680 at Array.forEach () at rulesRunner (vendors.chunk.js:348679) at applyRules (vendors.chunk.js:348486) at new FormRenderer (:9001/app/content-management~space.chunk.js:1665) at new FormRenderer (eval at ES6ProxyComponentFactory (vendors.chunk.js:1), :5:7)

OleksiL avatar Feb 18 '21 08:02 OleksiL

Try this...

[{
	"conditions": {
		"not": {
			"and": [
                             {
				"field_1": {
					"includes": "opt1"
				}
			    },
                             {
				"field_1": {
					"includes": "opt2"
				}
			    }
                         ]
		}
	},
	"event": {
		"type": "remove",
		"params": {
			"field": "field_2"
		}
	}
}]

Jaygiri avatar May 11 '21 04:05 Jaygiri

It still fails. With a different error though.

import React from 'react';
import Engine from 'json-rules-engine-simplified';
import Form from '@rjsf/core';
import applyRules from 'rjsf-conditionals';

const FormRenderer = () => {

  const jsonSchema = {
    "type": "object",
    "required": [
      "1c712355-2d88-4ece-a7fb-3d89b7865936"
    ],
    "properties": {
      "1c712355-2d88-4ece-a7fb-3d89b7865936": {
        "title": "text field",
        "hidden": false,
        "type": "string"
      },
      "2a6c64e9-d0a6-4ee9-8eff-ba371d9afe12": {
        "title": "multi select field",
        "type": "array",
        "default": [
          "opt1",
          "opt2"
        ],
        "hidden": false,
        "items": {
          "type": "string",
          "enum": [
            "opt1",
            "opt2"
          ],
          "enumNames": [
            "opt1",
            "opt2"
          ]
        },
        "minItems": 1,
        "uniqueItems": true
      }
    }
  };

  const jsonLayout = {
    "ui:order": [
      "1c712355-2d88-4ece-a7fb-3d89b7865936",
      "2a6c64e9-d0a6-4ee9-8eff-ba371d9afe12"
    ],
    "1c712355-2d88-4ece-a7fb-3d89b7865936": {
      "ui:disabled": false,
      "ui:options": {},
      "ui:widget": "text"
    },
    "2a6c64e9-d0a6-4ee9-8eff-ba371d9afe12": {
      "ui:widget": "checkboxes",
      "ui:disabled": false,
      "ui:options": {}
    }
  };

  const rules = [
    {
      "conditions": {
        "not": {
          "and": [
            {
              "or": [
                {
                  "2a6c64e9-d0a6-4ee9-8eff-ba371d9afe12": {
                    "includes": "opt1"
                  }
                },
                {
                  "2a6c64e9-d0a6-4ee9-8eff-ba371d9afe12": {
                    "includes": "opt2"
                  }
                }
              ]
            }
          ]
        }
      },
      "event": {
        "type": "remove",
        "params": {
          "field": "1c712355-2d88-4ece-a7fb-3d89b7865936"
        }
      }
    }
  ];

  const FormWithCondition = applyRules(
    jsonSchema,
    jsonLayout,
    rules,
    Engine
  )(Form);

  return <FormWithCondition/>;
}

export default FormRenderer;

When the component is rendered includes check from predicates library is executed few times (why?). On the first execution the values are correct and the check passes: image

But on the immediate subsequent execution the field value is undefined and exception is thrown: image In this way in the debug mode you can see that the form is rendered after the first check and immediately replaced with the exception on the second check.

OleksiL avatar Feb 17 '23 09:02 OleksiL