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: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/generics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ repository.workspace = true
version.workspace = true

[dependencies]
fortifier.workspace = true
fortifier = { workspace = true, features = ["email-address", "serde"] }
serde = { workspace = true, features = ["derive"] }
uuid = { workspace = true, features = ["serde"] }

[lints]
workspace = true
38 changes: 38 additions & 0 deletions examples/generics/src/action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::fmt::Debug;

use fortifier::{Validate, ValidateWithContext};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
#[serde(untagged, rename_all = "camelCase")]
pub enum CreateOrUpdate<C, U>
where
C: Debug + Send + Sync + ValidateWithContext<Context = ()>,
U: Debug + Send + Sync + ValidateWithContext<Context = ()>,
{
Update {
#[validate(skip)]
id: Uuid,
#[serde(flatten)]
data: U,
},
Create {
#[serde(flatten)]
data: C,
},
}

pub type CreateOrUpdateUser = CreateOrUpdate<CreateUser, UpdateUser>;

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct CreateUser {
#[validate(email_address)]
pub email: String,
}

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct UpdateUser {
#[validate(email_address)]
pub email: Option<String>,
}
40 changes: 40 additions & 0 deletions examples/generics/src/bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fmt::Debug;

use fortifier::Validate;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Validate)]
#[validate(custom(function = validate_min_max, error = BoundsMinMaxError<T>))]
pub struct Bounds<T>
where
T: Clone + Debug + PartialEq + PartialOrd + Send + Sync,
{
pub min: Option<T>,
pub max: Option<T>,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct BoundsMinMaxError<T>
where
T: Debug + PartialEq,
{
pub min: T,
pub max: T,
}

fn validate_min_max<T>(value: &Bounds<T>) -> Result<(), BoundsMinMaxError<T>>
where
T: Clone + Debug + PartialEq + PartialOrd + Send + Sync,
{
if let Some(min) = &value.min
&& let Some(max) = &value.max
&& min > max
{
Err(BoundsMinMaxError {
min: min.clone(),
max: max.clone(),
})
} else {
Ok(())
}
}
58 changes: 23 additions & 35 deletions examples/generics/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
use std::fmt::Debug;
mod action;
mod bounds;

use fortifier::{Validate, ValidationErrors};
use uuid::Uuid;

#[derive(Validate)]
#[validate(custom(function = validate_min_max, error = BoundsMinMaxError<T>))]
struct Bounds<T>
where
T: Clone + Debug + PartialEq + PartialOrd,
{
min: Option<T>,
max: Option<T>,
}
use crate::{
action::{CreateOrUpdateUser, CreateUser, UpdateUser},
bounds::{Bounds, BoundsMinMaxError, BoundsValidationError},
};

#[derive(Debug, PartialEq)]
struct BoundsMinMaxError<T>
where
T: Debug + PartialEq,
{
min: T,
max: T,
}
fn main() {
let data = CreateOrUpdateUser::Create {
data: CreateUser {
email: "amy.pond@example.com".to_owned(),
},
};

fn validate_min_max<T>(value: &Bounds<T>) -> Result<(), BoundsMinMaxError<T>>
where
T: Clone + Debug + PartialEq + PartialOrd,
{
if let Some(min) = &value.min
&& let Some(max) = &value.max
&& min > max
{
Err(BoundsMinMaxError {
min: min.clone(),
max: max.clone(),
})
} else {
Ok(())
}
}
assert!(data.validate_sync().is_ok());

let data = CreateOrUpdateUser::Update {
id: Uuid::nil(),
data: UpdateUser {
email: Some("amy.pond@example.com".to_owned()),
},
};

assert!(data.validate_sync().is_ok());

fn main() {
let bounds = Bounds {
min: Some(1),
max: Some(10),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::fmt::Debug;

use fortifier::{Validate, ValidateWithContext};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
#[serde(untagged, rename_all = "camelCase")]
enum CreateOrUpdate<C, U>
where
C: Debug + Send + Sync + ValidateWithContext<Context = ()>,
U: Debug + Send + Sync + ValidateWithContext<Context = ()>,
{
Update {
#[validate(skip)]
id: Uuid,
#[serde(flatten)]
data: U,
},
Create {
#[serde(flatten)]
data: C,
},
}

type CreateOrUpdateUser = CreateOrUpdate<CreateUser, UpdateUser>;

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
struct CreateUser {
#[validate(email_address)]
email: String,
}

#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
struct UpdateUser {
#[validate(email_address)]
email: Option<String>,
}

fn main() {
let data = CreateOrUpdateUser::Create {
data: CreateUser {
email: "amy.pond@example.com".to_owned(),
},
};

assert!(data.validate_sync().is_ok());

let data = CreateOrUpdateUser::Update {
id: Uuid::nil(),
data: UpdateUser {
email: Some("amy.pond@example.com".to_owned()),
},
};

assert!(data.validate_sync().is_ok());
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
#[validate(custom(function = validate_min_max, error = BoundsMinMaxError<T>))]
struct Bounds<T>
where
T: Clone + Debug + PartialEq + PartialOrd,
T: Clone + Debug + PartialEq + PartialOrd + Send + Sync,
{
min: Option<T>,
max: Option<T>,
Expand All @@ -16,15 +16,15 @@ where
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct BoundsMinMaxError<T>
where
T: Debug + PartialEq,
T: Debug + PartialEq + Send + Sync,
{
min: T,
max: T,
}

fn validate_min_max<T>(value: &Bounds<T>) -> Result<(), BoundsMinMaxError<T>>
where
T: Clone + Debug + PartialEq + PartialOrd,
T: Clone + Debug + PartialEq + PartialOrd + Send + Sync,
{
if let Some(min) = &value.min
&& let Some(max) = &value.max
Expand Down
Loading