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

Support functions in input context #33

Open
justsml opened this issue Sep 23, 2022 · 6 comments
Open

Support functions in input context #33

justsml opened this issue Sep 23, 2022 · 6 comments

Comments

@justsml
Copy link
Contributor

justsml commented Sep 23, 2022

No description provided.

@dara-rockwell
Copy link

This is still failing with both of the following:

import { ruleFactory } from '@elite-libs/rules-machine';

const things = [
  {
    name: "ABC"
  },
  {
    name: "DEF"
  },
  {
    name: "123"
  }
];

const thingRules = ([
  {
    if: 'isConditional(thing.name) == false', then: 'keep = false'
  },
  {
    return: "keep"
  }
]);

const filterWithRules = ruleFactory(thingRules)

function isFiltered(thing: Record<string, string>): boolean {
  const result = filterWithRules({
    thing,
    isConditional: (value:string) => value.endsWith('C'),
    keep: true
  });
  return result;
}

console.dir(things.filter(isFiltered));
import { ruleFactory } from '@elite-libs/rules-machine';

const things = [
  {
    name: "ABC"
  },
  {
    name: "DEF"
  },
  {
    name: "123"
  }
];

const thingRules = ([
  {
    if: 'isConditional(thing.name)', then: 'keep = false'
  },
  {
    return: "keep"
  }
]);

const filterWithRules = ruleFactory(thingRules)

function isFiltered(thing: Record<string, string>): boolean {
  const result = filterWithRules({
    thing,
    isConditional: (value:string) => value.endsWith('C'),
    keep: true
  });
  return result;
}

console.dir(things.filter(isFiltered));

@dara-rockwell
Copy link

@dara-rockwell
Copy link

dara-rockwell commented Sep 26, 2022

Found what may be the source of the problem:

import { ruleFactory } from '@elite-libs/rules-machine';

const things = [
  {
    rateCode: "Z2H",
    provider: "Marriott",
    shouldKeep: false
  },
  {
    rateCode: "C13",
    provider: "This is a Marriott Hotel",
    shouldKeep: false
  },
];

const thingRules = ([
  {
    if: 'stringContains(LOWER(thing.provider), "marriott")',
    then: "keep = false"
  },
  {
    return: "keep"
  }
]);

const filterWithRules = ruleFactory(thingRules, { trace: true })

function isFiltered(thing: Record<string, string>): boolean {
  const result = filterWithRules({
    thing,
    stringContains: (value: string, search: string): boolean => value.includes(search),
    keep: true
  });

  console.dir(result.trace)

  return result;
}

console.dir(things.filter(isFiltered));

According to the trace, it appears that stringContains is stripped out of the evaluated string:

  {
    operation: 'expression',
    rule: 'stringContains(LOWER(thing.provider), "marriott")',
    result: [ 'this is a marriott hotel', 'marriott' ],
    stepRow: 0,
    stepCount: 1
  },
  {
    operation: 'if',
    rule: 'stringContains(LOWER(thing.provider), "marriott")',
    result: true,
    currentState: '{"thing":{"rateCode":"C13","provider":"This is a Marriott Hotel","shouldKeep":false},"keep":true}',
    stepRow: 0,
    stepCount: 1
  },

I base this presumption on how it is evaluated if I strip the function name out manually:

{
    operation: 'expression',
    rule: '(LOWER(thing.provider), "marriott")',
    result: [ 'this is a marriott hotel', 'marriott' ],
    stepRow: 0,
    stepCount: 1
  },
  {
    operation: 'if',
    rule: '(LOWER(thing.provider), "marriott")',
    result: true,
    currentState: '{"thing":{"rateCode":"C13","provider":"This is a Marriott Hotel","shouldKeep":false},"keep":true}',
    stepRow: 0,
    stepCount: 1
  },

@dara-rockwell
Copy link

@chhatch @justsml

@dara-rockwell
Copy link

dara-rockwell commented Sep 26, 2022

@justsml
Copy link
Contributor Author

justsml commented Oct 31, 2022

Good spotting @dara-rockwell.

It looks like there is a function safe list thing going on.
Which reminds me, I need to put a lot more thought into this - the security 🙀 issues could be many.

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

No branches or pull requests

2 participants