Skip to content

Commit

Permalink
Verify array before try to access any position (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Henrique Oliveira authored Dec 8, 2023
1 parent 3f4cded commit 2f55e83
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 10 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,16 @@ func TestIssue71_example_max(t *testing.T) {
assert.JSONEq(t, expected, result.String())
}

func TestIssue74(t *testing.T) {
logic := strings.NewReader(`{"if":[ false, {"var":"values.0.categories"}, "else" ]}`)
data := strings.NewReader(`{ "values": [] }`)

var result bytes.Buffer
_ = Apply(logic, data, &result)
expected := `"else"`
assert.JSONEq(t, expected, result.String())
}

func TestJsonLogicWithSolvedVars(t *testing.T) {
rule := json.RawMessage(`{
"or":[
Expand Down
7 changes: 6 additions & 1 deletion vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func getVar(value, data interface{}) interface{} {
}

if isSlice(data) {
_value = data.([]interface{})[int(toNumber(part))]
pos := int(toNumber(part))
container := data.([]interface{})
if pos >= len(container) {
return _default
}
_value = container[pos]
}

if _value == nil {
Expand Down

0 comments on commit 2f55e83

Please sign in to comment.