-
Notifications
You must be signed in to change notification settings - Fork 1
이메일 유효성 검증 #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
이메일 유효성 검증 #75
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb4e963
feat: add subscriber email domain
reddevilmidzy bfcd4d5
refactor: move RepositoryURL to domain
reddevilmidzy 0067e1b
test: add test cases for repository url and subscriber email validation
reddevilmidzy 3a74e75
refactor: include email in API requests
reddevilmidzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| mod new_subscriber; | ||
| mod repository_url; | ||
| mod subscriber_email; | ||
|
|
||
| pub use new_subscriber::*; | ||
| pub use repository_url::*; | ||
| pub use subscriber_email::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| use crate::domain::repository_url::RepositoryURL; | ||
| use crate::domain::subscriber_email::SubscriberEmail; | ||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| pub struct NewSubscriber { | ||
| email: SubscriberEmail, | ||
| repository_url: RepositoryURL, | ||
| branch: Option<String>, // TODO: 브랜치 이름 제약 조건 확인하기 | ||
| } | ||
|
|
||
| impl NewSubscriber { | ||
| pub fn new( | ||
| email: SubscriberEmail, | ||
| repository_url: RepositoryURL, | ||
| branch: Option<String>, | ||
| ) -> Self { | ||
| Self { | ||
| email, | ||
| repository_url, | ||
| branch, | ||
| } | ||
| } | ||
|
|
||
| pub fn email(&self) -> &SubscriberEmail { | ||
| &self.email | ||
| } | ||
|
|
||
| pub fn repository_url(&self) -> &RepositoryURL { | ||
| &self.repository_url | ||
| } | ||
|
|
||
| pub fn branch(&self) -> Option<&String> { | ||
| self.branch.as_ref() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| const GITHUB_BASE_URL: &str = "https://github.com/"; | ||
| const GITHUB_URL_FORMAT: &str = "https://github.com/{owner}/{repo_name}"; | ||
|
|
||
| /// Represents a GitHub repository URL. | ||
| /// | ||
| /// This struct ensures that the URL is valid and follows the format | ||
| /// `https://github.com/{owner}/{repo_name}`. It includes validation logic | ||
| /// to enforce this format. | ||
| #[derive(Debug, Clone, Serialize)] | ||
| #[serde(transparent)] | ||
| pub struct RepositoryURL { | ||
| /// The URL of the repository. | ||
| url: String, | ||
| } | ||
|
|
||
| impl RepositoryURL { | ||
| /// Creates a new `RepositoryURL` instance. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `url` - The GitHub repository URL to validate and store. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// Returns `Ok(RepositoryURL)` if the URL is valid, or `Err(String)` if the URL is invalid. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use queensac::domain::RepositoryURL; | ||
| /// | ||
| /// let url = RepositoryURL::new("https://github.com/owner/repo").unwrap(); | ||
| /// ``` | ||
| pub fn new(url: impl Into<String>) -> Result<Self, String> { | ||
| let repo = RepositoryURL { url: url.into() }; | ||
| repo.validate()?; | ||
| Ok(repo) | ||
| } | ||
|
|
||
| /// Returns a reference to the repository URL. | ||
| pub fn url(&self) -> &str { | ||
| &self.url | ||
| } | ||
|
|
||
| fn validate(&self) -> Result<(), String> { | ||
| if !self.url.starts_with(GITHUB_BASE_URL) { | ||
| return Err(format!("URL must start with {}", GITHUB_BASE_URL)); | ||
| } | ||
| let parts: Vec<&str> = self | ||
| .url | ||
| .trim_start_matches(GITHUB_BASE_URL) | ||
| .split('/') | ||
| .collect(); | ||
| if parts.len() != 2 || parts[0].is_empty() || parts[1].is_empty() { | ||
| return Err(format!("URL must be in format {}", GITHUB_URL_FORMAT)); | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for RepositoryURL { | ||
| /// Custom deserialization logic for `RepositoryURL`. | ||
| /// | ||
| /// This implementation ensures that the URL is validated during | ||
| /// deserialization. If the URL is invalid, an error is returned. | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| let url = String::deserialize(deserializer)?; | ||
| RepositoryURL::new(url).map_err(serde::de::Error::custom) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_repository_url_creation() { | ||
| // Valid URLs | ||
| assert!(RepositoryURL::new("https://github.com/owner/repo").is_ok()); | ||
| assert!(RepositoryURL::new("https://github.com/rust-lang/rust").is_ok()); | ||
|
|
||
| // Invalid URLs | ||
| assert!(RepositoryURL::new("https://gitlab.com/owner/repo").is_err()); | ||
| assert!(RepositoryURL::new("https://github.com/").is_err()); | ||
| assert!(RepositoryURL::new("https://github.com/owner").is_err()); | ||
| assert!(RepositoryURL::new("https://github.com/owner/").is_err()); | ||
| assert!(RepositoryURL::new("http://github.com/owner/repo").is_err()); | ||
| assert!(RepositoryURL::new("https://github.com//repo").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_repository_url_deserialization() { | ||
| // Valid URLs | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"https://github.com/owner/repo\"").is_ok()); | ||
| assert!( | ||
| serde_json::from_str::<RepositoryURL>("\"https://github.com/rust-lang/rust\"").is_ok() | ||
| ); | ||
|
|
||
| // Invalid URLs | ||
| assert!( | ||
| serde_json::from_str::<RepositoryURL>("\"https://gitlab.com/owner/repo\"").is_err() | ||
| ); | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"https://github.com/\"").is_err()); | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"https://github.com/owner\"").is_err()); | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"https://github.com/owner/\"").is_err()); | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"http://github.com/owner/repo\"").is_err()); | ||
| assert!(serde_json::from_str::<RepositoryURL>("\"https://github.com//repo\"").is_err()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| pub struct SubscriberEmail(String); | ||
|
|
||
| impl SubscriberEmail { | ||
| /// Creates a new `SubscriberEmail` instance. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `email` - The email address to validate and store. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// Returns `Ok(SubscriberEmail)` if the email is valid, or `Err(String)` if the email is invalid. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use queensac::domain::SubscriberEmail; | ||
| /// | ||
| /// let email = SubscriberEmail::new("areyou@redddy.com").unwrap(); | ||
| /// ``` | ||
| pub fn new(email: impl Into<String>) -> Result<Self, String> { | ||
| let email = email.into(); | ||
| if validator::validate_email(&email) { | ||
| Ok(Self(email)) | ||
| } else { | ||
| Err(format!("{} is not a valid subscriber email.", email)) | ||
| } | ||
| } | ||
|
|
||
| /// Returns a reference to the email address. | ||
| pub fn as_str(&self) -> &str { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl AsRef<str> for SubscriberEmail { | ||
| fn as_ref(&self) -> &str { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::SubscriberEmail; | ||
| use fake::Fake; | ||
| use fake::faker::internet::en::SafeEmail; | ||
|
|
||
| #[test] | ||
| fn empty_string_is_rejected() { | ||
| assert!(SubscriberEmail::new("").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn email_missing_at_symbol_is_rejected() { | ||
| assert!(SubscriberEmail::new("redddy.com").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn email_missing_subject_is_rejected() { | ||
| assert!(SubscriberEmail::new("@redddy.com").is_err()); | ||
| } | ||
|
|
||
| #[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::new(valid_email.0).is_ok() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| pub mod domain; | ||
| pub mod git; | ||
| pub mod link; | ||
| pub mod schedule; | ||
|
|
||
| pub use domain::*; | ||
| pub use git::*; | ||
| pub use link::*; | ||
| pub use schedule::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.