question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing/Exporting JsonLogic with additional operators

See original GitHub issue

Firstly, thank you very much for this library. It’s been a breeze to use!

I’m in the process of replacing react-awesome-query-builder with this package and have hit my first hitch.

We are using JsonLogic with some additional operators such as: JSONLogic.add_operation("lessThanOrEqualDate", isBeforeOrEqual);

When attempting to import a query that uses this operator with parseJsonLogic e.g.

{
  "query": {
    "and": [
      {
        "lessThanOrEqualDate": [
          {
            "var": "device.authenticatedAt"
          },
          "PT-24H"
        ]
      }
    ]
  }
}

Then passing it into parseJsonLogic it seems to strip it out. However, any condition that is using one of the defaultOperators works fine.

I assume it’s something to do with parseJsonLogic being scoped to the default operators. Is there anyway to let it recognise additional ones?

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
hwhmeiklecommented, Nov 15, 2022

@jakeboone02 Thanks for the help! Will close this issue.

1reaction
jakeboone02commented, Nov 14, 2022

I don’t think you need to use transformQuery, just pass a custom ruleProcessor to formatQuery like below (CodeSandbox here):

import {
  defaultRuleProcessorJsonLogic,
  formatQuery,
  RuleGroupType,
  RuleType
} from "react-querybuilder";

const query: RuleGroupType = {
  combinator: "and",
  rules: [{ field: "device.authenticatedAt", operator: "<=", value: "PT-24H" }]
};

const jsonlgc = formatQuery(query, {
  format: "jsonlogic",
  ruleProcessor: (r: RuleType) => {
    if (r.field === "device.authenticatedAt") {
      return { lessThanOrEqualDate: defaultRuleProcessorJsonLogic(r)["<="] };
    }
    return defaultRuleProcessorJsonLogic(r);
  }
});

What that’s doing is taking '<=' property from the result of the default rule processor and assigning it to the lessThanOrEqualDate property of a new object (unless you’re dealing with a different field, in which case it just falls back to the default rule processor).

You may need to add some error checking and other contingencies but hopefully that gets you headed in the right direction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Operations (and outside libraries!) - JsonLogic
You can import that library into JsonLogic's list of operators! Note: JsonLogic can't know what's going on inside your custom ... Adding one...
Read more >
Additional operators - GitHub
JsonLogic operators cover only the essentials, but there might be a need to provide some extra operators out of the box, to make...
Read more >
How to use the json-logic-js.add_operation function in ... - Snyk
To help you get started, we've selected a few json-logic-js.add_operation examples, ... jsonlogic/operators'; // Configure JsonLogic lodashOperators.
Read more >
Representing logic as data in JSON - Stack Overflow
I've also written two parsers for it: JsonLogic for JavaScript and JsonLogic for ... I need to add some type conversions and other...
Read more >
Blend4Web. User Manual
opening/saving .blend files and importing/exporting scenes and assets. ... Export menu, or type b4w export in the operator search menu ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found