Automatically trim values before saving

This commit is contained in:
Pierre HUBERT 2023-08-31 19:23:21 +02:00
parent 0899835fab
commit 2a69c89065
2 changed files with 43 additions and 1 deletions

View File

@ -103,8 +103,29 @@ fn check_opt_str_val(
Ok(())
}
fn trim_opt_val(val: &mut Option<String>) {
if let Some(s) = val {
*val = Some(s.trim().to_string());
}
if val.as_deref() == Some("") {
*val = None;
}
}
impl MemberRequest {
pub async fn to_member(self, member: &mut Member) -> anyhow::Result<()> {
pub async fn to_member(mut self, member: &mut Member) -> anyhow::Result<()> {
// Trim values before processing
trim_opt_val(&mut self.first_name);
trim_opt_val(&mut self.last_name);
trim_opt_val(&mut self.birth_last_name);
trim_opt_val(&mut self.email);
trim_opt_val(&mut self.country);
trim_opt_val(&mut self.address);
trim_opt_val(&mut self.city);
trim_opt_val(&mut self.note);
trim_opt_val(&mut self.phone);
let c = StaticConstraints::default();
check_opt_str_val(
&self.first_name,

View File

@ -6,6 +6,7 @@ diesel::table! {
family_id -> Int4,
wife -> Nullable<Int4>,
husband -> Nullable<Int4>,
#[max_length = 1]
state -> Nullable<Varchar>,
photo_id -> Nullable<Int4>,
time_create -> Int8,
@ -23,7 +24,9 @@ diesel::table! {
families (id) {
id -> Int4,
time_create -> Int8,
#[max_length = 30]
name -> Varchar,
#[max_length = 7]
invitation_code -> Varchar,
disable_couple_photos -> Bool,
}
@ -33,16 +36,26 @@ diesel::table! {
members (id) {
id -> Int4,
family_id -> Int4,
#[max_length = 30]
first_name -> Nullable<Varchar>,
#[max_length = 30]
last_name -> Nullable<Varchar>,
#[max_length = 30]
birth_last_name -> Nullable<Varchar>,
photo_id -> Nullable<Int4>,
#[max_length = 255]
email -> Nullable<Varchar>,
#[max_length = 30]
phone -> Nullable<Varchar>,
#[max_length = 155]
address -> Nullable<Varchar>,
#[max_length = 150]
city -> Nullable<Varchar>,
#[max_length = 12]
postal_code -> Nullable<Varchar>,
#[max_length = 2]
country -> Nullable<Varchar>,
#[max_length = 1]
sex -> Nullable<Varchar>,
time_create -> Int8,
time_update -> Int8,
@ -71,11 +84,15 @@ diesel::table! {
diesel::table! {
photos (id) {
id -> Int4,
#[max_length = 36]
file_id -> Varchar,
time_create -> Int8,
#[max_length = 150]
mime_type -> Varchar,
#[max_length = 130]
sha512 -> Varchar,
file_size -> Int4,
#[max_length = 130]
thumb_sha512 -> Varchar,
}
}
@ -83,12 +100,16 @@ diesel::table! {
diesel::table! {
users (id) {
id -> Int4,
#[max_length = 30]
name -> Varchar,
#[max_length = 255]
email -> Varchar,
password -> Nullable<Varchar>,
time_create -> Int8,
#[max_length = 150]
reset_password_token -> Nullable<Varchar>,
time_gen_reset_token -> Int8,
#[max_length = 150]
delete_account_token -> Nullable<Varchar>,
time_gen_delete_account_token -> Int8,
time_activate -> Int8,