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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use serde_json::json;
use serde_valid::Validate;
#[test]
fn items_err_message() {
fn rule_sample(_a: &i32) -> Result<(), serde_valid::validation::Error> {
Err(serde_valid::validation::Error::Custom(
"Rule error.".to_owned(),
))
}
#[derive(Validate)]
struct TestStruct {
#[validate(min_items = 5)]
#[validate(max_items = 2)]
#[validate]
val: Vec<TestChildStruct>,
}
#[derive(Validate)]
#[rule(rule_sample(val))]
struct TestChildStruct {
#[validate(minimum = 1)]
#[validate(maximum = 10)]
val: i32,
}
let s = TestStruct {
val: vec![
TestChildStruct { val: 0 },
TestChildStruct { val: 5 },
TestChildStruct { val: 15 },
],
};
assert_eq!(
s.validate().unwrap_err().to_string(),
json!({
"errors": [],
"properties": {
"val": {
"errors": [
"The length of the items must be `>= 5`.",
"The length of the items must be `<= 2`.",
],
"items": {
"0": {
"errors": ["Rule error."],
"properties": {
"val": {
"errors": ["The number must be `>= 1`."]
}
}
},
"1": {
"errors": ["Rule error."],
"properties": {}
},
"2": {
"errors": ["Rule error."],
"properties": {
"val": {
"errors": ["The number must be `<= 10`."]
}
}
}
}
}
}
})
.to_string()
);
}