mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
fix(utils): fix bug in email validation (#1180)
This commit is contained in:
@ -11,9 +11,13 @@ use diesel::{
|
|||||||
serialize::{Output, ToSql},
|
serialize::{Output, ToSql},
|
||||||
sql_types, AsExpression,
|
sql_types, AsExpression,
|
||||||
};
|
};
|
||||||
|
use error_stack::{IntoReport, ResultExt};
|
||||||
use masking::{Secret, Strategy, WithType};
|
use masking::{Secret, Strategy, WithType};
|
||||||
|
|
||||||
use crate::{errors::ValidationError, validation::validate_email};
|
use crate::{
|
||||||
|
errors::{self, ValidationError},
|
||||||
|
validation::validate_email,
|
||||||
|
};
|
||||||
|
|
||||||
/// A string constant representing a redacted or masked value.
|
/// A string constant representing a redacted or masked value.
|
||||||
pub const REDACTED: &str = "Redacted";
|
pub const REDACTED: &str = "Redacted";
|
||||||
@ -101,8 +105,17 @@ where
|
|||||||
serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, Default, AsExpression,
|
serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq, Default, AsExpression,
|
||||||
)]
|
)]
|
||||||
#[diesel(sql_type = diesel::sql_types::Text)]
|
#[diesel(sql_type = diesel::sql_types::Text)]
|
||||||
|
#[serde(try_from = "String")]
|
||||||
pub struct Email(Secret<String, EmailStrategy>);
|
pub struct Email(Secret<String, EmailStrategy>);
|
||||||
|
|
||||||
|
impl TryFrom<String> for Email {
|
||||||
|
type Error = error_stack::Report<errors::ParsingError>;
|
||||||
|
|
||||||
|
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||||
|
Self::from_str(&value).change_context(errors::ParsingError::EmailParsingError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Secret<String, EmailStrategy>> for Email {
|
impl From<Secret<String, EmailStrategy>> for Email {
|
||||||
fn from(value: Secret<String, EmailStrategy>) -> Self {
|
fn from(value: Secret<String, EmailStrategy>) -> Self {
|
||||||
Self(value)
|
Self(value)
|
||||||
@ -157,8 +170,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Email {
|
impl FromStr for Email {
|
||||||
type Err = ValidationError;
|
type Err = error_stack::Report<ValidationError>;
|
||||||
|
|
||||||
fn from_str(email: &str) -> Result<Self, Self::Err> {
|
fn from_str(email: &str) -> Result<Self, Self::Err> {
|
||||||
if email.eq(REDACTED) {
|
if email.eq(REDACTED) {
|
||||||
return Ok(Self(Secret::new(email.to_string())));
|
return Ok(Self(Secret::new(email.to_string())));
|
||||||
@ -170,7 +182,8 @@ impl FromStr for Email {
|
|||||||
}
|
}
|
||||||
Err(_) => Err(ValidationError::InvalidValue {
|
Err(_) => Err(ValidationError::InvalidValue {
|
||||||
message: "Invalid email address format".into(),
|
message: "Invalid email address format".into(),
|
||||||
}),
|
})
|
||||||
|
.into_report(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -247,15 +260,13 @@ mod pii_masking_strategy_tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_valid_newtype_email() {
|
fn test_valid_newtype_email() {
|
||||||
let email_check: Result<Email, crate::errors::ValidationError> =
|
let email_check = Email::from_str("example@abc.com");
|
||||||
Email::from_str("example@abc.com");
|
|
||||||
assert!(email_check.is_ok());
|
assert!(email_check.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invalid_newtype_email() {
|
fn test_invalid_newtype_email() {
|
||||||
let email_check: Result<Email, crate::errors::ValidationError> =
|
let email_check = Email::from_str("example@abc@com");
|
||||||
Email::from_str("example@abc@com");
|
|
||||||
assert!(email_check.is_err());
|
assert!(email_check.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user