pub struct ExactLengthValidator { /* private fields */ }
Expand description
Built-in validator that checks whether the answer length is equal to the specified value.
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::{ExactLengthValidator, StringValidator, Validation};
let validator = ExactLengthValidator::new(3);
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(
Validation::Invalid("The length of the response should be 3".into()),
validator.validate("No")?,
);
let validator = ExactLengthValidator::new(3).with_message("Three characters please.");
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(Validation::Invalid("Three characters please.".into()), validator.validate("No")?);
Implementations§
source§impl ExactLengthValidator
impl ExactLengthValidator
sourcepub fn new(length: usize) -> ExactLengthValidator
pub fn new(length: usize) -> ExactLengthValidator
Create a new instance of this validator, requiring exactly the given length, otherwise returning an error with default message.
sourcepub fn with_message(self, message: impl Into<String>) -> ExactLengthValidator
pub fn with_message(self, message: impl Into<String>) -> ExactLengthValidator
Define a custom error message returned by the validator.
Defaults to The length of the response should be $length
.
Trait Implementations§
source§impl Clone for ExactLengthValidator
impl Clone for ExactLengthValidator
source§fn clone(&self) -> ExactLengthValidator
fn clone(&self) -> ExactLengthValidator
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