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
2 changes: 1 addition & 1 deletion benchmarks/compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ validator = ["dep:validator"]

[dependencies]
fortifier = { workspace = true, features = [
"email",
"email-address",
"serde",
"url",
], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- [Enum](./validate/enum.md)
- [Struct](./validate/struct.md)
- [Validations](./validations/README.md)
- [Email](./validations/email.md)
- [Email Address](./validations/email-address.md)
- [Length](./validations/length.md)
- [Phone Number](./validations/phone-number.md)
- [Regex]()
Expand Down
10 changes: 5 additions & 5 deletions book/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Add Fortifier to your project:

```shell
cargo add fortifier --features email
cargo add fortifier --features email-address
```

See [Installation](installation.md) for more details.
Expand Down Expand Up @@ -46,7 +46,7 @@ use fortifier::Validate;

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

#[validate(length(min = 1, max = 256))]
Expand All @@ -62,11 +62,11 @@ Call the `validate_sync` method on the data structure:

```rust
# extern crate fortifier;
use fortifier::{EmailError, LengthError, Validate, ValidationErrors};
use fortifier::{EmailAddressError, LengthError, Validate, ValidationErrors};

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

#[validate(length(min = 1, max = 256))]
Expand All @@ -90,7 +90,7 @@ fn main() {
data.validate_sync(),
Err(ValidationErrors::from_iter([
CreateUserValidationError::EmailAddress(
EmailError::MissingSeparator {},
EmailAddressError::MissingSeparator {},
),
CreateUserValidationError::Name(
LengthError::Min {
Expand Down
3 changes: 2 additions & 1 deletion book/src/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ cargo add fortifier
### Validations

- `all-validations` - Enable all features below.
- `email` - Email address validation using the [`email_address`](https://docs.rs/email_address/latest/email_address/) crate.
- `email-address` - Email address validation using the [`email_address`](https://docs.rs/email_address/latest/email_address/) crate.
- `phone-number` - Phone number validation using the [`phonenumber`](https://docs.rs/phonenumber/latest/phonenumber/) crate.
- `regex` - Regular expression validation using the [`regex`](https://docs.rs/regex/latest/regex/) crate.
- `url` - URL validation using the [`url`](https://docs.rs/url/latest/url/) crate.

Expand Down
2 changes: 1 addition & 1 deletion book/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Schema validation.
- Enums & structs
- Typed errors
- Built-in validations
- Email
- Email address
- Length
- Phone number
- Regex
Expand Down
2 changes: 1 addition & 1 deletion book/src/validations/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Validations

- [Email](./email.md)
- [Email Address](./email-address.md)
- [Length](./length.md)
- [Phone Number](./phone-number.md)
- [URL](./url.md)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Email
# Email Address

> [!NOTE]
> Requires the `email` feature.
> Requires the `email-address` feature.

Validate a string is an RFC-compliant email address using the [`email_address`](https://docs.rs/email_address/latest/email_address/) crate.

Expand All @@ -11,7 +11,7 @@ Validate a string is an RFC-compliant email address using the [`email_address`](
#
##[derive(Validate)]
struct User {
#[validate(email)]
#[validate(email_address)]
email_address: String
}
```
Expand Down Expand Up @@ -47,7 +47,7 @@ See [`Options::allow_display_text`](https://docs.rs/email_address/latest/email_a
#
##[derive(Validate)]
struct User<'a> {
#[validate(email(allow_display_text = false))]
#[validate(email_address(allow_display_text = false))]
email_address: &'a str
}

Expand Down Expand Up @@ -76,7 +76,7 @@ See [`Options::allow_domain_literal`](https://docs.rs/email_address/latest/email
#
##[derive(Validate)]
struct User<'a> {
#[validate(email(allow_domain_literal = false))]
#[validate(email_address(allow_domain_literal = false))]
email_address: &'a str
}

Expand Down Expand Up @@ -105,7 +105,7 @@ See [`Options::minimum_sub_domains`](https://docs.rs/email_address/latest/email_
#
##[derive(Validate)]
struct User<'a> {
#[validate(email(minimum_sub_domains = 2))]
#[validate(email_address(minimum_sub_domains = 2))]
email_address: &'a str
}

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version.workspace = true

[dependencies]
fortifier = { workspace = true, features = [
"email",
"email-address",
"phone-number",
"regex",
"url",
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/src/email_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use fortifier::Validate;
#[derive(Validate)]
pub enum ChangeEmailAddressRelation {
Create {
#[validate(email)]
#[validate(email_address)]
email_address: String,
},
Update {
id: String,

#[validate(email)]
#[validate(email_address)]
email_address: String,
},
Delete {
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{email_address::ChangeEmailAddressRelation, user::CreateUser};
async fn main() -> Result<(), Box<dyn Error>> {
let data = CreateUser {
name: "John Doe".to_owned(),
email: "john@doe.com".to_owned(),
email_address: "john@doe.com".to_owned(),
phone_number: "+44 20 7946 0000".to_owned(),
url: "https://john.doe.com".to_owned(),
country_code: "GB".to_owned(),
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct CreateUser {
#[validate(length(min = 1, max = 256))]
pub name: String,

#[validate(email)]
pub email: String,
#[validate(email_address)]
pub email_address: String,

#[validate(phone_number)]
pub phone_number: String,
Expand Down
2 changes: 1 addition & 1 deletion examples/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ version.workspace = true
[dependencies]
axum = "0.8.7"
fortifier = { workspace = true, features = [
"email",
"email-address",
"regex",
"serde",
"url",
Expand Down
2 changes: 1 addition & 1 deletion examples/server/src/user/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use utoipa::ToSchema;

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

#[validate(length(min = 1, max = 256))]
Expand Down
8 changes: 5 additions & 3 deletions packages/fortifier-macros/src/validate/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use syn::{Field, Ident, Result, Visibility};
use crate::{
validate::{attributes::enum_attributes, r#type::should_validate_type},
validation::{Execution, Validation},
validations::{Custom, Email, Length, Nested, PhoneNumber, Regex, Url},
validations::{Custom, EmailAddress, Length, Nested, PhoneNumber, Regex, Url},
};

pub enum LiteralOrIdent {
Expand Down Expand Up @@ -67,8 +67,10 @@ impl<'a> ValidateField<'a> {
result.validations.push(Box::new(Custom::parse(&meta)?));

Ok(())
} else if meta.path.is_ident("email") {
result.validations.push(Box::new(Email::parse(&meta)?));
} else if meta.path.is_ident("email_address") {
result
.validations
.push(Box::new(EmailAddress::parse(&meta)?));

Ok(())
} else if meta.path.is_ident("length") {
Expand Down
4 changes: 2 additions & 2 deletions packages/fortifier-macros/src/validations.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod custom;
mod email;
mod email_address;
mod length;
mod nested;
mod phone_number;
mod regex;
mod url;

pub use custom::*;
pub use email::*;
pub use email_address::*;
pub use length::*;
pub use nested::*;
pub use phone_number::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use syn::{Ident, LitBool, LitInt, Result, meta::ParseNestedMeta};

use crate::validation::{Execution, Validation};

pub struct Email {
pub struct EmailAddress {
allow_display_text: bool,
allow_domain_literal: bool,
minimum_sub_domains: usize,
}

impl Default for Email {
impl Default for EmailAddress {
fn default() -> Self {
Self {
allow_display_text: false,
Expand All @@ -20,9 +20,9 @@ impl Default for Email {
}
}

impl Validation for Email {
impl Validation for EmailAddress {
fn parse(meta: &ParseNestedMeta<'_>) -> Result<Self> {
let mut result = Email::default();
let mut result = EmailAddress::default();

if !meta.input.is_empty() {
meta.parse_nested_meta(|meta| {
Expand Down Expand Up @@ -51,10 +51,10 @@ impl Validation for Email {
}

fn ident(&self) -> Ident {
format_ident!("Email")
format_ident!("EmailAddress")
}
fn error_type(&self) -> TokenStream {
quote!(::fortifier::EmailError)
quote!(::fortifier::EmailAddressError)
}

fn expr(&self, execution: Execution, expr: &TokenStream) -> Option<TokenStream> {
Expand All @@ -66,13 +66,13 @@ impl Validation for Email {

Some(quote! {
{
const EMAIL_ADDRESS_OPTIONS: ::fortifier::EmailOptions = ::fortifier::EmailOptions {
const EMAIL_ADDRESS_OPTIONS: ::fortifier::EmailAddressOptions = ::fortifier::EmailAddressOptions {
allow_display_text: #allow_display_text,
allow_domain_literal: #allow_domain_literal,
minimum_sub_domains: #minimum_sub_domains,
};

::fortifier::ValidateEmail::validate_email(&#expr, EMAIL_ADDRESS_OPTIONS)
::fortifier::ValidateEmailAddress::validate_email_address(&#expr, EMAIL_ADDRESS_OPTIONS)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/fortifier-macros/tests/validate/enum_named_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use fortifier::{Validate, ValidationErrors};
#[derive(Validate)]
enum ChangeEmailAddressRelation {
Create {
#[validate(email)]
#[validate(email_address)]
email_address: String,
},
Update {
id: String,

#[validate(email)]
#[validate(email_address)]
email_address: String,
},
Delete {
Expand Down
4 changes: 2 additions & 2 deletions packages/fortifier-macros/tests/validate/enum_unnamed_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use fortifier::{Validate, ValidationErrors};

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use fortifier::{Validate, ValidateEmail, ValidateLength, ValidationErrors};
use fortifier::{Validate, ValidateEmailAddress, ValidateLength, ValidationErrors};

#[derive(Validate)]
struct CreateUser<E: ValidateEmail, N: ValidateLength<usize>> {
#[validate(email)]
email: E,
struct CreateUser<E: ValidateEmailAddress, N: ValidateLength<usize>> {
#[validate(email_address)]
email_address: E,

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

fn main() -> Result<(), ValidationErrors<CreateUserValidationError>> {
let data = CreateUser {
email: "john@doe.com",
email_address: "john@doe.com",
name: "John Doe",
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use fortifier::{Validate, ValidationErrors};

#[derive(Validate)]
struct CreateUser<'a, 'b> {
#[validate(email)]
email: &'a str,
#[validate(email_address)]
email_address: &'a str,

#[validate(length(min = 1, max = 256))]
name: &'b str,
}

fn main() -> Result<(), ValidationErrors<CreateUserValidationError>> {
let data = CreateUser {
email: "john@doe.com",
email_address: "john@doe.com",
name: "John Doe",
};

Expand Down
6 changes: 3 additions & 3 deletions packages/fortifier-macros/tests/validate/struct_named_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use fortifier::{Validate, ValidationErrors};

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

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

fn main() -> Result<(), ValidationErrors<CreateUserValidationError>> {
let data = CreateUser {
email: "john@doe.com".to_owned(),
email_address: "john@doe.com".to_owned(),
name: "John Doe".to_owned(),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use fortifier::Validate;

#[derive(Validate)]
struct EmailAddressData<'a> {
#[validate(email_address(allow_display_text = 1))]
value: &'a str,
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: expected boolean literal
--> tests/validations/email-address/invalid_allow_display_text_fail.rs:5:51
|
5 | #[validate(email_address(allow_display_text = 1))]
| ^
Loading
Loading