pub trait MultiOptionValidator<T>: DynClonewhere
T: ?Sized,{
// Required method
fn validate(
&self,
input: &[ListOption<&T>],
) -> Result<Validation, Box<dyn Error + Sync + Send>>;
}
Expand description
Validator used in MultiSelect
prompts.
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::list_option::ListOption;
use inquire::validator::{MultiOptionValidator, Validation};
let validator = |input: &[ListOption<&&str>]| {
if input.len() <= 2 {
Ok(Validation::Valid)
} else {
Ok(Validation::Invalid("You should select at most two options".into()))
}
};
let mut ans = vec![ListOption::new(0, &"a"), ListOption::new(1, &"b")];
assert_eq!(Validation::Valid, validator.validate(&ans[..])?);
ans.push(ListOption::new(3, &"d"));
assert_eq!(
Validation::Invalid("You should select at most two options".into()),
validator.validate(&ans[..])?
);
Required Methods§
sourcefn validate(
&self,
input: &[ListOption<&T>],
) -> Result<Validation, Box<dyn Error + Sync + Send>>
fn validate( &self, input: &[ListOption<&T>], ) -> Result<Validation, Box<dyn Error + Sync + Send>>
Confirm the given input list is a valid value.