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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::{traits::Length, MaxLengthError};

/// Max length validation of the string.
///
/// See <https://json-schema.org/understanding-json-schema/reference/string.html#length>
///
/// ```rust
/// use serde_json::json;
/// use serde_valid::{Validate, ValidateMaxLength};
///
/// struct MyType(String);
///
/// impl ValidateMaxLength for MyType {
///     fn validate_max_length(
///         &self,
///         max_length: usize,
///     ) -> Result<(), serde_valid::MaxLengthError> {
///         self.0.validate_max_length(max_length)
///     }
/// }
///
/// #[derive(Validate)]
/// struct TestStruct {
///     #[validate(max_length = 5)]
///     val: MyType,
/// }
///
/// let s = TestStruct {
///     val: MyType(String::from("abcdef")),
/// };
///
/// assert_eq!(
///     s.validate().unwrap_err().to_string(),
///     json!({
///         "errors": [],
///         "properties": {
///             "val": {
///                 "errors": ["The length of the value must be `<= 5`."]
///             }
///         }
///     })
///     .to_string()
/// );
/// ```
pub trait ValidateMaxLength {
    fn validate_max_length(&self, max_length: usize) -> Result<(), MaxLengthError>;
}

impl<T> ValidateMaxLength for T
where
    T: Length + ?Sized,
{
    fn validate_max_length(&self, max_length: usize) -> Result<(), MaxLengthError> {
        if max_length >= self.length() {
            Ok(())
        } else {
            Err(MaxLengthError::new(max_length))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::borrow::Cow;
    use std::ffi::{OsStr, OsString};
    use std::path::{Path, PathBuf};

    #[test]
    fn test_validate_string_max_length_ascii_is_true() {
        assert!(ValidateMaxLength::validate_max_length("abcde", 5).is_ok());
        assert!(ValidateMaxLength::validate_max_length("abcde", 6).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_unicode_is_true() {
        assert!(ValidateMaxLength::validate_max_length("a̐éö̲", 3).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_japanese_is_true() {
        assert!(ValidateMaxLength::validate_max_length("あ堯", 2).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_emoji_is_true() {
        assert!(ValidateMaxLength::validate_max_length("😍👺🙋🏽👨‍🎤👨‍👩‍👧‍👦", 5).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_string_type() {
        assert!(ValidateMaxLength::validate_max_length(&String::from("abcde"), 5).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_cow_str_type() {
        assert!(ValidateMaxLength::validate_max_length(&Cow::from("abcde"), 5).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_os_str_type() {
        assert!(ValidateMaxLength::validate_max_length(OsStr::new("fo�o"), 4).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_os_string_type() {
        assert!(ValidateMaxLength::validate_max_length(&OsString::from("fo�o"), 4).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_path_type() {
        assert!(ValidateMaxLength::validate_max_length(&Path::new("./foo/bar.txt"), 13).is_ok());
    }

    #[test]
    fn test_validate_string_max_length_path_buf_type() {
        assert!(
            ValidateMaxLength::validate_max_length(&PathBuf::from("./foo/bar.txt"), 13).is_ok()
        );
    }
}