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 .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: cargo clean -p fortifier

- name: Build Fortifier
run: cargo build -p fortifier --all-features
run: cargo build -p fortifier --features all-validations

- name: Run tests
run: mdbook test -L ../target/debug/deps
Expand Down
4 changes: 4 additions & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

- [Introduction](./introduction.md)
- [Getting Started](./getting-started.md)
- [Installation](./installation.md)
- [Validate](./validate/README.md)
- [Enum](./validate/enum.md)
- [Struct](./validate/struct.md)
- [Validations](./validations/README.md)
- [Email](./validations/email.md)
- [Length](./validations/length.md)
Expand Down
109 changes: 108 additions & 1 deletion book/src/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,110 @@
# Getting Started

TODO
## Install

Add Fortifier to your project:

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

See [Installation](installation.md) for more details.

## Data structure

Define a data structure:

```rust
struct CreateUser {
email_address: String,
name: String,
}
```

## Derive

Derive the `Validate` trait:

```rust
# extern crate fortifier;
use fortifier::Validate;

#[derive(Validate)]
struct CreateUser {
email_address: String,
name: String,
}
```

## Validations

Define validations:

```rust
# extern crate fortifier;
use fortifier::Validate;

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

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

## Validate

Fortifier supports both synchronous and asynchronous validation. This example will only use synchronous validation.

Call the `validate_sync` method on the data structure:

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

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

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

fn main() {
let data = CreateUser {
email_address: "amy.pond@example.com".to_string(),
name: "Amy Pond".to_string(),
};

assert_eq!(data.validate_sync(), Ok(()));

let data = CreateUser {
email_address: "invalid".to_string(),
name: "".to_string(),
};

assert_eq!(
data.validate_sync(),
Err(ValidationErrors::from_iter([
CreateUserValidationError::EmailAddress(
EmailError::MissingSeparator {},
),
CreateUserValidationError::Name(
LengthError::Min {
min: 1,
length: 0,
}
),
])),
);
}
```

## Next Steps

- [Installation](./installation.md) - Lists all available features.
- [Validate](./validate/README.md) - Describes how to use the `Validate` derive macro.
- [Validations](./validations/README.md) - Explains all available validations and their options.
32 changes: 32 additions & 0 deletions book/src/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Installation

```shell
cargo add fortifier
```

- [View on crates.io](https://crates.io/crates/fortifier)
- [View on docs.rs](https://docs.rs/fortifier/latest/fortifier/)
- [View source](https://github.com/RustForWeb/fortifier/tree/main/packages/fortifier)

## Features

### General

- `macros` (default) - Derive macro for the `Validate` trait ([`fortifier-macros`](https://docs.rs/fortifier-macros/latest/fortifier_macros/)).
- `message` - Add a human-readable `message` field to validation errors.

### Types

- `indexmap` - Support for the `IndexMap` and `IndexSet` types from the [`indexmap`](https://docs.rs/indexmap/latest/indexmap/) crate.

### Validations

- `all-validations` - Enable all features below.
- `email` - Email address validation using the [`email_address`](https://docs.rs/email_address/latest/email_address/) 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.

### Integrations

- `serde` - Support for the [`serde`](https://docs.rs/serde/latest/serde/) crate. Derives the `Deserialize` and `Serialize` traits for validation errors.
- `utoipa` - Support for the [`utoipa`](https://docs.rs/utoipa/latest/utoipa/) crate. Derives the `ToSchema` trait for validation errors.
6 changes: 6 additions & 0 deletions book/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Schema validation.

- Synchronous and asynchronous validation
- Enums and structs
- Typed errors
- Email, regex, URL and more
- Support for Serde and Utoipa

## Credits

Inspired by [`validator`](https://github.com/Keats/validator).
Expand Down
4 changes: 4 additions & 0 deletions book/src/validate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Validate

- [Enum](./enum.md)
- [Struct](./struct.md)
3 changes: 3 additions & 0 deletions book/src/validate/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Enum

TODO
3 changes: 3 additions & 0 deletions book/src/validate/struct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Struct

TODO
2 changes: 1 addition & 1 deletion examples/basic/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct CreateUser {
pub locales: Vec<String>,
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct OneLocaleRequiredError;

fn validate_one_locale_required(locales: &[String]) -> Result<(), OneLocaleRequiredError> {
Expand Down
2 changes: 1 addition & 1 deletion packages/fortifier-macros/src/validate/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> ValidateEnum<'a> {
error_ident,
quote! {
#[allow(dead_code)]
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#attributes
#visibility enum #error_ident {
#( #error_variant_idents(#error_variant_types) ),*
Expand Down
2 changes: 1 addition & 1 deletion packages/fortifier-macros/src/validate/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<'a> ValidateField<'a> {
(
ident.to_token_stream(),
Some(quote! {
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#attributes
#visibility enum #ident {
#( #variant_ident(#variant_type) ),*
Expand Down
2 changes: 1 addition & 1 deletion packages/fortifier-macros/src/validate/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn error_type<'a>(
error_ident.to_token_stream(),
quote! {
#[allow(dead_code)]
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#attributes
#visibility enum #error_ident {
#( #error_field_idents(#error_field_types) ),*
Expand Down
1 change: 1 addition & 0 deletions packages/fortifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true

[features]
default = ["macros"]
all-validations = ["email", "regex", "url"]
email = ["dep:email_address"]
indexmap = ["dep:indexmap"]
macros = ["dep:fortifier-macros"]
Expand Down