serde_valid/validation/object/
min_properties.rs1use crate::{traits::Size, MinPropertiesError};
2
3pub trait ValidateMinProperties {
49 fn validate_min_properties(&self, min_properties: usize) -> Result<(), MinPropertiesError>;
50}
51
52impl<T> ValidateMinProperties for T
53where
54 T: Size,
55{
56 fn validate_min_properties(&self, min_properties: usize) -> Result<(), MinPropertiesError> {
57 if min_properties <= self.size() {
58 Ok(())
59 } else {
60 Err(MinPropertiesError::new(min_properties))
61 }
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68 use serde_json::json;
69 use std::collections::BTreeMap;
70 use std::collections::HashMap;
71
72 #[test]
73 fn test_validate_object_min_properties_hash_map_type() {
74 let mut map = HashMap::new();
75 map.insert("key1".to_string(), "value1".to_string());
76 map.insert("key2".to_string(), "value2".to_string());
77 map.insert("key3".to_string(), "value3".to_string());
78 assert!(ValidateMinProperties::validate_min_properties(&map, 3).is_ok());
79 }
80
81 #[test]
82 fn test_validate_object_min_properties_btree_map_type() {
83 let mut map = BTreeMap::new();
84 map.insert("key1".to_string(), "value1".to_string());
85 map.insert("key2".to_string(), "value2".to_string());
86 map.insert("key3".to_string(), "value3".to_string());
87 assert!(ValidateMinProperties::validate_min_properties(&map, 3).is_ok());
88 }
89
90 #[test]
91 fn test_validate_object_min_properties_json_map_type() {
92 let value = json!({
93 "key1": "value1",
94 "key2": "value2",
95 "key3": "value3",
96 });
97 let map = value.as_object().unwrap();
98
99 assert!(ValidateMinProperties::validate_min_properties(map, 2).is_ok());
100 assert!(ValidateMinProperties::validate_min_properties(map, 3).is_ok());
101 }
102
103 #[test]
104 fn test_validate_object_min_properties_is_false() {
105 let value = json!({
106 "key1": "value1",
107 "key2": "value2",
108 "key3": "value3",
109 });
110 let map = value.as_object().unwrap();
111
112 assert!(ValidateMinProperties::validate_min_properties(map, 4).is_err());
113 }
114}