Trait aragog::Validate[][src]

pub trait Validate {
    fn validations(&self, errors: &mut Vec<String>);

    fn validate(&self) -> Result<(), ServiceError> { ... }
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_regex(
        field_path: &str,
        str: &str,
        regex: &str,
        errors: &mut Vec<String>
    ) -> bool { ... }
fn validate_greater_than(
        field_path: &str,
        value: i32,
        min_value: i32,
        errors: &mut Vec<String>
    ) -> bool { ... }
fn validate_greater_or_equal_to(
        field_path: &str,
        value: i32,
        min_value: i32,
        errors: &mut Vec<String>
    ) -> bool { ... }
fn validate_lesser_than(
        field_path: &str,
        value: i32,
        max_value: i32,
        errors: &mut Vec<String>
    ) -> bool { ... }
fn validate_lesser_or_equal_to(
        field_path: &str,
        value: i32,
        max_value: i32,
        errors: &mut Vec<String>
    ) -> bool { ... }
fn validate_edge_fields(&self, errors: &mut Vec<String>)
    where
        Self: EdgeRecord
, { ... } }

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

Loading content...

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]

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

fn validate_field_absence<T>(
    field_name: &str,
    field: &Option<T>,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_numeric_string(
    field_path: &str,
    str: &str,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_max_len(
    field_path: &str,
    str: &str,
    max_len: usize,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_min_len(
    field_path: &str,
    str: &str,
    min_len: usize,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_len(
    field_path: &str,
    str: &str,
    len: usize,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_regex(
    field_path: &str,
    str: &str,
    regex: &str,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_greater_than(
    field_path: &str,
    value: i32,
    min_value: i32,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_greater_or_equal_to(
    field_path: &str,
    value: i32,
    min_value: i32,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_lesser_than(
    field_path: &str,
    value: i32,
    max_value: i32,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_lesser_or_equal_to(
    field_path: &str,
    value: i32,
    max_value: i32,
    errors: &mut Vec<String>
) -> bool
[src]

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

fn validate_edge_fields(&self, errors: &mut Vec<String>) where
    Self: EdgeRecord
[src]

Validation method for EdgeRecord to use on Validate implementation. Verifies that both _from and _to fields have correct format.

Loading content...

Implementors

Loading content...