Added email regex validation and tests
This commit is contained in:
parent
f4b500eadb
commit
6cba2f93e1
|
@ -12,4 +12,5 @@ serde = {version = "1.0.159", features = ["derive"]}
|
||||||
serde_json = "1.0.95"
|
serde_json = "1.0.95"
|
||||||
reqwest = "0.11.16"
|
reqwest = "0.11.16"
|
||||||
url = "2.3.1"
|
url = "2.3.1"
|
||||||
chrono = "0.4.24"
|
chrono = "0.4.24"
|
||||||
|
regex = "1.7.3"
|
|
@ -2,6 +2,7 @@ pub mod schemas {
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
@ -80,6 +81,15 @@ pub mod schemas {
|
||||||
message: "Consent must be 'true' to register.".to_string(),
|
message: "Consent must be 'true' to register.".to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let regex =
|
||||||
|
regex::Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
|
||||||
|
if email.clone().is_some() && !regex.is_match(email.clone().unwrap().as_str()) {
|
||||||
|
return Err(RegisterSchemaError {
|
||||||
|
message: "The provided email address is in an invalid format.".to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(RegisterSchema {
|
return Ok(RegisterSchema {
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
|
@ -243,4 +253,47 @@ mod schemas_tests {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_email() {
|
||||||
|
assert_eq!(
|
||||||
|
RegisterSchema::new(
|
||||||
|
"Test".to_string(),
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
Some("p@p.p".to_string()),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
),
|
||||||
|
Err(RegisterSchemaError {
|
||||||
|
message: "The provided email address is in an invalid format.".to_string()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn valid_email() {
|
||||||
|
let reg = RegisterSchema::new(
|
||||||
|
"Test".to_string(),
|
||||||
|
None,
|
||||||
|
true,
|
||||||
|
Some("me@mail.xy".to_string()),
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
assert_ne!(
|
||||||
|
reg,
|
||||||
|
Err(RegisterSchemaError {
|
||||||
|
message: "The provided email address is in an invalid format.".to_string()
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue