Skip to content

Commit

Permalink
If jsonpath returns empty or nil, we should error (#267)
Browse files Browse the repository at this point in the history
* If jsonpath returns empty or nil, we should error


Co-authored-by: Sam Coward <scoward@vmware.com>
  • Loading branch information
emmjohnson and idoru authored Oct 27, 2021
1 parent 53df43d commit a5a259a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/eval/jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (e Evaluator) EvaluateJsonPath(path string, obj interface{}) (interface{},
return "", fmt.Errorf("too many results for the query: %s", path)
}

if len(interfaceList) == 0 {
return "", fmt.Errorf("jsonpath returned empty list: %s", path)
}

return interfaceList[0], nil
}

Expand Down
8 changes: 2 additions & 6 deletions pkg/eval/jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,11 @@ var _ = Describe("JsonPath", func() {

Context("when evaluate returns a list of no items", func() {
BeforeEach(func() {
evaluate.Returns([]interface{}{"some error"}, nil)
evaluate.Returns([]interface{}{}, nil)
result, err = evaluator.EvaluateJsonPath(path, obj)
})

ItDoesNotReturnAnError()

It("returns that single item", func() {
Expect(result).To(Equal("some error"))
})
ItReturnsAHelpfulError("jsonpath returned empty list")
})

Context("when evaluate returns a list with a single item", func() {
Expand Down

0 comments on commit a5a259a

Please sign in to comment.