Skip to content

Commit 945f301

Browse files
docs(book): add email validation (#23)
1 parent 0d74a46 commit 945f301

5 files changed

Lines changed: 132 additions & 5 deletions

File tree

.github/workflows/website.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ jobs:
3131
- name: Install mdBook
3232
run: cargo binstall --force -y mdbook mdbook-tabs
3333

34+
- name: Clean Fortifier
35+
run: cargo clean -p fortifier
36+
3437
- name: Build Fortifier
35-
run: cargo build -p fortifier
38+
run: cargo build -p fortifier --all-features
3639

3740
- name: Run tests
3841
run: mdbook test -L ../target/debug/deps

book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [Introduction](./introduction.md)
44
- [Getting Started](./getting-started.md)
55
- [Validations](./validations/README.md)
6-
- [Email]()
6+
- [Email](./validations/email.md)
77
- [Length](./validations/length.md)
88
- [Regex]()
99
- [URL]()

book/src/validations/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Validations
22

3+
- [Email](./email.md)
34
- [Length](./length.md)

book/src/validations/email.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Email
2+
3+
> [!NOTE]
4+
> Requires the `email` feature.
5+
6+
Validate a string is an RFC-compliant email address using the [`email_address`](https://docs.rs/email_address/latest/email_address/) crate.
7+
8+
```rust
9+
# extern crate fortifier;
10+
# use fortifier::Validate;
11+
#
12+
##[derive(Validate)]
13+
struct User {
14+
#[validate(email)]
15+
email_address: String
16+
}
17+
```
18+
19+
## Types
20+
21+
### String
22+
23+
- [`str`](https://doc.rust-lang.org/std/primitive.str.html)
24+
- [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
25+
26+
Validate the string is an RFC-compliant email address.
27+
28+
### Email address
29+
30+
- [`EmailAddress`](https://docs.rs/email_address/latest/email_address/struct.EmailAddress.html)
31+
32+
Validate the value is an RFC-compliant email address.
33+
34+
An `EmailAddress` can be constructed using [`EmailAddress::new_unchecked`](https://docs.rs/email_address/latest/email_address/struct.EmailAddress.html#method.new_unchecked) or with different options passed to [`EmailAddress::parse_with_options`](https://docs.rs/email_address/latest/email_address/struct.EmailAddress.html#method.parse_with_options), so it has to be re-validated.
35+
36+
## Options
37+
38+
### `allow_display_text`
39+
40+
Whether display text is allowed. Defaults to `false`.
41+
42+
See [`Options::allow_display_text`](https://docs.rs/email_address/latest/email_address/struct.Options.html#structfield.allow_display_text) for details.
43+
44+
```rust
45+
# extern crate fortifier;
46+
# use fortifier::Validate;
47+
#
48+
##[derive(Validate)]
49+
struct User<'a> {
50+
#[validate(email(allow_display_text = false))]
51+
email_address: &'a str
52+
}
53+
54+
fn main() {
55+
let user = User {
56+
email_address: "simon@example.com"
57+
};
58+
assert!(user.validate_sync().is_ok());
59+
60+
let user = User {
61+
email_address: "Simon <simon@example.com>"
62+
};
63+
assert!(user.validate_sync().is_err());
64+
}
65+
```
66+
67+
### `allow_domain_literal`
68+
69+
Whether domain literals are allowed. Defaults to `true`.
70+
71+
See [`Options::allow_domain_literal`](https://docs.rs/email_address/latest/email_address/struct.Options.html#structfield.allow_domain_literal) for details.
72+
73+
```rust
74+
# extern crate fortifier;
75+
# use fortifier::Validate;
76+
#
77+
##[derive(Validate)]
78+
struct User<'a> {
79+
#[validate(email(allow_domain_literal = false))]
80+
email_address: &'a str
81+
}
82+
83+
fn main() {
84+
let user = User {
85+
email_address: "simon@localhost"
86+
};
87+
assert!(user.validate_sync().is_ok());
88+
89+
let user = User {
90+
email_address: "simon@[127.0.0.1]"
91+
};
92+
assert!(user.validate_sync().is_err());
93+
}
94+
```
95+
96+
### `minimum_sub_domains`
97+
98+
The minimum number of domain segments. Defaults to `0`.
99+
100+
See [`Options::minimum_sub_domains`](https://docs.rs/email_address/latest/email_address/struct.Options.html#structfield.minimum_sub_domains) for details.
101+
102+
```rust
103+
# extern crate fortifier;
104+
# use fortifier::Validate;
105+
#
106+
##[derive(Validate)]
107+
struct User<'a> {
108+
#[validate(email(minimum_sub_domains = 2))]
109+
email_address: &'a str
110+
}
111+
112+
fn main() {
113+
let user = User {
114+
email_address: "simon@example.com"
115+
};
116+
assert!(user.validate_sync().is_ok());
117+
118+
let user = User {
119+
email_address: "simon@localhost"
120+
};
121+
assert!(user.validate_sync().is_err());
122+
}
123+
```

book/src/validations/length.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Validate the amount of entries in an iterable.
4040

4141
## Options
4242

43-
### Equal
43+
### `equal`
4444

4545
The length should be equal to the specified expression.
4646

@@ -55,7 +55,7 @@ struct User {
5555
}
5656
```
5757

58-
### Minimum
58+
### `min`
5959

6060
The length should be equal to or greater than the specified expression.
6161

@@ -70,7 +70,7 @@ struct User {
7070
}
7171
```
7272

73-
### Maximum
73+
### `max`
7474

7575
The length should be equal to or less than the specified expression.
7676

0 commit comments

Comments
 (0)