pub struct MinLengthValidator { /* private fields */ }
Expand description
Built-in validator that checks whether the answer length is larger than or equal to the specified threshold.
The validator uses a custom-built length function that has a special implementation for strings which counts the number of graphemes. See this StackOverflow question.
Examples
use inquire::validator::{MinLengthValidator, StringValidator, Validation};
let validator = MinLengthValidator::new(3);
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(
Validation::Invalid("The length of the response should be at least 3".into()),
validator.validate("No")?,
);
let validator = MinLengthValidator::new(3).with_message("You have to give me more than that!");
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(
Validation::Invalid("You have to give me more than that!".into()),
validator.validate("No")?,
);
Implementations§
source§impl MinLengthValidator
impl MinLengthValidator
sourcepub fn new(limit: usize) -> MinLengthValidator
pub fn new(limit: usize) -> MinLengthValidator
Create a new instance of this validator, requiring at least the given length, otherwise returning an error with default message.
sourcepub fn with_message(self, message: impl Into<String>) -> MinLengthValidator
pub fn with_message(self, message: impl Into<String>) -> MinLengthValidator
Define a custom error message returned by the validator.
Defaults to The length of the response should be at least $length
.
Trait Implementations§
source§impl Clone for MinLengthValidator
impl Clone for MinLengthValidator
source§fn clone(&self) -> MinLengthValidator
fn clone(&self) -> MinLengthValidator
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more