pub fn predicate<P>(
predicate: P,
) -> JsonPredicateMatcher<P, NoDescription, NoDescription>
Expand description
Creates a custom JSON matcher from an arbitrary predicate function.
This function allows defining ad-hoc JSON matchers inline by supplying a closure or function
that returns true
for matching values.
The resulting matcher can optionally be extended with:
.with_description("expected", "not expected")
— to provide custom messages, and.with_explain_fn(|v| Description::new().text(...))
— to describe mismatches dynamically.
§Example
let matcher = json::predicate(|v| v.as_i64().map_or(false, |n| n > 0))
.with_description("a positive number", "a non-positive number");
verify_that!(j!(42), &matcher);
verify_that!(j!(-1), not(&matcher));
Use this when no built-in matcher (like is_string()
or is_null()
) fits your case.