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

How to perform logical operations? #112

Open
aesher9o1 opened this issue Apr 26, 2021 · 6 comments
Open

How to perform logical operations? #112

aesher9o1 opened this issue Apr 26, 2021 · 6 comments

Comments

@aesher9o1
Copy link

Suppose a value of a key depends on multiple factors. Say a variable isAllowedAcess depends on the user's location and the user's device. Is there a way to design such schema?

@devinivy
Copy link
Member

I believe that currently no way to do that: you'd have to push that logic down into the params that you pass to the store. But in my opinion it would be a useful feature. Do you have any suggestions for an API to achieve this?

@aesher9o1
Copy link
Author

Currently here's how i plan to do it in our organization

{
  "allow": {
    "_and": [
      {
        "$filter": "globalSessionCount",
        "$range": [{ "limit": 10, "value": true }]
      },
      {
        "$filter": "localSessionCount.length",
        "$range": [{ "limit": 5, "value": true }]
      }
    ]
  },
  "$meta": {
    "name": "concurrent session rule"
  }
}

I have created a wrapper that checks if the node you have requested has an _and. This has way too much limitation hence we do not plan to use it actively.

@devinivy
Copy link
Member

devinivy commented May 5, 2021

I wonder if one way to speak to this would be to allow the user to provide a function with their logic within the schema. The function would be passed the params and env, and its result would be injected into the store when get() is called. This is just a rough mockup to illustrate, but perhaps that would look something like this:

new Confidence.Store({
  isAllowedAccess: {
    $fn: ({ location, device }, { NODE_ENV }) => NODE_ENV === 'production' && location !== 'US' && device !== 'iphone'
  }
});

@Nargonath
Copy link
Member

Nargonath commented May 7, 2021

I like that suggestion @devinivy. This would serve a broad range of use cases without complexifying the API a bunch. If we were to go the logical operators route we'd have to add numerous operators and there will always be people who'd want specific operator but here you can do whatever you want in your function.

@damusix
Copy link

damusix commented Jan 29, 2022

Whats the status of this? Looking to regex against ENVs for manifest plugins. This $fn(criteria): any concept would be very useful.

@devinivy
Copy link
Member

If there are no other proposals, I would take a PR for $fn 👍

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

No branches or pull requests

4 participants