macro_rules! length { ($length:expr) => { ... }; ($length:expr, $message:expr) => { ... }; }
Expand description
Shorthand for the built-in ExactLengthValidator
that checks whether the answer length is
equal to the specified value.
Arguments
$length
- Expected length of the input.$message
- optional - Error message returned by the validator. Defaults to “The length of the response should be $length”
Examples
use inquire::{length, validator::{StringValidator, Validation}};
let validator = length!(3);
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(Validation::Invalid("The length of the response should be 3".into()), validator.validate("No")?);
let validator = length!(3, "Three characters please.");
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(Validation::Invalid("Three characters please.".into()), validator.validate("No")?);