Skip to content

Commit c5c3da8

Browse files
feat: add basic struct validation (#2)
1 parent dac1647 commit c5c3da8

23 files changed

Lines changed: 595 additions & 57 deletions

File tree

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["packages/*"]
2+
members = ["example", "packages/*"]
33
resolver = "2"
44

55
[workspace.package]

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Schema validation.
66

77
See [the Fortifier book](https://fortifier.rustforweb.org/) for documentation.
88

9+
## Credits
10+
11+
Inspired by [`validator`](https://github.com/Keats/validator).
12+
913
## License
1014

1115
This project is available under the [MIT license](LICENSE.md).

book/book.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[book]
22
authors = ["Daniëlle Huisman"]
33
language = "en"
4-
multilingual = false
54
src = "src"
65
title = "Fortifier"
76

book/src/introduction.md

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

33
Schema validation.
44

5+
## Credits
6+
7+
Inspired by [`validator`](https://github.com/Keats/validator).
8+
59
## License
610

711
This project is available under the [MIT license](https://github.com/RustForWeb/fortifier/blob/main/LICENSE.md).

example/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "fortifier-example"
3+
description = "Fortifier example."
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
version.workspace = true
10+
11+
[dependencies]
12+
fortifier.workspace = true

example/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::error::Error;
2+
3+
use fortifier::Validate;
4+
5+
#[derive(Validate)]
6+
struct CreateUser {
7+
#[validate(email)]
8+
email: String,
9+
10+
#[validate(length(min = 1, max = 256))]
11+
name: String,
12+
}
13+
14+
fn main() -> Result<(), Box<dyn Error>> {
15+
let data = CreateUser {
16+
email: "john@doe.com".to_owned(),
17+
name: "John Doe".to_owned(),
18+
};
19+
20+
data.validate_sync()?;
21+
22+
Ok(())
23+
}

packages/fortifier-macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ version.workspace = true
1212
proc-macro = true
1313

1414
[dependencies]
15+
convert_case = "0.9.0"
1516
proc-macro2 = "1.0.103"
1617
quote = "1.0.42"
1718
syn = "2.0.110"

packages/fortifier-macros/src/derive.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
mod derive;
1+
mod validate;
2+
mod validations;
23

34
use proc_macro::TokenStream;
4-
use syn::{DeriveInput, parse_macro_input};
5+
use syn::{DeriveInput, Error, parse_macro_input};
56

6-
use crate::derive::validate_tokens;
7+
use crate::validate::validate_tokens;
78

89
#[proc_macro_derive(Validate, attributes(validate))]
910
pub fn derive(input: TokenStream) -> TokenStream {
1011
let input = parse_macro_input!(input as DeriveInput);
1112

12-
validate_tokens(input).into()
13+
validate_tokens(input)
14+
.unwrap_or_else(Error::into_compile_error)
15+
.into()
1316
}

0 commit comments

Comments
 (0)