json-rules-engine-simplified icon indicating copy to clipboard operation
json-rules-engine-simplified copied to clipboard

extending predicate not working

Open theluk opened this issue 6 years ago • 0 comments

Hello,

I am a little bit frustrated because I cannot explain what is happening here.

Problem: extending predicate and then using the predicate in the conditions simply does not work.

import predicate from "predicate";
import Engine from 'json-rules-engine-simplified'
predicate.range = predicate.curry((val, range) => {
    console.log(val, range)
});
[
  {
    "conditions": { "age": { "range": [ 20, 40 ] } },
    "event": "test"
  }
]

So when running this, I receive the following error in the console, and my range is never used.

Uncaught ReferenceError: Rule contains invalid predicates 0,1 at toError (utils.js:52) at validatePredicates (validation.js:120) at Engine.validate (Engine.js:26) at Engine.addRule (Engine.js:40)

This actually makes sense to me, as here https://github.com/RxNT/json-rules-engine-simplified/blob/948d16247e67aab907acc69bdd5aff3256bbe3b5/src/validation.js#L25 it is always checked if it is a object. And at the time when the range params are coming in, it will evaluate [20, 40] to true as array is always typeof [] === "object" true. Therefore then of course Object.keys(rule) makes [20, 40] to 0,1...

What I now completely do not understand is, how it is possible that the tests run perfectly in this repo? It seems in the repo, as within the test cases, this check in the validation is never hit. How can this be?

Thanks

theluk avatar Jul 08 '18 16:07 theluk