-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser.rs
More file actions
40 lines (30 loc) · 958 Bytes
/
user.rs
File metadata and controls
40 lines (30 loc) · 958 Bytes
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
use std::sync::LazyLock;
use fortifier::Validate;
use regex::Regex;
static COUNTRY_CODE_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("valid regex"));
#[derive(Validate)]
pub struct CreateUser {
#[validate(length(min = 1, max = 256))]
pub name: String,
#[validate(email_address)]
pub email_address: String,
#[validate(phone_number)]
pub phone_number: String,
#[validate(url)]
pub url: String,
#[validate(regex = &COUNTRY_CODE_REGEX)]
pub country_code: String,
#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
#[validate(length(min = 1))]
pub locales: Vec<String>,
}
#[derive(Debug, PartialEq)]
pub struct OneLocaleRequiredError;
fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
if locales.is_empty() {
Err(OneLocaleRequiredError)
} else {
Ok(())
}
}