macro_rules! min_length {
    ($length:expr) => { ... };
    ($length:expr, $message:expr) => { ... };
}
Expand description

Built-in validator that checks whether the answer length is larger than or equal to the specified threshold.

The validator uses a custom-built length function that has a special implementation for strings which counts the number of graphemes. See this StackOverflow question.

Arguments

  • $length - Minimum length of the input.
  • $message - optional - Error message returned by the validator. Defaults to “The length of the response should be at least $length”

Examples

use inquire::{min_length, validator::{StringValidator, Validation}};

let validator: StringValidator = min_length!(3);
assert_eq!(Validation::Valid, validator("Yes")?);
assert_eq!(Validation::Invalid("The length of the response should be at least 3".into()), validator("No")?);

let validator: StringValidator = min_length!(3, "You have to give me more than that!");
assert_eq!(Validation::Valid, validator("Yes")?);
assert_eq!(Validation::Invalid("You have to give me more than that!".into()), validator("No")?);