Skip to content

Commit 44a4ff2

Browse files
docs(book): add getting started and installation (#24)
1 parent 945f301 commit 44a4ff2

13 files changed

Lines changed: 166 additions & 6 deletions

File tree

.github/workflows/website.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
run: cargo clean -p fortifier
3636

3737
- name: Build Fortifier
38-
run: cargo build -p fortifier --all-features
38+
run: cargo build -p fortifier --features all-validations
3939

4040
- name: Run tests
4141
run: mdbook test -L ../target/debug/deps

book/src/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
- [Introduction](./introduction.md)
44
- [Getting Started](./getting-started.md)
5+
- [Installation](./installation.md)
6+
- [Validate](./validate/README.md)
7+
- [Enum](./validate/enum.md)
8+
- [Struct](./validate/struct.md)
59
- [Validations](./validations/README.md)
610
- [Email](./validations/email.md)
711
- [Length](./validations/length.md)

book/src/getting-started.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,110 @@
11
# Getting Started
22

3-
TODO
3+
## Install
4+
5+
Add Fortifier to your project:
6+
7+
```shell
8+
cargo add fortifier --features email
9+
```
10+
11+
See [Installation](installation.md) for more details.
12+
13+
## Data structure
14+
15+
Define a data structure:
16+
17+
```rust
18+
struct CreateUser {
19+
email_address: String,
20+
name: String,
21+
}
22+
```
23+
24+
## Derive
25+
26+
Derive the `Validate` trait:
27+
28+
```rust
29+
# extern crate fortifier;
30+
use fortifier::Validate;
31+
32+
#[derive(Validate)]
33+
struct CreateUser {
34+
email_address: String,
35+
name: String,
36+
}
37+
```
38+
39+
## Validations
40+
41+
Define validations:
42+
43+
```rust
44+
# extern crate fortifier;
45+
use fortifier::Validate;
46+
47+
#[derive(Validate)]
48+
struct CreateUser {
49+
#[validate(email)]
50+
email_address: String,
51+
52+
#[validate(length(min = 1, max = 256))]
53+
name: String,
54+
}
55+
```
56+
57+
## Validate
58+
59+
Fortifier supports both synchronous and asynchronous validation. This example will only use synchronous validation.
60+
61+
Call the `validate_sync` method on the data structure:
62+
63+
```rust
64+
# extern crate fortifier;
65+
use fortifier::{EmailError, LengthError, Validate, ValidationErrors};
66+
67+
#[derive(Validate)]
68+
struct CreateUser {
69+
#[validate(email)]
70+
email_address: String,
71+
72+
#[validate(length(min = 1, max = 256))]
73+
name: String,
74+
}
75+
76+
fn main() {
77+
let data = CreateUser {
78+
email_address: "amy.pond@example.com".to_string(),
79+
name: "Amy Pond".to_string(),
80+
};
81+
82+
assert_eq!(data.validate_sync(), Ok(()));
83+
84+
let data = CreateUser {
85+
email_address: "invalid".to_string(),
86+
name: "".to_string(),
87+
};
88+
89+
assert_eq!(
90+
data.validate_sync(),
91+
Err(ValidationErrors::from_iter([
92+
CreateUserValidationError::EmailAddress(
93+
EmailError::MissingSeparator {},
94+
),
95+
CreateUserValidationError::Name(
96+
LengthError::Min {
97+
min: 1,
98+
length: 0,
99+
}
100+
),
101+
])),
102+
);
103+
}
104+
```
105+
106+
## Next Steps
107+
108+
- [Installation](./installation.md) - Lists all available features.
109+
- [Validate](./validate/README.md) - Describes how to use the `Validate` derive macro.
110+
- [Validations](./validations/README.md) - Explains all available validations and their options.

book/src/installation.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Installation
2+
3+
```shell
4+
cargo add fortifier
5+
```
6+
7+
- [View on crates.io](https://crates.io/crates/fortifier)
8+
- [View on docs.rs](https://docs.rs/fortifier/latest/fortifier/)
9+
- [View source](https://github.com/RustForWeb/fortifier/tree/main/packages/fortifier)
10+
11+
## Features
12+
13+
### General
14+
15+
- `macros` (default) - Derive macro for the `Validate` trait ([`fortifier-macros`](https://docs.rs/fortifier-macros/latest/fortifier_macros/)).
16+
- `message` - Add a human-readable `message` field to validation errors.
17+
18+
### Types
19+
20+
- `indexmap` - Support for the `IndexMap` and `IndexSet` types from the [`indexmap`](https://docs.rs/indexmap/latest/indexmap/) crate.
21+
22+
### Validations
23+
24+
- `all-validations` - Enable all features below.
25+
- `email` - Email address validation using the [`email_address`](https://docs.rs/email_address/latest/email_address/) crate.
26+
- `regex` - Regular expression validation using the [`regex`](https://docs.rs/regex/latest/regex/) crate.
27+
- `url` - URL validation using the [`url`](https://docs.rs/url/latest/url/) crate.
28+
29+
### Integrations
30+
31+
- `serde` - Support for the [`serde`](https://docs.rs/serde/latest/serde/) crate. Derives the `Deserialize` and `Serialize` traits for validation errors.
32+
- `utoipa` - Support for the [`utoipa`](https://docs.rs/utoipa/latest/utoipa/) crate. Derives the `ToSchema` trait for validation errors.

book/src/introduction.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Schema validation.
44

5+
- Synchronous and asynchronous validation
6+
- Enums and structs
7+
- Typed errors
8+
- Email, regex, URL and more
9+
- Support for Serde and Utoipa
10+
511
## Credits
612

713
Inspired by [`validator`](https://github.com/Keats/validator).

book/src/validate/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Validate
2+
3+
- [Enum](./enum.md)
4+
- [Struct](./struct.md)

book/src/validate/enum.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Enum
2+
3+
TODO

book/src/validate/struct.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Struct
2+
3+
TODO

examples/basic/src/user.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct CreateUser {
2525
pub locales: Vec<String>,
2626
}
2727

28-
#[derive(Debug)]
28+
#[derive(Debug, PartialEq)]
2929
pub struct OneLocaleRequiredError;
3030

3131
fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {

packages/fortifier-macros/src/validate/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a> ValidateEnum<'a> {
6161
error_ident,
6262
quote! {
6363
#[allow(dead_code)]
64-
#[derive(Debug)]
64+
#[derive(Debug, PartialEq)]
6565
#attributes
6666
#visibility enum #error_ident {
6767
#( #error_variant_idents(#error_variant_types) ),*

0 commit comments

Comments
 (0)