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

Can't get json-logic-engine to work with this logic #20

Closed
boozedog opened this issue Nov 10, 2023 · 3 comments
Closed

Can't get json-logic-engine to work with this logic #20

boozedog opened this issue Nov 10, 2023 · 3 comments

Comments

@boozedog
Copy link

Hi there! I am evaluating json-logic-engine and I am finding that the following logic compiles but crashes at runtime. Same rule/data works in json-logic-js.

Logic:

{
    "map": [
        {
            "var": ""
        },
        {
            "*": {
                "cat": [
                    {
                        "var": "a"
                    },
                    " ",
                    {
                        "var": "b"
                    }
                ]
            }
        }
    ]
}

Data:

[
    {
        "a": "foo",
        "b": "bar"
    },
    {
        "a": "fizz",
        "b": "buzz"
    }
]

Error:

Uncaught TypeError: ("" + ((intermediate value) ?? null) + " " + ((intermediate value) ?? null)).reduce is not a function
@boozedog boozedog changed the title Can't get JsonLogic to work with this logic Can't get json-logic-engine to work with this logic Nov 10, 2023
@TotalTechGeek
Copy link
Owner

TotalTechGeek commented Nov 10, 2023

I'm not entirely sure that this should be considered valid logic; I'm tempted to put some work in to make the error message better (and not crash it at runtime), but the logic is essentially asking:

multiply("foo bar")

Multiplying with one argument, where that argument is a string.

Is the expectation here that if multiply receives a single operand, it just returns the same value, with the same type? I'm not sure if I want to make that the default behavior, but if that is indeed your need, you can do this:

engine.addMethod('*', (data) => Array.isArray(data) ? data.reduce((a, b) => +a * +b) : data, { deterministic: true })

This can be used to override the behavior.

I can be convinced though!

@boozedog
Copy link
Author

Ahh thanks @TotalTechGeek ! I am new to JsonLogic ... that was definitely a mistake in my logic. I was just trying to return some strings as part of my learning process. Here's the corrected json:

{
    "map": [
        {
            "var": ""
        },
        {
            "cat": [
                {
                    "var": "a"
                },
                " ",
                {
                    "var": "b"
                }
            ]
        }
    ]
}

which returns the expected output: ["foo bar","fizz buzz"]

I agree that a better error message would be helpful, but on the other hand json-logic-js executes it with no error at all which seems worse to me!

Thanks for your help

@TotalTechGeek
Copy link
Owner

If you have any other questions, please don't hesitate to drop a comment!

I'm going to leave this issue open to look into improving the error messages / validation.

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