pub trait MultiOptionValidator<T>: DynClone
where 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§

source

fn validate( &self, input: &[ListOption<&T>], ) -> Result<Validation, Box<dyn Error + Sync + Send>>

Confirm the given input list is a valid value.

Trait Implementations§

source§

impl<T> Clone for Box<dyn MultiOptionValidator<T>>

source§

fn clone(&self) -> Box<dyn MultiOptionValidator<T>>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Implementors§