init
This commit is contained in:
commit
5da65fb603
10 changed files with 2232 additions and 0 deletions
40
src/model/valid.rs
Normal file
40
src/model/valid.rs
Normal 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;
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue