RulesEngine icon indicating copy to clipboard operation
RulesEngine copied to clipboard

Validating 'Type' property in json returns an error

Open reidono1 opened this issue 3 years ago • 2 comments

I have a scenario where I need to compare the 'Type' field in json data:

{ "Name": "test", "Type": "Basic" }

My rule is: (I'm using event as the RuleParameter name property)

event.Type == \"Basic\"

The following error is returned: Exception while parsing expression event.Type == "Basic" - Expression of type 'Newtonsoft.Json.Linq.JTokenType' expected

Works fine if I compare the Name field. Any suggestions?

Implementation

           var rules = new List<Rule>();
                rules.Add(
                    new Rule()
                    {
                        RuleName = Guid.NewGuid().ToString(),
                        RuleExpressionType = RuleExpressionType.LambdaExpression,
                        Expression = "event.Type == \"Basic\""
                    });
           

            var workflows = new List<Workflow>()
            {
                new Workflow()
                {
                    WorkflowName = WorkflowName,
                    Rules = rules
                }
            };

           var deserializedPayload = JsonConvert.DeserializeObject("{\r\n  \"Name\": \"test\",\r\n  \"Type\": \"Basic\"\r\n}");
           var input = new RuleParameter("event", deserializedPayload);

           var ruleEngine = new RulesEngine.RulesEngine(workflows.ToArray());

           await ruleEngine.ExecuteAllRulesAsync(WorkflowName, input);

reidono1 avatar Jul 13 '22 08:07 reidono1

It looks like something to do with 'Type' as some reserved keyword. I will check on it and get back to you. Meanwhile can you try doing event.Type.ToString() == "Basic" and see if that unblocks you?

abbasc52 avatar Jul 15 '22 04:07 abbasc52

Thanks, there is a workaround if you use event[\"Type\"] == \"Basic\".

reidono1 avatar Jul 15 '22 11:07 reidono1