pub fn word_count(n: usize) -> ValidatorExpand description
Exactly n whitespace-separated words.
Examples found in repository?
examples/prompt_validate.rs (line 27)
9fn main() {
10 let _ = colors(); // silence unused-import warning if you remove theming below
11
12 intro("Validation playground");
13
14 let abort = "Aborted — nothing was saved.";
15
16 // 12-word seed phrase, composed from primitives.
17 let lowercase_words = Validator::new(|v: &str| {
18 if v.split_whitespace()
19 .all(|w| w.chars().all(|c| c.is_ascii_lowercase()))
20 {
21 Ok(())
22 } else {
23 Err("Words must be lowercase ASCII".into())
24 }
25 });
26 let _seed = text("Recovery phrase")
27 .rule(word_count(12).and(lowercase_words))
28 .run()
29 .or_cancel(abort);
30
31 let _pw = secret("Set a password")
32 .rule(
33 min_chars(12)
34 .and(has_upper())
35 .and(has_lower())
36 .and(has_digit())
37 .and(has_special())
38 .msg("≥12 chars with upper, lower, digit, and a special char"),
39 )
40 .run()
41 .or_cancel(abort);
42
43 let _mail = text("Email")
44 .placeholder("you@example.com")
45 .rule(email())
46 .run()
47 .or_cancel(abort);
48
49 let _port = text("Listen port")
50 .default("8080")
51 .rule(int_between(1024, 65535))
52 .run()
53 .or_cancel(abort);
54
55 let _user = text("Username")
56 .rule(
57 min_chars(3)
58 .and(max_chars(20))
59 .and(alphanumeric())
60 .msg("3–20 alphanumeric characters"),
61 )
62 .run()
63 .or_cancel(abort);
64
65 let _name = text("Display name")
66 .validate(|s: &str| {
67 if s.contains(char::is_whitespace) {
68 Err("No spaces allowed".into())
69 } else {
70 Ok(())
71 }
72 })
73 .run()
74 .or_cancel(abort);
75
76 outro("All validators passed!");
77}