json-logic-rb 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +18 -9
- data/lib/json_logic/engine.rb +6 -1
- data/lib/json_logic/errors/unknown_operator_error.rb +7 -0
- data/lib/json_logic/operations/exists.rb +1 -1
- data/lib/json_logic/operations/missing.rb +1 -1
- data/lib/json_logic/operations/missing_some.rb +1 -1
- data/lib/json_logic/operations/try.rb +1 -1
- data/lib/json_logic/semantics.rb +6 -0
- data/lib/json_logic/version.rb +1 -1
- data/lib/json_logic.rb +0 -1
- metadata +3 -3
- data/lib/json_logic/ext/array.rb +0 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 810c50b59976cf832b8171f84696315690a47d99a894f65e7430b6a62f5effa0
|
|
4
|
+
data.tar.gz: d398558a21b6b194da078ba175f03349ddc5f8f17a51d939e0b554f267a9e15d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a4fb7f21bfebbd7f852f62be1dc7acf4a9047aac1af3405b5d37e3c64fdc9b08b7c30b85a3168e1b64c716aefcf2526a6003b20ba5494ce2b8c55f1c5b01d225
|
|
7
|
+
data.tar.gz: 604c4a8adbe4b06fd130e0fcc07443a79899d2f1f777bf6b82da815fb9277fef5bff7c2c596329ce62042ece12d1258f0cb61fc997a43bf532546efb7881f618
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
[![jsonlogic core][src-core]]
|
|
1
|
+
[![jsonlogic core][src-core]][core-tests] [![jsonlogic community][src-community]][community-suites] [![compliance 100%][badge-compliance]][ci-main] [![Build Status][badge-buildkite]][ci-main] [![rubygems][badge-rubygems]][rubygems] [![license][badge-license]][license]
|
|
2
2
|
|
|
3
3
|
# json-logic-rb
|
|
4
4
|
|
|
@@ -8,12 +8,12 @@ Ruby implementation of [JsonLogic](https://jsonlogic.com/). Pure and extensible.
|
|
|
8
8
|
- [What](#what)
|
|
9
9
|
- [Install](#install)
|
|
10
10
|
- [Quick start](#quick-start)
|
|
11
|
-
- [
|
|
11
|
+
- [Compliance](#compliance)
|
|
12
12
|
- [Supported Operations (Built-in)](#supported-operations-built-in)
|
|
13
13
|
- [Adding Operations](#adding-operations)
|
|
14
14
|
- [Enable JsonLogic Semantics (optional)](#enable-jsonlogic-semantics-optional)
|
|
15
15
|
- [Parameters](#parameters)
|
|
16
|
-
- [Proc
|
|
16
|
+
- [Proc and Lambda](#proc-and-lambda)
|
|
17
17
|
- [Class](#class)
|
|
18
18
|
- [Laziness](#laziness)
|
|
19
19
|
- [1. Default Operations](#1-default-operations)
|
|
@@ -24,7 +24,7 @@ Ruby implementation of [JsonLogic](https://jsonlogic.com/). Pure and extensible.
|
|
|
24
24
|
- [Truthiness](#truthiness)
|
|
25
25
|
- [Security](#security)
|
|
26
26
|
- [License](#license)
|
|
27
|
-
- [
|
|
27
|
+
- [Maintainers](#maintainers)
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
@@ -75,11 +75,11 @@ JsonLogic.apply(rule, data)
|
|
|
75
75
|
|
|
76
76
|
|
|
77
77
|
|
|
78
|
-
##
|
|
78
|
+
## Compliance
|
|
79
79
|
|
|
80
80
|
The JsonLogic specification provides test suites — concrete inputs with expected outputs that validate the implementation of operations. The specification comes in two variants:
|
|
81
|
-
- [![jsonlogic core][src-core]](https://jsonlogic.com
|
|
82
|
-
- [![jsonlogic community][src-community]](https://github.com/json-logic
|
|
81
|
+
- [![jsonlogic core][src-core]](https://jsonlogic.com) – [original JsonLogic website](https://jsonlogic.com/tests.json)
|
|
82
|
+
- [![jsonlogic community][src-community]](https://github.com/json-logic) – [extensions built on top of the core](https://github.com/json-logic/compat-tables/tree/main/suites)
|
|
83
83
|
|
|
84
84
|
The "extra" exists because "core" hasn't changed in years — and that’s fine, "core" is a solid, finished foundation. Think of it as v1, while "extra" is the v2+ evolution as there are no visible plans to change the original.
|
|
85
85
|
|
|
@@ -144,13 +144,22 @@ The JsonLogic specification provides test suites — concrete inputs with expect
|
|
|
144
144
|
|
|
145
145
|
[src-core]: https://img.shields.io/badge/jsonlogic-core-2ea44f?style=flat-square
|
|
146
146
|
[src-community]: https://img.shields.io/badge/jsonlogic--community-extra-0366d6?style=flat-square
|
|
147
|
+
[badge-compliance]: https://img.shields.io/badge/compliance-100%25-brightgreen
|
|
148
|
+
[badge-buildkite]: https://img.shields.io/github/actions/workflow/status/tavrelkate/json-logic-rb/compliance.yml?branch=main&label=build
|
|
149
|
+
[badge-rubygems]: https://img.shields.io/gem/v/json-logic-rb
|
|
150
|
+
[badge-license]: https://img.shields.io/badge/license-MIT-informational
|
|
151
|
+
[core-tests]: https://jsonlogic.com
|
|
152
|
+
[community-suites]: https://github.com/json-logic
|
|
153
|
+
[ci-main]: https://github.com/tavrelkate/json-logic-rb/actions/workflows/compliance.yml?query=branch%3Amain
|
|
154
|
+
[rubygems]: https://rubygems.org/gems/json-logic-rb
|
|
155
|
+
[license]: LICENSE
|
|
147
156
|
|
|
148
157
|
|
|
149
158
|
## Adding Operations
|
|
150
159
|
|
|
151
160
|
Don’t expect JsonLogic to include every specialized operation. It’s intentionally small and not a programming language. It will never do everything.
|
|
152
161
|
|
|
153
|
-
Before you reach for a custom solution, see if you can express your logic using the
|
|
162
|
+
Before you reach for a custom solution, see if you can express your logic using the [§Supported Operations (Built-in)](#supported-operations-built-in). Often, a simple change in perspective is all you need to get the job done with what's already there.
|
|
154
163
|
|
|
155
164
|
If that doesn't cut it, adding a custom operation is straightforward. Keep it simple: start with a Proc or a Lambda. If needed – promote it to a Class.
|
|
156
165
|
|
|
@@ -172,7 +181,7 @@ Operator function use a consistent call shape:
|
|
|
172
181
|
->((string, prefix), data) { string.to_s.start_with?(prefix.to_s) }
|
|
173
182
|
```
|
|
174
183
|
|
|
175
|
-
### Proc
|
|
184
|
+
### Proc and Lambda
|
|
176
185
|
|
|
177
186
|
Pick the Operation type.
|
|
178
187
|
|
data/lib/json_logic/engine.rb
CHANGED
|
@@ -33,7 +33,12 @@ module JsonLogic
|
|
|
33
33
|
return rule.transform_values { |value| evaluate(value, data) }
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
if op_class.values_only?
|
|
37
|
+
evaluated = evaluate(raw_args, data)
|
|
38
|
+
args = evaluated.is_a?(Array) ? evaluated : [evaluated]
|
|
39
|
+
else
|
|
40
|
+
args = raw_args
|
|
41
|
+
end
|
|
37
42
|
begin
|
|
38
43
|
result = op_class.new.call(args, data)
|
|
39
44
|
raise JsonLogic::NaNError.new if result.is_a?(Float) && (result.nan? || result.infinite?)
|
|
@@ -10,5 +10,5 @@ class JsonLogic::Operations::Exists < JsonLogic::Operation
|
|
|
10
10
|
|
|
11
11
|
private
|
|
12
12
|
|
|
13
|
-
def normalize_path(args, data) = args.is_a?(Array) ?
|
|
13
|
+
def normalize_path(args, data) = args.is_a?(Array) ? args.as_array.map { |part| JsonLogic.apply(part, data) } : JsonLogic.apply(args, data)
|
|
14
14
|
end
|
|
@@ -6,6 +6,6 @@ class JsonLogic::Operations::Missing < JsonLogic::Operation
|
|
|
6
6
|
def self.name = "missing"
|
|
7
7
|
|
|
8
8
|
def call(args, data)
|
|
9
|
-
|
|
9
|
+
args.as_array.select { |key| JsonLogic::Tree.new(data).dig(key).nil? }
|
|
10
10
|
end
|
|
11
11
|
end
|
|
@@ -7,7 +7,7 @@ class JsonLogic::Operations::MissingSome < JsonLogic::Operation
|
|
|
7
7
|
|
|
8
8
|
def call((min_ok, list), data)
|
|
9
9
|
json = JsonLogic::Tree.new(data)
|
|
10
|
-
keys =
|
|
10
|
+
keys = list.as_array
|
|
11
11
|
missing = keys.select { |key| json.dig(key).nil? }
|
|
12
12
|
(keys.size - missing.size) >= min_ok ? [] : missing
|
|
13
13
|
end
|
|
@@ -6,7 +6,7 @@ class JsonLogic::Operations::Try < JsonLogic::LazyOperation
|
|
|
6
6
|
def self.name = "try"
|
|
7
7
|
|
|
8
8
|
def call(arguments, data)
|
|
9
|
-
arguments =
|
|
9
|
+
arguments = arguments.as_array
|
|
10
10
|
raise JsonLogic::InvalidArgumentsError.new if arguments.empty?
|
|
11
11
|
|
|
12
12
|
state = { data: data, error: nil }
|
data/lib/json_logic/semantics.rb
CHANGED
data/lib/json_logic/version.rb
CHANGED
data/lib/json_logic.rb
CHANGED
|
@@ -6,7 +6,6 @@ require_relative 'json_logic/errors/error'
|
|
|
6
6
|
require_relative 'json_logic/errors/logic_error'
|
|
7
7
|
require_relative 'json_logic/errors/invalid_arguments_error'
|
|
8
8
|
require_relative 'json_logic/errors/nan_error'
|
|
9
|
-
require_relative 'json_logic/ext/array'
|
|
10
9
|
require_relative 'json_logic/tree'
|
|
11
10
|
require_relative 'json_logic/scope'
|
|
12
11
|
require_relative 'json_logic/operation'
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-logic-rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tavrel Kate
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-09-06 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
12
|
description: Ruby implementation of JsonLogic. JsonLogic rules are JSON trees. The
|
|
13
13
|
engine walks that tree and returns a Ruby value. Full compliance with both core
|
|
@@ -25,7 +25,7 @@ files:
|
|
|
25
25
|
- lib/json_logic/errors/invalid_arguments_error.rb
|
|
26
26
|
- lib/json_logic/errors/logic_error.rb
|
|
27
27
|
- lib/json_logic/errors/nan_error.rb
|
|
28
|
-
- lib/json_logic/
|
|
28
|
+
- lib/json_logic/errors/unknown_operator_error.rb
|
|
29
29
|
- lib/json_logic/lazy_operation.rb
|
|
30
30
|
- lib/json_logic/operation.rb
|
|
31
31
|
- lib/json_logic/operations/add.rb
|