JsonMatcher

Trait JsonMatcher 

Source
pub trait JsonMatcher {
    // Required method
    fn json_matches(&self, value: &Value) -> Vec<JsonMatcherError>;
}
Expand description

Core trait for implementing JSON value matchers.

Implement this trait to create custom validation logic for JSON values. The trait requires a single method json_matches that returns a vector of errors - an empty vector indicates a successful match.

§Example

use serde_json::{json, Value};
use json_matcher::{assert_jm, JsonMatcher, JsonMatcherError};

struct OnlyVowels;

impl JsonMatcher for OnlyVowels {
    fn json_matches(&self, value: &Value) -> Vec<JsonMatcherError> {
        match value.as_str() {
            Some(s) if s.chars().all(|c| "aeiouAEIOU".contains(c)) => vec![],
            Some(_) => vec![JsonMatcherError::at_root("String contains non-vowel characters")],
            None => vec![JsonMatcherError::at_root("Expected string")],
        }
    }
}

let data = json!({
    "sound": "aeiou",
    "count": 5
});

assert_jm!(data, {
    "sound": OnlyVowels,
    "count": 5
});

Required Methods§

Implementations on Foreign Types§

Source§

impl JsonMatcher for &Value

Source§

impl JsonMatcher for &str

Source§

impl JsonMatcher for &String

Source§

impl JsonMatcher for Value

Source§

impl JsonMatcher for bool

Source§

impl JsonMatcher for f32

Source§

impl JsonMatcher for f64

Source§

impl JsonMatcher for i8

Source§

impl JsonMatcher for i16

Source§

impl JsonMatcher for i32

Source§

impl JsonMatcher for i64

Source§

impl JsonMatcher for u8

Source§

impl JsonMatcher for u16

Source§

impl JsonMatcher for u32

Source§

impl JsonMatcher for ()

Source§

impl JsonMatcher for String

Source§

impl JsonMatcher for Vec<&dyn JsonMatcher>

Source§

impl JsonMatcher for Vec<Box<dyn JsonMatcher>>

Source§

impl JsonMatcher for [&dyn JsonMatcher]

Source§

impl JsonMatcher for [Box<dyn JsonMatcher>]

Source§

impl<T: JsonMatcher> JsonMatcher for [T]

Source§

impl<T: JsonMatcher> JsonMatcher for Vec<T>

Implementors§