Struct inquire::validator::MinLengthValidator
source · 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) -> Self
pub fn new(limit: usize) -> Self
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>) -> Self
pub fn with_message(self, message: impl Into<String>) -> Self
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 moresource§impl<T: ?Sized> MultiOptionValidator<T> for MinLengthValidator
impl<T: ?Sized> MultiOptionValidator<T> for MinLengthValidator
source§fn validate(
&self,
input: &[ListOption<&T>]
) -> Result<Validation, CustomUserError>
fn validate( &self, input: &[ListOption<&T>] ) -> Result<Validation, CustomUserError>
Confirm the given input list is a valid value.
source§impl StringValidator for MinLengthValidator
impl StringValidator for MinLengthValidator
source§fn validate(&self, input: &str) -> Result<Validation, CustomUserError>
fn validate(&self, input: &str) -> Result<Validation, CustomUserError>
Confirm the given input string is a valid value.
Auto Trait Implementations§
impl RefUnwindSafe for MinLengthValidator
impl Send for MinLengthValidator
impl Sync for MinLengthValidator
impl Unpin for MinLengthValidator
impl UnwindSafe for MinLengthValidator
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more