Data Navigator Expectations
Assert against deeply nested data structures by path
Asserting against a deeply nested structure usually means a chain of intermediate variables and structKeyExists() guards before you reach the value you actually care about. The data navigator matchers collapse that into a single path expression, built on BoxLang's dataNavigate() built-in.
Paths are JSONPath-style expressions, supporting dot notation, array indexing, recursive descent, wildcards, slices and filters. The full syntax is listed under Path Syntax below.
The examples below all assume this structure:
var data = {
"app" : { "name" : "TestApp", "settings" : { "debug" : true, "port" : 8080 } },
"users" : [ { "name" : "Alice", "age" : 30 } ]
}Path Syntax
Every matcher on this page takes the same path expression, handed straight to BoxLang's dataNavigate().
Dot notation
app.settings.port
The value at that key path
Array index
users[1]
The first user — indexing is 1-based
Recursive descent
..name
Every name key at any depth
Wildcard, keys
app.settings.*
Every value under settings
Wildcard, array
users[*].name
The name of every user
Slice
primes[1:3]
Elements 1 through 3, inclusive
Open-ended slice
primes[2:]
Element 2 through the end
Filter
users[?(@.age > 18)].name
The name of every user over 18
Filters use @ to refer to the current element and support ==, !=, >, <, >= and <=. Whitespace inside the filter is tolerated, so [?(@.active==true)] and [?( @.active == true )] are equivalent.
A path that resolves to many values — anything using a wildcard, slice, recursive descent or filter — is best paired with queryPath(), which returns every match. path() and the toHavePath*() matchers resolve to the first match.
Existence
toHavePath()
Asserts a path resolves.
Values
toHavePathValue()
Asserts the value at a path equals an expected value.
toHavePathType()
Asserts the type at a path. Accepts standard types (string, numeric, boolean, struct, array) and the aliases str, num, bool, arr, obj and map.
toHavePathSatisfying()
Asserts the value at a path satisfies a predicate closure, for anything equality cannot express.
Chaining
path()
Navigates to a path and hands back a normal Expectation on the value there, so you can chain any matcher you like rather than being limited to the path matchers above.
queryPath()
Navigates to a path and hands back an Expectation on an array of every match. This is the one to use with wildcards, slices, filters and recursive descent, where a path resolves to many values rather than one.
Slices take an inclusive, 1-based window into an array:
A Real Example
Validating an API response is where this earns its keep:
Last updated
Was this helpful?