async-graphql/src/validators/min_length.rs

18 lines
404 B
Rust
Raw Normal View History

2021-11-14 13:09:14 +00:00
use crate::{InputType, InputValueError};
2021-11-15 01:12:13 +00:00
pub fn min_length<T: AsRef<str> + InputType>(
2021-11-14 13:09:14 +00:00
value: &T,
len: usize,
) -> Result<(), InputValueError<T>> {
if value.as_ref().len() >= len {
Ok(())
} else {
Err(format!(
"the string length is {}, must be greater than or equal to {}",
value.as_ref().len(),
len
)
.into())
}
}