diff --git a/Cargo.lock b/Cargo.lock index 302d467..7fb3845 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -571,6 +571,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "if_chain" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" + [[package]] name = "indexmap" version = "1.9.1" @@ -809,6 +815,30 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "proc-macro2" version = "1.0.40" @@ -1184,6 +1214,33 @@ dependencies = [ "serde_derive", "serde_json", "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]] diff --git a/Cargo.toml b/Cargo.toml index 4862ce5..4613f99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,6 @@ actix-web = "4" rusqlite = { version = "0.27.0", features = ["chrono"] } serde = { version = "1", features = ["derive"] } serde_json = "1" -validator = "0.15.0" +validator = { version = "0.15.0", features = ["derive"] } md5 = "0.7.0" chrono = "0.4.19" diff --git a/src/comment.rs b/src/comment.rs index 66b139e..f35bf04 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -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, + #[validate(email)] pub email: Option, pub text: String, } diff --git a/src/main.rs b/src/main.rs index 3721688..4d3eb66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -32,6 +33,9 @@ async fn post_comment(data: web::Data, 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() }