Fix == / != to match JavaScript abstract equality for null#23
Open
erikgaal wants to merge 1 commit into
Open
Conversation
The JsonLogic format is designed so a rule evaluates identically in
json-logic-js (front end) and this library (back end). For null, the two
diverged:
{"==": [null, 0]} => json-logic-js: false json-logic-php: true
In JavaScript, null is loosely-equal only to null/undefined, never to 0,
false or "". PHP's native == coerces null to those, so ==/!= returned the
opposite of the reference implementation whenever an operand was null (for
example a missing or unanswered field compared with "== 0").
Guard the null case in == and != so null is loosely-equal only to null,
matching json-logic-js. Non-null comparisons are unchanged. The shared
jsonlogic.com test suite still passes; added focused coverage for the null
cases.
6a76a66 to
bb24f45
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
JsonLogic's value proposition is that a rule evaluates identically on the front end (
json-logic-js) and the back end (this library). Fornull, they diverge:In JavaScript,
nullis loosely-equal only tonull/undefined, never to0,false, or"". PHP's native==coercesnullto those, so==/!=return the opposite of the reference implementation whenever an operand isnull. This bites real rules: a missing or unanswered field compared with{"==": [{"var": "x"}, 0]}isfalsein the browser buttruehere, so client-side visibility and server-side validation disagree.Fix
Guard the
nullcase in==and!=sonullis loosely-equal only tonull, matchingjson-logic-js. Non-null comparisons are untouched (PHP 8 already matches JS for those).Tests
The shared jsonlogic.com
tests.jsonparity suite still passes (it has nonull-equality cases, which is how this gap went unnoticed). Addedtests/NullLooseEqualityTest.phpcoveringnullagainstnull,0,false,"","0",[]for both==and!=, plus non-null sanity cases.Scope is intentionally limited to the
nulldivergence; broader JS-coercion edge cases are left as-is.