TotalTechGeek / json-logic-engine

Construct complex rules with JSON & process them.
MIT License
46 stars 9 forks source link

Inconsistent behavior when using the "in" string operation #25

Closed beeme1mr closed 10 months ago

beeme1mr commented 10 months ago

Overview

There's an inconsistency between the json-logic-js and json-logic-engine when using the 'in' string operation. It appears to be undefined behavior according to https://jsonlogic.com/tests.json but the way json-logic-js behaves is more in line with what I expected the results to be.

Steps to reproduce

JsonLogic JS

  1. Go to the JsonLogic playground
  2. Add the rule: {"in":["Spring", { "var": "city" }]}
  3. Do not define data
  4. Click "Compute"

You should see "false" as the output. Adding { "city": "Springfield" } as Data and recomputing returns "true".

JSON Logic Engine

  1. Go to the JSON Logic Engine playground
  2. Add the logic section: {"in":["Spring", { "var": "city" }]}
  3. Remove the predefined data. (this isn't required but makes for an easier comparison)
  4. Click "Execute"

You should see "true" as the output and an expected error in the dev console. Adding { "city": "test" } as Data and recomputing returns "false" with no errors. Adding { "city": "Springfield" } as Data and recomputing returns "true".

The console error is:

main.3cfcfd21.js:2 Uncaught TypeError: Cannot read properties of null (reading 'includes')
    at eval (eval at processBuiltString (common.78e4810e.js:1:68555), <anonymous>:1:90)
    at Object.built (VM161 common.78e4810e.js:1:436054)
    at g (VM161 common.78e4810e.js:1:60808)
    at Object.He (main.3cfcfd21.js:2:81052)
    at Ye (main.3cfcfd21.js:2:81206)
    at main.3cfcfd21.js:2:99391
    at Cr (main.3cfcfd21.js:2:99485)
    at Or (main.3cfcfd21.js:2:99899)
    at main.3cfcfd21.js:2:105547
    at ze (main.3cfcfd21.js:2:181021)

Summary

JSON Logic Engine isn't validating the runtime input type from user supplied context. JsonLogic tends to return a falsy value when user supplied context is invalid for a given ruleset. This is important for our use case because the people writing the rules may not be the ones defining the context.

TotalTechGeek commented 10 months ago

Hi @beeme1mr,

Thank you for your bug report!

I've pushed v1.3.1 to resolve this issue in the module; I'm going to try to update the docs website & set up a cicd pipeline for it very soon.

TotalTechGeek commented 10 months ago

The library used on the docs website has been updated now as well; I'll need to take the weekend to update some of the documentation & build out the pipeline, but you should be able to confirm in the sandbox now (it may require a cache reset).