CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.6k stars 461 forks source link

Not able to resolve JsonPath when it includes a where condition #318

Open Meenakshise opened 1 year ago

Meenakshise commented 1 year ago

I have specified this as my condition

conditions: { all: [{ fact: 'displayMessage', operator: 'equal', value: "iphone", path:"$.childobj.phoneNumbers.[?(@.number=='0123-4567-8888')].type" }]

And this is the fact -

const facts = { displayMessage: {"childobj" : { "age" : 45 , "phoneNumbers": [ { "type": "iphone", "number": "0123-4567-8888" }, { "type": "home", "number": "0123-4567-8910" } ]} } }

It is not working.. Can you please tell me if this is supported or not? It is working if i specify this path - $.childobj.phoneNumbers.[0].type where no condition is specified. Whereas in the above path I have specified the condition.

Meenakshise commented 1 year ago

I think it's because Json-Path always returns the result in the form of an array when we have a where condition in our json-path like this $.childobj.phoneNumbers.[?(@.number=='0123-4567-8888')].type - the return type of this selection is an array. But when we don't have a where condition, the result it returns is in the form of a string. We can validate this in the javascript $.childobj.phoneNumbers.[0].type - the return type of this selection is a string

Hence while matching these values it results in false as the data types are completely different.

So what I did was while deriving the factvalue from the almanac.js file inside the factValue method I just added the below line of code

if(_typeof(pathValue)== 'string' ) return pathValue; else if(_typeof(pathValue) == 'object') return pathValue[0];

After this change it is working for me. Can I use this fix to support this or will it break anything else. Please confirm me.