better validation

This commit is contained in:
JMARyA 2024-07-19 10:38:39 +02:00
parent d473a32820
commit 3f251a224f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 41 additions and 20 deletions

View file

@ -1,19 +1,29 @@
/// This trait allows a `Model` to be validated.
pub trait Validate {
/// Validate the `Model`
fn validate(&self) -> impl std::future::Future<Output = bool> + Send;
fn validate(&self) -> impl std::future::Future<Output = Result<(), String>> + Send;
}
/// Validate a value and return `false` if validation fails.
/// Validate a value and return an Error if validation fails.
#[macro_export]
macro_rules! validate {
($val:expr) => {
if !$val.validate().await {
return false;
if let Err(err) = $val.validate().await {
return Err(format!("{}: {}", stringify!($val), err));
}
};
}
#[macro_export]
macro_rules! count_items {
// Base case: single item
($single:ident) => { 1 };
// Recursive case: count each item
($head:ident, $($tail:ident),*) => {
1 + mongod::count_items!($($tail),*)
};
}
/// This macro checks for the type of a reference and is useful for validation.
/// It will check all supplied types and return `false` if none are matching.
///
@ -34,7 +44,10 @@ macro_rules! assert_reference_of {
}
)*
if !match_found {
return false;
let possible_types: [&str; mongod::count_items!($($struct_name),*)] = [
$(stringify!($struct_name)),*
];
return Err(format!("{} is not of any type: {:?}", stringify!($var), possible_types));
}
};
}