Add email validation

This commit is contained in:
Elnu 2022-07-08 22:04:15 -07:00
parent 0785bc2fe9
commit 2699a01c54
4 changed files with 65 additions and 2 deletions

View file

@ -1,5 +1,6 @@
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use validator::Validate;
// Master comment type that is stored in database
pub struct Comment {
@ -33,9 +34,10 @@ pub struct CommentSend {
}
// Comment type received containing new comment data
#[derive(Deserialize)]
#[derive(Deserialize, Validate)]
pub struct CommentReceive {
pub author: Option<String>,
#[validate(email)]
pub email: Option<String>,
pub text: String,
}

View file

@ -6,6 +6,7 @@ pub use database::Database;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
use std::sync::Mutex;
use validator::Validate;
struct AppState {
db: Mutex<Database>,
@ -32,6 +33,9 @@ async fn post_comment(data: web::Data<AppState>, bytes: web::Bytes) -> impl Resp
Ok(comment) => comment,
Err(_) => return HttpResponse::BadRequest(),
};
if comment.validate().is_err() {
return HttpResponse::BadRequest();
}
db.create_comment(&comment.to_master()).unwrap();
HttpResponse::Ok()
}