Add email validation

main
Elnu 2 years ago
parent 0785bc2fe9
commit 2699a01c54

57
Cargo.lock generated

@ -571,6 +571,12 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "if_chain"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.1" version = "1.9.1"
@ -809,6 +815,30 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.40" version = "1.0.40"
@ -1184,6 +1214,33 @@ dependencies = [
"serde_derive", "serde_derive",
"serde_json", "serde_json",
"url", "url",
"validator_derive",
]
[[package]]
name = "validator_derive"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea7ed5e8cf2b6bdd64a6c4ce851da25388a89327b17b88424ceced6bd5017923"
dependencies = [
"if_chain",
"lazy_static",
"proc-macro-error",
"proc-macro2",
"quote",
"regex",
"syn",
"validator_types",
]
[[package]]
name = "validator_types"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ddf34293296847abfc1493b15c6e2f5d3cd19f57ad7d22673bf4c6278da329"
dependencies = [
"proc-macro2",
"syn",
] ]
[[package]] [[package]]

@ -10,6 +10,6 @@ actix-web = "4"
rusqlite = { version = "0.27.0", features = ["chrono"] } rusqlite = { version = "0.27.0", features = ["chrono"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
validator = "0.15.0" validator = { version = "0.15.0", features = ["derive"] }
md5 = "0.7.0" md5 = "0.7.0"
chrono = "0.4.19" chrono = "0.4.19"

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

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

Loading…
Cancel
Save