hapipal / confidence

Dynamic, declarative configurations
Other
264 stars 44 forks source link

How to perform logical operations? #112

Open aesher9o1 opened 3 years ago

aesher9o1 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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

devinivy commented 2 years ago

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