pub trait Addition {
    // Provided methods
    fn add_actions<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _client: &'life1 Client
    ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error>>> + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn add_validation<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _client: &'life1 Client
    ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, String>, Box<dyn Error>>> + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Methods for additional actions and additional validation. Hint: Add the Model parameter is_use_addition = true.

Example:

use async_trait::async_trait;
use mongodb::{bson::doc, Client};
use std::{collections::HashMap, error::Error};

#[Model(
    is_use_addition = true,
    ignore_fields = "confirm_password"
)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct ModelName {
    username: TextField
    password: PasswordField,
    confirm_password: PasswordField,
}

#[async_trait(?Send)]
impl Addition for ModelName {
    async fn add_actions(
        &self,
        _client: &Client,
    ) -> Result<HashMap<String, String>, Box<dyn Error>> {
        // Get clean data.
        let username = self.username.get().unwrap_or_default();
        self.username.set(username.to_uppercase());
        Ok(())
    }

    async fn add_validation(
        &self,
        _client: &Client,
    ) -> Result<HashMap<String, String>, Box<dyn Error>> {
        // Hint: error_map.insert("field_name", "Error message.")
        let mut error_map = HashMap::<String, String>::new();

        // Get clean data.
        let password = self.password.get().unwrap_or_default();
        let confirm_password = self.confirm_password.get().unwrap_or_default();

        // Fields validation.
        if (!password.is_empty() && !confirm_password.is_empty()) && password != confirm_password {
            error_map.insert("confirm_password".into(), t!("password_mismatch"));
        }

        Ok(error_map)
    }
}

Provided Methods§

source

fn add_actions<'life0, 'life1, 'async_trait>( &'life0 mut self, _client: &'life1 Client ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error>>> + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

It is intended for additional actions with fields. Hint: This method is execute first.

source

fn add_validation<'life0, 'life1, 'async_trait>( &'life0 self, _client: &'life1 Client ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, String>, Box<dyn Error>>> + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

It is supposed to be use to additional validation of fields. Hint: This method is execute second.

Implementors§