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§

source

fn validate(&self, input: &str) -> Result<Validation, CustomUserError>

Confirm the given input string is a valid value.

Trait Implementations§

source§

impl Clone for Box<dyn StringValidator>

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§