bhgames / json-logic-ruby Goto Github PK
View Code? Open in Web Editor NEWBuild complex rules, serialize them as JSON, and execute them in ruby
License: MIT License
Build complex rules, serialize them as JSON, and execute them in ruby
License: MIT License
Seems like symbolized keys in both data and filters give JSONLogic some grief.
[1] pry(main)> require 'json_logic'
=> true
[2] pry(main)> require 'active_support/core_ext/hash'
=> true
[3] pry(main)> filter = { "in": [ { "var": "foo.bar" }, [5] ] }
=> {:in=>[{:var=>"foo.bar"}, [5]]}
[4] pry(main)> good = { foo: { bar: 5 } }
=> {:foo=>{:bar=>5}}
[5] pry(main)> bad = { foo: { bar: 10 } }
=> {:foo=>{:bar=>10}}
[6] pry(main)> JSONLogic.apply(filter, good)
NoMethodError: undefined method `var' for JSONLogic::Operation:Class
from /opt/rh/rh-ruby24/root/usr/local/share/gems/gems/json_logic-0.3/lib/json_logic/operation.rb:117:in `perform'
[9] pry(main)> JSONLogic.apply(filter.deep_stringify_keys, good.deep_stringify_keys)
=> true
Given the common use of symbols and the way a number of database libraries return JSON/JSONB data, it may be worth making it symbol/string agnostic.
JSONLogic.apply({ "+" => [{"var": "age"}, 1] }, {"age": 75})
returns NoMethodError: undefined method `var' for JSONLogic::Operation:Class
On JSONLogic.com, is 76.
The JS library exposes a function called uses_data
which is useful to check if all variables have data in provided data.
jwadhams/json-logic-js#39 (comment)
It'd be awesome to have this in this ruby implementation too so that we can do validation that all the variable values are available before trying to evaluate (which could lead to weird errors)
I've noticed that the in
operator is failing when receiving nil
values as the array.
We have been using JSONLogic for a couple of years now to evaluate rules on serialized objects at runtime. Most of the time, we don't know the actual values of the variables used in the rules since they are built by serializing objects retrieved from our database.
When the variable (array to search in) doesn't exist in the object, a nil
value is passed as the array, so in that case the gem is failing due to a NoMethodError: undefined method
include?' for nil:NilClass`.
Here you can see an example:
rule = {"in" => ["value_to_search", {"var" => "array"}]}
variable = {"array" => nil} (Consider that nil is the value after some processing at runtime)
JSONLogic.apply(rule, variable)
According to the spec at https://jsonlogic.com/, the ==
operator should return true when comparing inputs such as 1.0
and 1
I've noticed that the operations in this port work entirely different then they do in the original JS port.
Specifically here:
json-logic-ruby/lib/json_logic/operation.rb
Line 117 in 97b4b5d
JSONLogic is defined to accept the elements in an array as the argument to an operation.
However, your operations accept an array and the original data object.
With all the testing I've done so far, it seems like everything officially supported in the JS port works the same in this Ruby port.
Ex)
rule = {+: [1, 2]}
data = {}
JSONLogic.apply(rule, data) # 3
The result of this will be 3
in both ports โ
However, if you were to define something like this in ruby:
rule = {+: [{var: 'data'}]}
data = {data: [1,2]}
JSONLogic.apply(rule, data) # 3
and in JS
rule = {"+": [{"var": "data"}]}
data = {"data": [1,2]}
jsonLogic.apply(rule, data) # 1
You end up with 3
in Ruby and 1
in JS. โ
Ultimately this leads to inconsistencies when trying to develop cross-platform.
Is there any way to compare 2 Json logic expressions for equality other than doing a deep compare of the hash object?
I believe this is a bug, but please tell me if it's just my expectation that needs to be corrected:
irb(main):028:0> data = {"code": "45736123"}
=> {:code=>"45736123"}
irb(main):029:0> rule = {"in": ["123",{"var": "code"}]}
=> {:in=>["123", {:var=>"code"}]}
irb(main):030:0> JSONLogic.apply(rule,data)
=> false
Works fine when hardcoded:
irb(main):031:0> rule = {"in": ["123","45736123"]}
=> {:in=>["123", "45736123"]}
irb(main):032:0> JSONLogic.apply(rule,data)
=> true
irb(main):033:0> JSONLogic.apply(rule,nil)
=> true
Hi, would you accept a PR to add a size
or length
operator? I would implement it as described here: jwadhams/json-logic#4. Though that issue provides a workaround it's a lot of code to do a very simple thing. Feel free to close this after replying.
Problem Statement:
While trying to access a subset array from within a superset array using the "All" operator, I found that the "IN" operator does not function correctly when an array is accessed via the "VAR" operator.
Observed Behavior:
In the rule example below, the "IN" operator fails to correctly validate if all elements of the subset array are contained within the superset array:
Rule:
{ "all": [ {"var": "subset"}, {"in": [{"var": ""}, {"var": "superset"}]} ] }
Data
{ "subset": ["apple", "banana", "orange"], "superset": ["apple", "banana", "orange", "grape", "pear"] }
Expected Outcome:
The evaluation of this rule should yield True, as the subset array is fully included in the superset array.
Actual Outcome:
The rule incorrectly returns False.
Hint / Workaround:
When I specify the array directly within the rule instead of referencing it with the "VAR" operator, the "IN" operator behaves as expected:
Working Rule:
{ "all": [ {"var": "subset"}, {"in": [{"var": ""}, ["apple", "banana", "orange", "grape", "pear"]]} ] }
I suspect there might be a bug in the way the "IN" operator processes references to arrays. Your assistance in identifying and resolving the root cause of this behavior would be greatly appreciated.
This so-called 'gem' only works as advertised with Javascript.
Don't waste your time...
-- cc
I noticed that the repo is not being actively maintained for quite a long time. I would be pleased to dedicate same time to help in the maintenance of this gem if you agreed. Please let me know what do you think @bhgames.
Not an actual issue, just wanted to say thank you so much for creating this gem!
https://rubygems.org/gems/json_logic stores only 0.1 there is no version supporting added add_method from here: 48568b6
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.