|
1 | 1 | # Getting Started |
2 | 2 |
|
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. |
0 commit comments