ShinyJsonLogic โจ
A boring, correct and production-ready JSONLogic implementation for Ruby. โจ
ShinyJsonLogic is a pure Ruby, zero-dependency JSON Logic implementation, designed to offer a reliable and well-tested engine for Ruby applications.
This gem focuses on predictable behavior, strict spec alignment, high compatibility and long-term maintainability.
Why ShinyJsonLogic?
- ๐งฉ Zero runtime dependencies (stdlib-only). Just plug & play!
- ๐ฐ๏ธ Ruby 2.4+ compatible, one of the lowest minimum versions supported in the Ruby ecosystem.
- ๐ง Actively maintained and continuously improved.
- ๐ Highest JSONLogic compatibility in the Ruby ecosystem, as measured against the official test suites.
If you want JSON Logic to just work in Ruby, this is the safe default.
Test it for yourself!
Try it out in the sandbox at jsonlogicruby.com or run the official test suite with bin/test.sh to see the compatibility for yourself.
Installation
Add it to your Gemfile:
gem "shiny_json_logic"Then run:
bundle installOr install it directly:
gem install shiny_json_logicand require it in your project:
require "shiny_json_logic"Migrating from json-logic-ruby
If you're currently using json-logic-ruby, migration is seamless.
ShinyJsonLogic provides JsonLogic and JSONLogic as aliases, so you only need to swap the gem in your Gemfile:
- gem "json-logic-ruby"
+ gem "shiny_json_logic"Your existing code will work without changes:
require "shiny_json_logic"
# Both of these work exactly as before:
JsonLogic.apply(rule, data)
JSONLogic.apply(rule, data)
# Or use the new module name:
ShinyJsonLogic.apply(rule, data)Why should you give us a chance?
- ๐ Better spec compliance and fewer edge-case bugs
- โจ Support for new operators (
val,exists,??,try,throw,preserve) - ๐ง Actively maintained
- ๐งช Higher test coverage against official JSONLogic test suites
Usage
Basic usage is intentionally simple:
require "shiny_json_logic"
rule = {
"==" => [
{ "var" => "status" },
"active"
]
}
data = { "status" => "active" }
ShinyJsonLogic.apply(rule, data)
# => trueNested logic
Rules can be nested arbitrarily:
rule = {
"if" => [
{ "var" => "financing" },
{ "missing" => ["apr"] },
[]
]
}
data = { "financing" => true }
ShinyJsonLogic.apply(rule, data)
# => ["apr"]Supported operators
Our goal is full JSON Logic coverage.
Currently implemented operators include:
Logic
if, and, or, !, !!, ?:, tryโจ, throwโจ
Comparison
==, ===, !=, !==, >, >=, <, <=
Data access
var, missing, missing_some, valโจ, existsโจ
Math
+, -, *, /, %, min, max
Strings
cat, substr
Arrays
merge, in, ??โจ (coalesce operator)
Iterable operations
map, reduce, filter, some, all, none
Evaluation
preserveโจ
See the spec for the full list of operators and their behavior.
Error Handling
ShinyJsonLogic uses native Ruby exceptions for error handling:
# Unknown operators raise an error
ShinyJsonLogic.apply({ "unknown_op" => [1, 2] }, {})
# => raises ShinyJsonLogic::Errors::UnknownOperator
# Invalid arguments raise an error
ShinyJsonLogic.apply({ "+" => ["not", "numbers"] }, {})
# => raises ShinyJsonLogic::Errors::InvalidArguments
# You can use try/throw for controlled error handling within rules
rule = {
"try" => [
{ "throw" => "Something went wrong" },
{ "cat" => ["Error: ", { "var" => "type" }] }
]
}
ShinyJsonLogic.apply(rule, {})
# => "Error: Something went wrong"Error classes:
-
ShinyJsonLogic::Errors::UnknownOperator- Unknown operator in rule -
ShinyJsonLogic::Errors::InvalidArguments- Invalid arguments to operator -
ShinyJsonLogic::Errors::NotANumber- NaN result in numeric operation
Compatibility
Compatibility is measured against the official JSONLogic test suite (json-logic/.github/tests):
| Test Suite | Status |
|---|---|
| Official tests | 100% (601/601) |
See badges/compat.json for the exact numbers behind the badge.
Development
After checking out the repo:
bin/setup
bundle exec rspecOpen a console:
bin/consoleInstall locally:
bundle installHow to run the compatibility test suite:
bin/test.shContributing
Contributions are welcome โ especially:
- spec alignment improvements
- missing operators
- edge-case tests
- performance improvements
Please include tests with any change.
Repository:
https://github.com/luismoyano/shiny_json_logic
License
MIT License.
Use it. Fork it. Ship it. (:
Shine bright like a Ruby ๐