"in" operator errors out if array value data is null or undefined; Should return false
See original GitHub issueGiven the following rule:
{ "in": [ "1", { "var": "MyData.Values" } ] }
Using it on the following data will cause an error when, IMO, it should return false;
{
"MyData": {
"Values": null
}
}
Should be easy enough to check for a null/undefined data set before testing the “in” operation and return false if necessary.
This may also be a problem with other supported array operations. Although, I have not tested them to see.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
TypeError: cannot use 'in' operator to search for 'x' in 'y'
The in operator can only be used to check if a property is in an object. You can't search in strings, or in...
Read more >How can I determine if a variable is 'undefined' or 'null'?
Usually you'd have EmpName(or some other variable) be passed into a function, or the return value of another function and therefore declared(Example: "var...
Read more >7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin
A detailed article about 'undefined' keyword in JavaScript. 7 tips on how to handle correctly 'undefined' and increase code durability.
Read more >JavaScript Common Mistakes - W3Schools
To solve this problem, you must test if an object is not null , and not undefined . But this can still throw...
Read more >How to Check if Object is Empty in JavaScript - Samantha Ming
Looks good so far, it returns false for non-objects. ... But watch out! These values will throw an error. // TypeError: Cannot covert...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can work on this, you’re right it’s going to make the library more robust.
In the meantime you might be able to make your rules more robust by using an empty array as your default in your var calls, like
Came to report this bug. I fixed locally by adding a “safeIn” function
jsonLogic.add_operation('safeIn', (a, b) => { if(!b || typeof b.indexOf === "undefined") return false; return (b.indexOf(a) !== -1); } )