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

Ability to filter rule-groups before applying group based rules #1114

Open
chintanvora7 opened this issue Sep 2, 2024 · 2 comments
Open

Comments

@chintanvora7
Copy link

chintanvora7 commented Sep 2, 2024

Is your feature request related to a problem? Please describe.

Currently, the library supports 'some', 'all' and 'none' operators exposed by JSONLogic. While this works fine for most scenarios involving lists or arrays, there are certain gaps here in case we need to filter the array first and then apply the rules. Taking the cars example showcased here again: https://ukrbublik.github.io/react-awesome-query-builder/

Supported Scenario:

  1. Check if all car objects meet the rules
  2. Check if at least 1 car object meet the rules
  3. Check if no car object that meet the rules
  4. Check if car objects has n objects that meet the rules

Unsupported Scenario:

  1. Check if all cars where Vendor = Ford meet the rules

The use case of filtering before we apply the rules would be pretty powerful when we have a complex nested structure

Example:

Screenshot 2024-09-02 at 4 35 58 PM

JSONlogic supports this with their filter operator - https://jsonlogic.com/operations.html#map-reduce-and-filter

As example JSONLogic rule and input data is given below:

JSON Logic Rule: (Filter Cars list on vendor == Ford THEN check year == 1990)

ruleFilter = {
  "and": [
    {
      "all": [
        {
          "filter": [
            {
              "var": "cars"
            },
            {
              "and": [
                {
                  "==": [
                    {
                      "var": "vendor"
                    },
                    "Ford"
                  ]
                }
              ]
            }
          ]
        },
        {
          "==": [
            {
              "var": "year"
            },
            1990
          ]
        }
      ]
    }
  ]
}

Example Data that passes above:

{
  "cars": [
    {
      "vendor": "Toyota",
      "year": "2010",
      "mileage": 80000
    },
    {
      "vendor": "Ford",
      "year": "1990",
      "mileage": 150000
    },
    {
      "vendor": "Ford",
      "year": "1990",
      "mileage": 200000
    }
  ]
} 

Describe the solution you'd like
I've tried playing around with custom operators and it doesn't seem straightforward to implement this (please correct me if I'm wrong here). I feel the current rule-group should have a 'add filter' option which allows nested rules and then it should allow additional rules that apply on the filtered list.

Map feature could also work in a similar fashion if need be

Describe alternatives you've considered
The only way I can see right now to enable something like this is to break my rules into 2 pieces. The first piece will only support 'filter' and as filter returns a list of array. That list has to be programmatically passed in as input data to the next jsonlogic rule which would evaluate to a TRUTHY or FALSY output

Additional context
If there is an easy way to enable something with existing config, I'd appreciate an example. Appreciate the support for this library!

@chintanvora7
Copy link
Author

@ukrbublik any thoughts on if this can be added as an enhancement or somehow can be done with existing capabilities?

Would really make life easy when we're working with complex array objects of different types as you could filter what you need and then apply the rules. I understand this can be done programmatically as well but would be neat if we could leverage JSONLogic's inbuilt Filter function

@ukrbublik ukrbublik added this to the Improve config milestone Nov 12, 2024
@ukrbublik
Copy link
Owner

This can be added as an enhancement.
I don't think there is a good way to achieve this using existing capabilities.
We need to clearly distinct filter and condition queries (red and blue ones in your screenshot) for rule-group which is not yet possible.

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

No branches or pull requests

2 participants