Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions example/src/email_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use fortifier::Validate;

#[derive(Validate)]
pub enum ChangeEmailAddressRelation {
Create {
#[validate(email)]
email_address: String,
},
Update {
id: String,

#[validate(email)]
email_address: String,
},
Delete {
id: String,
},
}
56 changes: 22 additions & 34 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
use std::{error::Error, sync::LazyLock};
mod email_address;
mod user;

use fortifier::Validate;
use regex::Regex;

static COUNTRY_CODE_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("Regex should be valid."));

#[derive(Validate)]
struct CreateUser {
#[validate(email)]
email: String,

#[validate(length(min = 1, max = 256))]
name: String,

#[validate(url)]
url: String,

#[validate(regex = &COUNTRY_CODE_REGEX)]
country_code: String,
use std::error::Error;

#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
#[validate(length(min = 1))]
locales: Vec<String>,
}
use fortifier::Validate;

#[derive(Debug)]
struct OneLocaleRequiredError;

fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
if locales.is_empty() {
Err(OneLocaleRequiredError)
} else {
Ok(())
}
}
use crate::{email_address::ChangeEmailAddressRelation, user::CreateUser};

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

data.validate().await?;

let data = ChangeEmailAddressRelation::Create {
email_address: "john@doe.com".to_owned(),
};

data.validate().await?;

let data = ChangeEmailAddressRelation::Update {
id: "1".to_owned(),
email_address: "john@doe.com".to_owned(),
};

data.validate().await?;

let data = ChangeEmailAddressRelation::Delete { id: "1".to_owned() };

data.validate().await?;

Ok(())
}
37 changes: 37 additions & 0 deletions example/src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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("Regex should be valid."));

#[derive(Validate)]
pub struct CreateUser {
#[validate(email)]
pub email: String,

#[validate(length(min = 1, max = 256))]
pub name: 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)]
pub struct OneLocaleRequiredError;

fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
if locales.is_empty() {
Err(OneLocaleRequiredError)
} else {
Ok(())
}
}
1 change: 1 addition & 0 deletions packages/fortifier-macros/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod r#enum;
mod field;
mod fields;
mod r#struct;
mod r#union;

Expand Down
Loading