Skip to content

Commit 59c3027

Browse files
feat: add enum support (#15)
1 parent 047360b commit 59c3027

11 files changed

Lines changed: 905 additions & 355 deletions

File tree

example/src/email_address.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use fortifier::Validate;
2+
3+
#[derive(Validate)]
4+
pub enum ChangeEmailAddressRelation {
5+
Create {
6+
#[validate(email)]
7+
email_address: String,
8+
},
9+
Update {
10+
id: String,
11+
12+
#[validate(email)]
13+
email_address: String,
14+
},
15+
Delete {
16+
id: String,
17+
},
18+
}

example/src/main.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,11 @@
1-
use std::{error::Error, sync::LazyLock};
1+
mod email_address;
2+
mod user;
23

3-
use fortifier::Validate;
4-
use regex::Regex;
5-
6-
static COUNTRY_CODE_REGEX: LazyLock<Regex> =
7-
LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("Regex should be valid."));
8-
9-
#[derive(Validate)]
10-
struct CreateUser {
11-
#[validate(email)]
12-
email: String,
13-
14-
#[validate(length(min = 1, max = 256))]
15-
name: String,
16-
17-
#[validate(url)]
18-
url: String,
19-
20-
#[validate(regex = &COUNTRY_CODE_REGEX)]
21-
country_code: String,
4+
use std::error::Error;
225

23-
#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
24-
#[validate(length(min = 1))]
25-
locales: Vec<String>,
26-
}
6+
use fortifier::Validate;
277

28-
#[derive(Debug)]
29-
struct OneLocaleRequiredError;
30-
31-
fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
32-
if locales.is_empty() {
33-
Err(OneLocaleRequiredError)
34-
} else {
35-
Ok(())
36-
}
37-
}
8+
use crate::{email_address::ChangeEmailAddressRelation, user::CreateUser};
389

3910
#[tokio::main]
4011
async fn main() -> Result<(), Box<dyn Error>> {
@@ -48,5 +19,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
4819

4920
data.validate().await?;
5021

22+
let data = ChangeEmailAddressRelation::Create {
23+
email_address: "john@doe.com".to_owned(),
24+
};
25+
26+
data.validate().await?;
27+
28+
let data = ChangeEmailAddressRelation::Update {
29+
id: "1".to_owned(),
30+
email_address: "john@doe.com".to_owned(),
31+
};
32+
33+
data.validate().await?;
34+
35+
let data = ChangeEmailAddressRelation::Delete { id: "1".to_owned() };
36+
37+
data.validate().await?;
38+
5139
Ok(())
5240
}

example/src/user.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::sync::LazyLock;
2+
3+
use fortifier::Validate;
4+
use regex::Regex;
5+
6+
static COUNTRY_CODE_REGEX: LazyLock<Regex> =
7+
LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("Regex should be valid."));
8+
9+
#[derive(Validate)]
10+
pub struct CreateUser {
11+
#[validate(email)]
12+
pub email: String,
13+
14+
#[validate(length(min = 1, max = 256))]
15+
pub name: String,
16+
17+
#[validate(url)]
18+
pub url: String,
19+
20+
#[validate(regex = &COUNTRY_CODE_REGEX)]
21+
pub country_code: String,
22+
23+
#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
24+
#[validate(length(min = 1))]
25+
pub locales: Vec<String>,
26+
}
27+
28+
#[derive(Debug)]
29+
pub struct OneLocaleRequiredError;
30+
31+
fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
32+
if locales.is_empty() {
33+
Err(OneLocaleRequiredError)
34+
} else {
35+
Ok(())
36+
}
37+
}

packages/fortifier-macros/src/validate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod r#enum;
22
mod field;
3+
mod fields;
34
mod r#struct;
45
mod r#union;
56

0 commit comments

Comments
 (0)