Skip to content

Commit

Permalink
fix(newsletter): validate email (#454)
Browse files Browse the repository at this point in the history
Returns HTTP 400 if the provided email is not an email address.
  • Loading branch information
caugner authored Mar 26, 2024
1 parent deb9d54 commit cd7c0f9
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/api/newsletter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use actix_web::{
use basket::{Basket, SubscribeOpts, YesNo};
use diesel::PgConnection;
use serde::{Deserialize, Serialize};
use validator::Validate;

use crate::{
api::error::ApiError,
Expand All @@ -31,8 +32,9 @@ struct Subscribed {
pub subscribed: bool,
}

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Validate)]
pub struct SubscriptionRequest {
#[validate(email(message = "must be an email address"))]
pub email: String,
}

Expand All @@ -53,6 +55,7 @@ pub async fn subscribe_anonymous_handler(
basket: Data<Option<Basket>>,
subscription_req: web::Json<SubscriptionRequest>,
) -> Result<HttpResponse, ApiError> {
subscription_req.validate()?;
if let Some(basket) = &**basket {
basket
.subscribe(
Expand Down

0 comments on commit cd7c0f9

Please sign in to comment.