-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
43 lines (33 loc) · 914 Bytes
/
main.rs
File metadata and controls
43 lines (33 loc) · 914 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
41
42
43
use std::error::Error;
use fortifier::Validate;
#[derive(Validate)]
struct CreateUser {
#[validate(email)]
email: String,
#[validate(length(min = 1, max = 256))]
name: String,
#[validate(url)]
url: String,
#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
locales: Vec<String>,
}
#[derive(Debug)]
struct OneLocaleRequiredError;
fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
if locales.is_empty() {
Err(OneLocaleRequiredError)
} else {
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let data = CreateUser {
email: "john@doe.com".to_owned(),
name: "John Doe".to_owned(),
url: "https://john.doe.com".to_owned(),
locales: vec!["en_GB".to_owned()],
};
data.validate().await?;
Ok(())
}