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

failing to check array items by index (from an empty array) #74

Closed
denisazevedo opened this issue Dec 7, 2023 · 1 comment · Fixed by #75 or #76
Closed

failing to check array items by index (from an empty array) #74

denisazevedo opened this issue Dec 7, 2023 · 1 comment · Fixed by #75 or #76

Comments

@denisazevedo
Copy link

First of all, that's a great lib, thank you all for your hard work on this!

When we try to access an specific condition in an empty array, we get index out of range.
How can we check if the array is not empty before accessing their items?

Quick playground example

logic := strings.NewReader(`{"if":[ false, {"var":"values.0.categories"}, "else" ]}`)
data := strings.NewReader(`{ "values": [] }`)

var result bytes.Buffer
if err := jsonlogic.Apply(logic, data, &result); err != nil {
    fmt.Printf("failed, %v", err)
}

fmt.Println(result.String())

In the example above, even forcing the false in the if condition, it's still evaluating the true part, causing the index out of range.
Shouldn't return the "else" value , even returning the error?
This is what the JSON Logic playground returns:

image

Is there any other way to get the value of a property from an array's item (like categories from the first position in my example)?

Thank you

@diegoholiveira
Copy link
Owner

Thanks for reporting it :)

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