Trait aragog::Validate

source ·
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§

source

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

source

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§

source

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§

source

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.

source

fn is_valid(&self) -> bool

Runs all validations and returns a false if they failed, on success true is returned

source

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 failure
  • field - Optional value, if field is Some<T> the function will succeed
  • errors - the mutable reference of the error message vector like provided in validations
Returns

true if field is Some<T> on failure, false is returned and errors stored a new message

source

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 failure
  • field - Optional value, if field is None the function will succeed
  • errors - the mutable reference of the error message vector like provided in validations
Returns

true if field is None on failure, false is returned and errors stored a new message

source

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 errors
  • str - the field value to validate
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • str - the field value to validate
  • max_len - The maximum length of str
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • str - the field value to validate
  • min_len - The minimum length of str
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • str - the field value to validate
  • len - The expected length of str
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • field_value - the Iter field value to validate
  • max_count - The maximum count of field_value elements
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • field_value - the Iter field value to validate
  • min_count - The minimum count of field_value elements
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • field_value - the Iter field value to validate
  • count - The expected exact count of field_value elements
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • str - the field value to validate
  • regex - The regular expression str must match
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • value - the field value to validate
  • min_value - The comparison value
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • value - the field value to validate
  • min_value - The comparison value
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • value - the field value to validate
  • max_value - The comparison value
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

source

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 errors
  • value - the field value to validate
  • max_value - The comparison value
  • errors - a mutable reference to a vector of String to be filled with error messages like provided in Validate::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

Object Safety§

This trait is not object safe.

Implementors§