Fix: Distinguish missing vs null in var resolution (use default only when missing)#59
Conversation
…n using MISSING (cherry picked from commit 0e14b3b)
var resolution (use default only when missing)
|
This PR now only contains the changes I originally intended. |
|
@jamsesso It seems the publish package step failed: |
@jamsesso Could you please take a look? |
Is there any chance you could look into the publish package failure? |
Supersedes #58; this PR contains only the fix.
This fixes
varresolution so a default is applied only when the path is missing, not when the value is explicitlynull.Why
Per the JsonLogic docs, the second
varargument is a default “for values that might be missing.” Treatingnullas missing deviates from that intent and leads to surprising results.What changed
MISSINGsentinel to track “not found” during path traversal.containsKeyto differentiate an absent key from a present key withnull.MISSINGfor out-of-bounds indices.evaluate(JsonLogicVariable, …)applies the default only when the traversal yieldsMISSING; explicitnullis 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:nullreturnsnull(no default).nullor non-traversable types returnnull.varkey 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.