Trait inquire::validator::StringValidator
source · pub trait StringValidator: DynClone {
// Required method
fn validate(&self, input: &str) -> Result<Validation, CustomUserError>;
}
Expand description
Validator that receives a string slice as the input, such as Text
and
Password
.
If the input provided by the user is valid, your validator should return Ok(Validation::Valid)
.
If the input is not valid, your validator should return Ok(Validation::Invalid(ErrorMessage))
,
where the content of ErrorMessage
is recommended to be a string whose content will be displayed
to the user as an error message. It is also recommended that this value gives a helpful feedback to the user.
§Examples
use inquire::validator::{StringValidator, Validation};
let validator = |input: &str| match input.chars().find(|c| c.is_numeric()) {
Some(_) => Ok(Validation::Valid),
None => Ok(Validation::Invalid(
"Your password should contain at least 1 digit".into(),
)),
};
assert_eq!(Validation::Valid, validator.validate("hunter2")?);
assert_eq!(
Validation::Invalid("Your password should contain at least 1 digit".into()),
validator.validate("password")?
);
Required Methods§
sourcefn validate(&self, input: &str) -> Result<Validation, CustomUserError>
fn validate(&self, input: &str) -> Result<Validation, CustomUserError>
Confirm the given input string is a valid value.