Skip to content

Commit 388b48c

Browse files
docs(book): add regular expression validation
1 parent 444ac20 commit 388b48c

8 files changed

Lines changed: 92 additions & 22 deletions

File tree

book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [Email Address](./validations/email-address.md)
1111
- [Length](./validations/length.md)
1212
- [Phone Number](./validations/phone-number.md)
13-
- [Regex]()
13+
- [Regular Expression](./validations/regular-expression.md)
1414
- [URL](./validations/url.md)
1515
- [Integrations]()
1616
- [Serde]()

book/src/introduction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Schema validation.
99
- Email address
1010
- Length
1111
- Phone number
12-
- Regex
12+
- Regular expression
1313
- URL
1414
- Support for Serde & Utoipa
1515

book/src/validations/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
- [Email Address](./email-address.md)
44
- [Length](./length.md)
55
- [Phone Number](./phone-number.md)
6+
- [Regular Expression](./regular-expression.md)
67
- [URL](./url.md)

book/src/validations/email-address.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Validate a string is an RFC-compliant email address using the [`email_address`](
77

88
```rust
99
# extern crate fortifier;
10-
# use fortifier::Validate;
1110
#
12-
##[derive(Validate)]
11+
use fortifier::Validate;
12+
13+
#[derive(Validate)]
1314
struct User {
1415
#[validate(email_address)]
1516
email_address: String
@@ -43,9 +44,10 @@ See [`Options::allow_display_text`](https://docs.rs/email_address/latest/email_a
4344

4445
```rust
4546
# extern crate fortifier;
46-
# use fortifier::Validate;
4747
#
48-
##[derive(Validate)]
48+
use fortifier::Validate;
49+
50+
#[derive(Validate)]
4951
struct User<'a> {
5052
#[validate(email_address(allow_display_text = false))]
5153
email_address: &'a str
@@ -72,9 +74,10 @@ See [`Options::allow_domain_literal`](https://docs.rs/email_address/latest/email
7274

7375
```rust
7476
# extern crate fortifier;
75-
# use fortifier::Validate;
7677
#
77-
##[derive(Validate)]
78+
use fortifier::Validate;
79+
80+
#[derive(Validate)]
7881
struct User<'a> {
7982
#[validate(email_address(allow_domain_literal = false))]
8083
email_address: &'a str
@@ -101,9 +104,10 @@ See [`Options::minimum_sub_domains`](https://docs.rs/email_address/latest/email_
101104

102105
```rust
103106
# extern crate fortifier;
104-
# use fortifier::Validate;
105107
#
106-
##[derive(Validate)]
108+
use fortifier::Validate;
109+
110+
#[derive(Validate)]
107111
struct User<'a> {
108112
#[validate(email_address(minimum_sub_domains = 2))]
109113
email_address: &'a str

book/src/validations/length.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ Validate the length of a string or iterable.
44

55
```rust
66
# extern crate fortifier;
7-
# use fortifier::Validate;
87
#
9-
##[derive(Validate)]
8+
use fortifier::Validate;
9+
10+
#[derive(Validate)]
1011
struct User {
1112
#[validate(length(min = 1, max = 256))]
1213
name: String
@@ -46,9 +47,10 @@ The length should be equal to the specified expression.
4647

4748
```rust
4849
# extern crate fortifier;
49-
# use fortifier::Validate;
5050
#
51-
##[derive(Validate)]
51+
use fortifier::Validate;
52+
53+
#[derive(Validate)]
5254
struct User {
5355
#[validate(length(equal = 2))]
5456
country_code: String
@@ -61,9 +63,10 @@ The length should be equal to or greater than the specified expression.
6163

6264
```rust
6365
# extern crate fortifier;
64-
# use fortifier::Validate;
6566
#
66-
##[derive(Validate)]
67+
use fortifier::Validate;
68+
69+
#[derive(Validate)]
6770
struct User {
6871
#[validate(length(min = 1))]
6972
name: String
@@ -76,9 +79,10 @@ The length should be equal to or less than the specified expression.
7679

7780
```rust
7881
# extern crate fortifier;
79-
# use fortifier::Validate;
8082
#
81-
##[derive(Validate)]
83+
use fortifier::Validate;
84+
85+
#[derive(Validate)]
8286
struct User {
8387
#[validate(length(max = 256))]
8488
name: String

book/src/validations/phone-number.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Validate a string is a specification-compliant phone number using the [`phonenum
77

88
```rust
99
# extern crate fortifier;
10-
# use fortifier::Validate;
1110
#
12-
##[derive(Validate)]
11+
use fortifier::Validate;
12+
13+
#[derive(Validate)]
1314
struct User {
1415
#[validate(phone_number)]
1516
phone_number: String
@@ -43,6 +44,7 @@ See [`phonenumber::country::Id`](https://docs.rs/phonenumber/latest/phonenumber/
4344

4445
```rust
4546
# extern crate fortifier;
47+
#
4648
use fortifier::{PhoneNumberCountry, Validate};
4749

4850
#[derive(Validate)]
@@ -72,6 +74,7 @@ See [`phonenumber::country::Id`](https://docs.rs/phonenumber/latest/phonenumber/
7274

7375
```rust
7476
# extern crate fortifier;
77+
#
7578
use fortifier::{PhoneNumberCountry, Validate};
7679

7780
#[derive(Validate)]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Regular Expression
2+
3+
> [!NOTE]
4+
> Requires the `regex` feature.
5+
6+
Validate a string matches a regular expression using the [`regex`](https://docs.rs/regex/latest/regex/) crate.
7+
8+
```rust
9+
# extern crate fortifier;
10+
#
11+
use std::sync::LazyLock;
12+
13+
use fortifier::Validate;
14+
use regex::Regex;
15+
16+
static COUNTRY_CODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("valid regex"));
17+
18+
#[derive(Validate)]
19+
struct User {
20+
#[validate(regex = &COUNTRY_CODE_REGEX)]
21+
country_code: String,
22+
}
23+
```
24+
25+
## Types
26+
27+
### String
28+
29+
- [`str`](https://doc.rust-lang.org/std/primitive.str.html)
30+
- [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
31+
32+
Validate the string matches the specified regular expression.
33+
34+
## Options
35+
36+
### `expression`
37+
38+
The regular expression to match against.
39+
40+
The recommended approach for global regular expressions is to use a static [`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html).
41+
42+
```rust
43+
# extern crate fortifier;
44+
#
45+
use std::sync::LazyLock;
46+
47+
use fortifier::Validate;
48+
use regex::Regex;
49+
50+
static COUNTRY_CODE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[A-Z]{2}").expect("valid regex"));
51+
52+
#[derive(Validate)]
53+
struct User {
54+
#[validate(regex(expression = &COUNTRY_CODE_REGEX))]
55+
country_code: String,
56+
}
57+
```

book/src/validations/url.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ Validate a string is a specification-compliant URL using the [`url`](https://doc
77

88
```rust
99
# extern crate fortifier;
10-
# use fortifier::Validate;
1110
#
12-
##[derive(Validate)]
11+
use fortifier::Validate;
12+
13+
#[derive(Validate)]
1314
struct User {
1415
#[validate(url)]
1516
url: String

0 commit comments

Comments
 (0)