Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<=' operator does not distinguish 0 and null, should be '<==' #109

Open
gasp opened this issue Feb 9, 2022 · 1 comment
Open

<=' operator does not distinguish 0 and null, should be '<==' #109

gasp opened this issue Feb 9, 2022 · 1 comment

Comments

@gasp
Copy link

gasp commented Feb 9, 2022

let's consider this rule, looking for values between or equals 0 and 1

const rule = { '<=': [0, { var: 'data' }, 1] };

with data being 0, it works

const zeroInput = { data: 0 };
const zeroArrayInput = { data: [0] };
jsonLogic.apply(rule, zeroInput); // returns true
jsonLogic.apply(rule, zeroArrayInput); // returns true

with data being null, this is wrongly interpreted

const nullInput = { data: null };
jsonLogic.apply(rule, nullInput); // returns true, whereas it should be returning false
@jwadhams
Copy link
Owner

jwadhams commented Feb 9, 2022

0 <= null is true in JavaScript.

The implementation is literally just calling the language builtin in this case... there's a little extra sugar to allow a three-way-comparison (for "between" like 0 <= X <= 2) but it all boils down to JavaScript, in it's ineffable way, deciding that 0 <= null is true

The implementation already puts all the boring operations (the ones that don't control flow and can be executed depth-first) in the operations object so you could replace the default <= using one of your own design (maybe throwing a type error if you try to compare with NULL) just by using addOperation without having to fork the project itself.

You could also use addOperation to implement an <== operator for your own, then you'd have both tools available. It didn't occur to me in the JsonLogic spec because no language I work in day-to-day has one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants