This commit is contained in:
JMARyA 2024-07-17 09:40:59 +02:00
commit 5da65fb603
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 2232 additions and 0 deletions

40
src/model/valid.rs Normal file
View file

@ -0,0 +1,40 @@
/// This trait allows a `Model` to be validated.
pub trait Validate {
/// Validate the `Model`
async fn validate(&self) -> bool;
}
/// Validate a value and return `false` if validation fails.
#[macro_export]
macro_rules! validate {
($val:expr) => {
if !$val.validate().await {
return false;
}
};
}
/// 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.
///
/// # Example
/// ```
/// fn validate(&self) -> bool {
/// assert_reference_of!(self.owner, Person);
/// true
/// }
/// ```
#[macro_export]
macro_rules! assert_reference_of {
($var:expr, $($struct_name:ident),+) => {
let mut match_found = false;
$(
if $var.is_of_collection($struct_name::collection_name()) {
match_found = true;
}
)*
if !match_found {
return false;
}
};
}