Skip to main content

JsonMatcher

Trait JsonMatcher 

Source
pub trait JsonMatcher: Debug {
    // Required methods
    fn matches(&self, value: &Value) -> bool;
    fn description(&self) -> String;
}
Expand description

Core trait for implementing JSON value matchers.

This trait allows creation of custom matchers for flexible value validation. Implementors must provide both matching logic and a description of the matcher.

§Examples

use json_test::JsonMatcher;
use serde_json::Value;

#[derive(Debug)]
struct DateMatcher;

impl JsonMatcher for DateMatcher {
    fn matches(&self, value: &Value) -> bool {
        value.as_str()
            .map(|s| s.contains("-"))
            .unwrap_or(false)
    }

    fn description(&self) -> String {
        "is a date string".to_string()
    }
}

Required Methods§

Source

fn matches(&self, value: &Value) -> bool

Tests if a JSON value matches this matcher’s criteria.

§Arguments
  • value - The JSON value to test
§Returns

Returns true if the value matches the criteria, false otherwise.

Source

fn description(&self) -> String

Returns a human-readable description of the matcher’s criteria.

This description is used in error messages when assertions fail.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§