Fix: Distinguish missing vs null in var
resolution (use default only when missing)
#59
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supersedes #58; this PR contains only the fix.
This fixes
var
resolution so a default is applied only when the path is missing, not when the value is explicitlynull
.Why
Per the JsonLogic docs, the second
var
argument is a default “for values that might be missing.” Treatingnull
as missing deviates from that intent and leads to surprising results.What changed
MISSING
sentinel to track “not found” during path traversal.containsKey
to differentiate an absent key from a present key withnull
.MISSING
for out-of-bounds indices.evaluate(JsonLogicVariable, …)
applies the default only when the traversal yieldsMISSING
; explicitnull
is returned asnull
.Examples
{"var": ["user.age", 42]}
with{"user":{"age": null}}
→ null (no default){"var": ["items.2", "missing"]}
with{"items":[10,20]}
→ "missing"{"var": ""}
returns the entire data object (same instance)Tests
Added coverage to
VariableTests
:null
returnsnull
(no default).null
or non-traversable types returnnull
.var
key returns the original data object.Compatibility
Behavior change for callers who relied on “default-for-null.” This aligns with the JsonLogic spec and other interpreters.