Trait inquire::validator::CustomTypeValidator

source ·
pub trait CustomTypeValidator<T: ?Sized>: DynClone {
    // Required method
    fn validate(&self, input: &T) -> Result<Validation, CustomUserError>;
}
Expand description

Validator used in CustomType 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: &T) -> Result<Validation, CustomUserError>

Confirm the given input list is a valid value.

Trait Implementations§

source§

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

source§

fn clone(&self) -> Self

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§

source§

impl<F, T> CustomTypeValidator<T> for F