icydb_model/base/validator/intl/
phone.rs1use crate::{prelude::*, visitor::Validator};
8
9#[validator]
16pub struct E164PhoneNumber;
17
18impl Validator<str> for E164PhoneNumber {
19 fn validate(&self, s: &str, ctx: &mut dyn VisitorContext) {
20 let Some(digits) = s.strip_prefix('+') else {
21 ctx.issue("phone number must start with +");
22 return;
23 };
24
25 if digits.is_empty() || !digits.chars().all(|ch| ch.is_ascii_digit()) {
26 ctx.issue("phone number must contain only digits after +");
27 return;
28 }
29
30 if digits.starts_with('0') {
31 ctx.issue("phone number country code must not start with 0");
32 return;
33 }
34
35 let digit_count = digits.len();
36
37 if !(7..=15).contains(&digit_count) {
38 ctx.issue(format!(
39 "phone number has {digit_count} digits; expected 7 to 15"
40 ));
41 }
42 }
43}
44
45#[cfg(test)]
50mod tests;