async_graphql/validators/
chars_max_length.rs1use crate::{InputType, InputValueError};
2
3pub fn chars_max_length<T: AsRef<str> + InputType>(
4 value: &T,
5 len: usize,
6) -> Result<(), InputValueError<T>> {
7 if value.as_ref().chars().count() <= len {
8 Ok(())
9 } else {
10 Err(format!(
11 "the chars length is {}, must be less than or equal to {}",
12 value.as_ref().chars().count(),
13 len
14 )
15 .into())
16 }
17}
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22
23 #[test]
24 fn test_chars_max_length() {
25 assert!(chars_max_length(&"你好".to_string(), 3).is_ok());
26 assert!(chars_max_length(&"你好啊".to_string(), 3).is_ok());
27 assert!(chars_max_length(&"嗨你好啊".to_string(), 3).is_err());
28 }
29}