Trait aragog::Validate [−][src]
The Validate
trait of the Aragog library.
This trait provides the possibility to validate an instance or its fields formats or logic. Its main use
it to validate a new or updated Record
model instance before saving it.
Example
#[derive(Record, Clone, Deserialize, Serialize)] pub struct User { pub name: String, pub age: u32, pub email: String, pub job: Option<String>, pub phone: String, } impl Validate for User { fn validations(&self, errors: &mut Vec<String>) { if self.age > 18 { Self::validate_field_presence("job", &self.job, errors); } Self::validate_min_len("name", &self.name, 6, errors); Self::validate_len("phone", &self.phone, 10, errors); Self::validate_numeric_string("phone", &self.phone, errors); if self.age < 13 { errors.push("You are too young to use this website".to_string()); } } }
Required methods
fn validations(&self, errors: &mut Vec<String>)
[src]
Runs all the defined validation on fields and fills the errors
string vector with custom error messages
Provided methods
fn validate(&self) -> Result<(), ServiceError>
[src]
Validates the object field formats, logic or anything. Calls the validations
method
and will render a complete ServiceError
::ValidationError
on validation failure.
On success returns ()
fn is_valid(&self) -> bool
[src]
Runs all validations and returns a false
if they failed, on success true
is returned
fn validate_field_presence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
[src]
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
Helper function to simply check the presence of a field. This function is usually used inside the
validations
method since it will fill the errors
with a message if the field
is missing.
Arguments
field_name
- The string slice name of the field, will be used in the error message on failurefield
- Optional value, iffield
isSome<T>
the function will succeederrors
- the mutable reference of the error message vector like provided invalidations
Returns
true
if field
is Some<T>
on failure, false
is returned and errors
stored a new message
fn validate_field_absence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
[src]
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
Helper function to simply check the absence of a field. This function is usually used inside the
validations
method since it will fill the errors
with a message if the field
is missing.
Arguments
field_name
- The string slice name of the field, will be used in the error message on failurefield
- Optional value, iffield
isNone
the function will succeederrors
- the mutable reference of the error message vector like provided invalidations
Returns
true
if field
is None
on failure, false
is returned and errors
stored a new message
fn validate_numeric_string(
field_path: &str,
str: &str,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
str: &str,
errors: &mut Vec<String>
) -> bool
Validates that str
is numeric. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsstr
- the field value to validateerrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_max_len(
field_path: &str,
str: &str,
max_len: usize,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
str: &str,
max_len: usize,
errors: &mut Vec<String>
) -> bool
Validates that str
is not longer than expected. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsstr
- the field value to validatemax_len
- The maximum length ofstr
errors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_min_len(
field_path: &str,
str: &str,
min_len: usize,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
str: &str,
min_len: usize,
errors: &mut Vec<String>
) -> bool
Validates that str
is not shorter than expected. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsstr
- the field value to validatemin_len
- The minimum length ofstr
errors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_len(
field_path: &str,
str: &str,
len: usize,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
str: &str,
len: usize,
errors: &mut Vec<String>
) -> bool
Validates that str
has the exact expected length. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsstr
- the field value to validatelen
- The expected length ofstr
errors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_regex(
field_path: &str,
str: &str,
regex: &str,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
str: &str,
regex: &str,
errors: &mut Vec<String>
) -> bool
Validates that str
matches a regexp. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsstr
- the field value to validateregex
- The regular expressionstr
must matcherrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_greater_than(
field_path: &str,
value: i32,
min_value: i32,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
value: i32,
min_value: i32,
errors: &mut Vec<String>
) -> bool
Validates that value
is greater than min_value
. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsvalue
- the field value to validatemin_value
- The comparison valueerrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_greater_or_equal_to(
field_path: &str,
value: i32,
min_value: i32,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
value: i32,
min_value: i32,
errors: &mut Vec<String>
) -> bool
Validates that value
is greater or equal to min_value
. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsvalue
- the field value to validatemin_value
- The comparison valueerrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_lesser_than(
field_path: &str,
value: i32,
max_value: i32,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
value: i32,
max_value: i32,
errors: &mut Vec<String>
) -> bool
Validates that value
is lower than max_value
. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsvalue
- the field value to validatemax_value
- The comparison valueerrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_lesser_or_equal_to(
field_path: &str,
value: i32,
max_value: i32,
errors: &mut Vec<String>
) -> bool
[src]
field_path: &str,
value: i32,
max_value: i32,
errors: &mut Vec<String>
) -> bool
Validates that value
is lower or equal to max_value
. Usually used as a helper function for implementations of
Validate
trait.
Arguments
field_path
- the string slice representing the field name or path for clear errorsvalue
- the field value to validatemax_value
- The comparison valueerrors
- a mutable reference to a vector of String to be filled with error messages like provided inValidate
::validations
Returns
On success true
is returned and errors
stays unchanged. On failure false
is returned and a
new error message is added to errors
fn validate_edge_fields(&self, errors: &mut Vec<String>) where
Self: EdgeRecord,
[src]
Self: EdgeRecord,
Validation method for EdgeRecord
to use on Validate
implementation.
Verifies that both _from
and _to
fields have correct format.