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
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = "0.0.1"
[workspace.dependencies]
fortifier = { path = "./packages/fortifier", version = "0.0.1" }
fortifier-macros = { path = "./packages/fortifier-macros", version = "0.0.1" }
regex = "1.12.2"
tokio = "1.48.0"

[workspace.lints.rust]
Expand Down
1 change: 1 addition & 0 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true

[dependencies]
fortifier.workspace = true
regex.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }

[lints]
Expand Down
10 changes: 9 additions & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::error::Error;
use std::{error::Error, 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)]
struct CreateUser {
Expand All @@ -13,6 +17,9 @@ struct CreateUser {
#[validate(url)]
url: String,

#[validate(regex(expr = &COUNTRY_CODE_REGEX))]
country_code: String,

#[validate(custom(function = validate_one_locale_required, error = OneLocaleRequiredError))]
#[validate(length(min = 1))]
locales: Vec<String>,
Expand All @@ -35,6 +42,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
email: "john@doe.com".to_owned(),
name: "John Doe".to_owned(),
url: "https://john.doe.com".to_owned(),
country_code: "GB".to_owned(),
locales: vec!["en_GB".to_owned()],
};

Expand Down
8 changes: 6 additions & 2 deletions packages/fortifier-macros/src/validate/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::{Field, Ident, Result};

use crate::{
validation::Validation,
validations::{Custom, Email, Length, Url},
validations::{Custom, Email, Length, Regex, Url},
};

pub struct ValidateField {
Expand Down Expand Up @@ -44,13 +44,17 @@ impl ValidateField {
} else if meta.path.is_ident("length") {
result.validations.push(Box::new(Length::parse(&meta)?));

Ok(())
} else if meta.path.is_ident("regex") {
result.validations.push(Box::new(Regex::parse(&meta)?));

Ok(())
} else if meta.path.is_ident("url") {
result.validations.push(Box::new(Url::parse(&meta)?));

Ok(())
} else {
Err(meta.error("unknown validate parameter"))
Err(meta.error("unknown parameter"))
}
})?;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/fortifier-macros/src/validations.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod custom;
mod email;
mod length;
mod regex;
mod url;

pub use custom::*;
pub use email::*;
pub use length::*;
pub use regex::*;
pub use url::*;
2 changes: 1 addition & 1 deletion packages/fortifier-macros/src/validations/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Validation for Length {

Ok(())
} else {
Err(meta.error("unknown length parameter"))
Err(meta.error("unknown parameter"))
}
})?;

Expand Down
51 changes: 51 additions & 0 deletions packages/fortifier-macros/src/validations/regex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{Expr, Ident, Result, meta::ParseNestedMeta};

use crate::validation::Validation;

pub struct Regex {
pub expr: Expr,
}

impl Validation for Regex {
fn parse(meta: &ParseNestedMeta<'_>) -> Result<Self> {
let mut expr: Option<Expr> = None;

meta.parse_nested_meta(|meta| {
if meta.path.is_ident("expr") {
expr = Some(meta.value()?.parse()?);

Ok(())
} else {
Err(meta.error("unknown parameter"))
}
})?;

let Some(expr) = expr else {
return Err(meta.error("missing expr parameter"));
};

Ok(Regex { expr })
}

fn is_async(&self) -> bool {
false
}

fn ident(&self) -> Ident {
format_ident!("Regex")
}

fn error_type(&self) -> TokenStream {
quote!(RegexError)
}

fn tokens(&self, expr: &TokenStream) -> TokenStream {
let regex_expr = &self.expr;

quote! {
#expr.validate_regex(#regex_expr)
}
}
}
4 changes: 3 additions & 1 deletion packages/fortifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ repository.workspace = true
version.workspace = true

[features]
default = ["macros", "url"]
default = ["macros", "regex", "url"]
indexmap = ["dep:indexmap"]
macros = ["dep:fortifier-macros"]
regex = ["dep:regex"]
url = ["dep:url"]

[dependencies]
fortifier-macros = { workspace = true, optional = true }
indexmap = { version = "2.12.0", optional = true }
regex = { workspace = true, optional = true }
url = { version = "2.5.7", optional = true }

[lints]
Expand Down
4 changes: 4 additions & 0 deletions packages/fortifier/src/validations.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod email;
mod length;
#[cfg(feature = "regex")]
mod regex;
#[cfg(feature = "url")]
mod url;

pub use email::*;
pub use length::*;
#[cfg(feature = "regex")]
pub use regex::*;
#[cfg(feature = "url")]
pub use url::*;
Loading
Loading