1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use Value;
use crateJsonMatcherError;
/// 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`](JsonMatcher::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
/// });
/// ```