pub trait Validate {
const SIMPLE_EMAIL_REGEX: &'static str = r#"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"#;
const RFC_5322_EMAIL_REGEX: &'static str = r#"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"#;
Show 17 methods
// Required method
fn validations(&self, errors: &mut Vec<String>);
// Provided methods
fn validate(&self) -> Result<(), Error> { ... }
fn is_valid(&self) -> bool { ... }
fn validate_field_presence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_field_absence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_numeric_string(
field_path: &str,
str: &str,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_max_len(
field_path: &str,
str: &str,
max_len: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_min_len(
field_path: &str,
str: &str,
min_len: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_len(
field_path: &str,
str: &str,
len: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_max_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
max_count: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_min_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
min_count: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
count: usize,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_regex(
field_path: &str,
str: &str,
regex: &str,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_greater_than<T: PartialOrd + Display>(
field_path: &str,
value: T,
min_value: T,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_greater_or_equal_to<T: PartialOrd + Display>(
field_path: &str,
value: T,
min_value: T,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_lesser_than<T: PartialOrd + Display>(
field_path: &str,
value: T,
max_value: T,
errors: &mut Vec<String>
) -> bool { ... }
fn validate_lesser_or_equal_to<T: PartialOrd + Display>(
field_path: &str,
value: T,
max_value: T,
errors: &mut Vec<String>
) -> bool { ... }
}
Expand description
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());
}
}
}
Provided Associated Constants§
sourceconst SIMPLE_EMAIL_REGEX: &'static str = r#"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"#
const SIMPLE_EMAIL_REGEX: &'static str = r#"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}"#
A simple and fast regular expression to validate email formats
sourceconst RFC_5322_EMAIL_REGEX: &'static str = r#"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"#
const RFC_5322_EMAIL_REGEX: &'static str = r#"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"#
RFC 5322 compliant regular expression to validate email formats from emailregex.com.
It is significantly slower than the simple_email_regex
Required Methods§
sourcefn validations(&self, errors: &mut Vec<String>)
fn validations(&self, errors: &mut Vec<String>)
Runs all the defined validation on fields and fills the errors
string vector with custom error messages
Provided Methods§
sourcefn validate(&self) -> Result<(), Error>
fn validate(&self) -> Result<(), Error>
Validates the object field formats, logic or anything.
Calls the validations
method and return ()
on success.
Errors
Will render a complete Error
::ValidationError
on validation failure.
sourcefn is_valid(&self) -> bool
fn is_valid(&self) -> bool
Runs all validations and returns a false
if they failed, on success true
is returned
sourcefn validate_field_presence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
fn validate_field_presence<T>( 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
sourcefn validate_field_absence<T>(
field_name: &str,
field: &Option<T>,
errors: &mut Vec<String>
) -> bool
fn validate_field_absence<T>( 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
sourcefn validate_numeric_string(
field_path: &str,
str: &str,
errors: &mut Vec<String>
) -> bool
fn validate_numeric_string( 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
sourcefn validate_max_len(
field_path: &str,
str: &str,
max_len: usize,
errors: &mut Vec<String>
) -> bool
fn validate_max_len( 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
sourcefn validate_min_len(
field_path: &str,
str: &str,
min_len: usize,
errors: &mut Vec<String>
) -> bool
fn validate_min_len( 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
sourcefn validate_len(
field_path: &str,
str: &str,
len: usize,
errors: &mut Vec<String>
) -> bool
fn validate_len( 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
sourcefn validate_max_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
max_count: usize,
errors: &mut Vec<String>
) -> bool
fn validate_max_count<T>( field_path: &str, field_value: Iter<'_, T>, max_count: usize, errors: &mut Vec<String> ) -> bool
Validates the maximum number of elements in field_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 errorsfield_value
- theIter
field value to validatemax_count
- The maximum count offield_value
elementserrors
- 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
sourcefn validate_min_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
min_count: usize,
errors: &mut Vec<String>
) -> bool
fn validate_min_count<T>( field_path: &str, field_value: Iter<'_, T>, min_count: usize, errors: &mut Vec<String> ) -> bool
Validates the minimum number of elements in field_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 errorsfield_value
- theIter
field value to validatemin_count
- The minimum count offield_value
elementserrors
- 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
sourcefn validate_count<T>(
field_path: &str,
field_value: Iter<'_, T>,
count: usize,
errors: &mut Vec<String>
) -> bool
fn validate_count<T>( field_path: &str, field_value: Iter<'_, T>, count: usize, errors: &mut Vec<String> ) -> bool
Validates the exact number of elements in field_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 errorsfield_value
- theIter
field value to validatecount
- The expected exact count offield_value
elementserrors
- 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
sourcefn validate_regex(
field_path: &str,
str: &str,
regex: &str,
errors: &mut Vec<String>
) -> bool
fn validate_regex( 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
sourcefn validate_greater_than<T: PartialOrd + Display>(
field_path: &str,
value: T,
min_value: T,
errors: &mut Vec<String>
) -> bool
fn validate_greater_than<T: PartialOrd + Display>( field_path: &str, value: T, min_value: T, 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
sourcefn validate_greater_or_equal_to<T: PartialOrd + Display>(
field_path: &str,
value: T,
min_value: T,
errors: &mut Vec<String>
) -> bool
fn validate_greater_or_equal_to<T: PartialOrd + Display>( field_path: &str, value: T, min_value: T, 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
sourcefn validate_lesser_than<T: PartialOrd + Display>(
field_path: &str,
value: T,
max_value: T,
errors: &mut Vec<String>
) -> bool
fn validate_lesser_than<T: PartialOrd + Display>( field_path: &str, value: T, max_value: T, 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
sourcefn validate_lesser_or_equal_to<T: PartialOrd + Display>(
field_path: &str,
value: T,
max_value: T,
errors: &mut Vec<String>
) -> bool
fn validate_lesser_or_equal_to<T: PartialOrd + Display>( field_path: &str, value: T, max_value: T, 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