-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.rs
More file actions
89 lines (77 loc) · 2.63 KB
/
Copy pathvalidate.rs
File metadata and controls
89 lines (77 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Host-side field checks (not Angular `NgModel` / forms).
//!
//! Prefer compiling a [`Regex`] once when the Host is constructed; pass `&Regex`
//! into [`pattern`] / [`pattern_value`] on each `get` rather than recompiling.
use regex::Regex;
use crate::value::Value;
/// Show validation UI only after the user has edited the field (Angular dirty semantics).
#[must_use]
pub const fn show_when_dirty(invalid: bool, dirty: bool) -> bool {
invalid && dirty
}
/// Error when `value` is empty or whitespace-only.
#[must_use]
pub fn required(value: &str) -> Option<&'static str> {
if value.trim().is_empty() {
Some("This field is required")
} else {
None
}
}
/// [`required`] against a [`Value`] (uses string / input-event text).
#[must_use]
pub fn required_value(value: &Value) -> Option<&'static str> {
value
.as_str()
.map_or(Some("This field is required"), required)
}
/// Error when non-empty `value` has fewer than `min` Unicode scalar values.
///
/// Empty strings pass so optional fields can use this without [`required`].
#[must_use]
pub fn min_length(value: &str, min: usize) -> Option<&'static str> {
let len = value.chars().count();
if len == 0 || len >= min {
None
} else {
Some("Minimum length is not met")
}
}
/// [`min_length`] against a [`Value`] (missing / non-string treated as empty).
#[must_use]
pub fn min_length_value(value: &Value, min: usize) -> Option<&'static str> {
min_length(value.as_str().unwrap_or(""), min)
}
/// Error when `value` has more than `max` Unicode scalar values.
#[must_use]
pub fn max_length(value: &str, max: usize) -> Option<&'static str> {
if value.chars().count() <= max {
None
} else {
Some("Maximum length exceeded")
}
}
/// [`max_length`] against a [`Value`] (missing / non-string treated as empty).
#[must_use]
pub fn max_length_value(value: &Value, max: usize) -> Option<&'static str> {
max_length(value.as_str().unwrap_or(""), max)
}
/// Error when `value` does not fully match `re` ([`Regex::is_match`]).
#[must_use]
pub fn pattern(value: &str, re: &Regex) -> Option<&'static str> {
if re.is_match(value) {
None
} else {
Some("Invalid format")
}
}
/// [`pattern`] against a [`Value`] (missing / non-string treated as empty).
#[must_use]
pub fn pattern_value(value: &Value, re: &Regex) -> Option<&'static str> {
pattern(value.as_str().unwrap_or(""), re)
}
/// First non-`None` message in `checks` (compose `required` + length / pattern).
#[must_use]
pub fn first_error(checks: &[Option<&'static str>]) -> Option<&'static str> {
checks.iter().copied().find_map(|msg| msg)
}