Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
OleksiL opened this issue Feb 18, 2021 · 2 comments
Open

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

OleksiL opened this issue Feb 18, 2021 · 2 comments

Comments

@OleksiL
Copy link

OleksiL commented Feb 18, 2021

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)

@Jaygiri
Copy link

Jaygiri commented May 11, 2021

Try this...

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

@OleksiL
Copy link
Author

OleksiL commented Feb 17, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants