How to check if rule value is substring of data value? (json-logic-js)

32 views Asked by At

I want to accomplish a simple operation: checking if the rule value is a substring of the data value.

Let's say the rule value is 'check' and the data value is 'checking'. It must return true since the rule value is a substring of the data value.

Here is my current rule:

{
  "and": [
    {
      "in": [
        {
          "var": "ACTION1"
        },
        "check"
      ]
    }
  ]
}

And here is my data:

{
  "ACTION1": "check the dictionary"
}

Currently, this setup won't return true unless we place the rule value and data value in the opposite order. However, I don't want to do this since the data value is dynamic.

Can you please help me?


I can't find json-logic-js tag. sorry for tagging jsonlogic

2

There are 2 answers

0
tempra On

i fixed by transferring the value at the top of var object.

rule data

{
  "and": [
    {
      "in": [
        "check", // transferred it above
        {
          "var": "ACTION1"
        }
      ]
    }
  ]
}

Basically,

  • Place the value before var object - dataValue.includes(ruleValue) (js)
  • Place the value after var object - ruleValue.includes(dataValue) (js)
1
Nukhba Mehboob On

To achieve the desired behavior of checking if the rule value is a substring of the data value, you can use the contains operator instead of in.

The contains operator checks if the data value contains the specified substring.

Here's how you can modify your rule:

{
  "and": [
    {
      "contains": [
        {
          "var": "ACTION1"
        },
        "check"
      ]
    }
  ]
}

the rule will return true if the data value check the dictionary contains the substring "check", which aligns with your requirement.

Here is the sample that I tried running in the simple Json logic playground https://jsonlogic.com/play.html

Executed rule and data