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

truthiness of objects #34

Open
gregsdennis opened this issue Nov 24, 2021 · 3 comments
Open

truthiness of objects #34

gregsdennis opened this issue Nov 24, 2021 · 3 comments

Comments

@gregsdennis
Copy link
Contributor

gregsdennis commented Nov 24, 2021

The table presented on the website doesn't list how to treat objects (empty or non-empty) in regard to truthiness. Is this defined somewhere else?

On your playground,

{
  "if": [
    {}, 
    true, 
    false
  ]
}

returns true, while

{
  "if": [
    { "test": "value" },
    true,
    false
  ]
}

returns nothing.

@jwadhams
Copy link
Owner

So part of the problem is that JsonLogic is pretty crummy at handling inline objects, because it tries to use {"test":"value"} as logic too. Way at the bottom of the page there should be an error message

Unrecognized operation test

But even if we push it into a variable, I bet empty objects are sort of inconsistent by implementation.

Do you have an opinion what it should be? I think in both languages I maintain implementations for (JavaScript and PHP) objects are truthy, even empty objects. That seems the easiest to make consistent (you wouldn't have to wonder about whether an object with zero public attributes but some private attributes should behave, do methods count as properties in your language, etc etc)

@gregsdennis
Copy link
Contributor Author

gregsdennis commented Nov 25, 2021

I have a client who's expecting existence to be truthy. (See linked issue.) The specific use case has a non-empty object, so I'm not sure about empty objects.

Personally I have no preference, other than it should be defined. I just want to know what to implement.

Non-empty objects being truthy and empty objects being falsy would align with how arrays are treated.

@gregsdennis
Copy link
Contributor Author

@jwadhams do you have any more information on this?

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