Skip to content

Commit

Permalink
feat: Email syntax validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Sep 6, 2023
1 parent 405029a commit 2f6ce9c
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 29 deletions.
143 changes: 131 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = "1.0.115"
config = { version = "0.13", default-features = false, features = ["yaml"] }
uuid = { version = "1", features = ["v4"] }
sqlx = { version = "0.6", default-features = false, features = ["runtime-actix-rustls", "macros", "postgres", "uuid", "chrono", "migrate", "offline"] }
sqlx = { version = "0.6", default-features = false, features = [
"runtime-actix-rustls",
"macros",
"postgres",
"uuid",
"chrono",
"migrate",
"offline",
] }
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
Expand All @@ -30,8 +38,10 @@ tracing-actix-web = "0.7"
serde-aux = "4"
unicode-segmentation = "1"
claim = "0.5.0"
validator = "0.16.1"

[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }


fake = "~2.3"
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"
7 changes: 7 additions & 0 deletions src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod new_subscriber;
pub mod subscriber_email;
pub mod subscriber_name;

pub use new_subscriber::NewSubscriber;
pub use subscriber_email::SubscriberEmail;
pub use subscriber_name::SubscriberName;
8 changes: 8 additions & 0 deletions src/domain/new_subscriber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::domain::subscriber_name::SubscriberName;

use super::SubscriberEmail;

pub struct NewSubscriber {
pub email: SubscriberEmail,
pub name: SubscriberName,
}
60 changes: 60 additions & 0 deletions src/domain/subscriber_email.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use validator::validate_email;

#[derive(Debug)]
pub struct SubscriberEmail(String);

impl SubscriberEmail {
pub fn parse(s: String) -> Result<SubscriberEmail, String> {
if validate_email(&s) {
Ok(Self(s))
} else {
Err(format!("{} is not a valid subscriber email", s))
}
}
}

impl AsRef<str> for SubscriberEmail {
fn as_ref(&self) -> &str {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::SubscriberEmail;
use claim::assert_err;
use fake::{faker::internet::en::SafeEmail, Fake};

#[test]
fn empty_string_is_rejected() {
let email = "".to_string();
assert_err!(SubscriberEmail::parse(email));
}

#[test]
fn email_missing_at_symbol_is_rejected() {
let email = "ursuladomain.com".to_string();
assert_err!(SubscriberEmail::parse(email));
}

#[test]
fn email_missing_subject_is_rejected() {
let email = "@domain.com".to_string();
assert_err!(SubscriberEmail::parse(email));
}

#[derive(Debug, Clone)]
struct ValidEmailFixture(pub String);

impl quickcheck::Arbitrary for ValidEmailFixture {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Self {
let email = SafeEmail().fake_with_rng(g);
Self(email)
}
}

#[quickcheck_macros::quickcheck]
fn valid_emails_are_parsed_successfully(valid_email: ValidEmailFixture) -> bool {
SubscriberEmail::parse(valid_email.0).is_ok()
}
}
Loading

0 comments on commit 2f6ce9c

Please sign in to comment.