Skip to content

Commit fa92a89

Browse files
feat: add basic struct validation
1 parent dac1647 commit fa92a89

22 files changed

Lines changed: 595 additions & 56 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/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
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mod r#enum;
2+
mod r#struct;
3+
mod r#union;
4+
5+
use proc_macro2::TokenStream;
6+
use quote::format_ident;
7+
use syn::{Data, DeriveInput, Result};
8+
9+
use crate::validate::{
10+
r#enum::validate_enum, r#struct::validate_struct_tokens, union::validate_union,
11+
};
12+
13+
pub fn validate_tokens(input: DeriveInput) -> Result<TokenStream> {
14+
let ident = input.ident;
15+
let error_ident = format_ident!("{ident}ValidationError");
16+
17+
match input.data {
18+
Data::Struct(data) => validate_struct_tokens(ident, error_ident, data),
19+
Data::Enum(data) => validate_enum(ident, error_ident, data),
20+
Data::Union(data) => validate_union(ident, error_ident, data),
21+
}
22+
}

0 commit comments

Comments
 (0)