From c81bb9ec2572e10da9c4e9ee6c87ea8f7c65a2d0 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 29 Jun 2018 14:56:00 +0200 Subject: [PATCH] Make forms validatable --- src/routes/blogs.rs | 13 ++++++++++++- src/routes/comments.rs | 5 +++-- src/routes/posts.rs | 13 ++++++++++++- src/routes/session.rs | 5 ++++- src/routes/user.rs | 16 +++++++++++++++- 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 1e627439..0dc44838 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -5,6 +5,7 @@ use rocket::{ }; use rocket_contrib::Template; use serde_json; +use validator::{Validate, ValidationError}; use plume_common::activity_pub::ActivityStream; use plume_common::utils; @@ -49,11 +50,21 @@ fn new_auth() -> Flash{ utils::requires_login("You need to be logged in order to create a new blog", uri!(new)) } -#[derive(FromForm)] +#[derive(FromForm, Validate)] struct NewBlogForm { + #[validate(custom = "valid_slug")] pub title: String } +fn valid_slug(title: &str) -> Result<(), ValidationError> { + let slug = utils::make_actor_id(title.to_string()); + if slug.len() == 0 { + Err(ValidationError::new("empty_slug")) + } else { + Ok(()) + } +} + #[post("/blogs/new", data = "")] fn create(conn: DbConn, data: LenientForm, user: User) -> Redirect { let form = data.get(); diff --git a/src/routes/comments.rs b/src/routes/comments.rs index ee5112bb..78fd2852 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -3,6 +3,7 @@ use rocket::{ response::Redirect }; use serde_json; +use validator::Validate; use plume_common::activity_pub::broadcast; use plume_models::{ @@ -15,9 +16,10 @@ use plume_models::{ }; use inbox::Inbox; -#[derive(FromForm, Debug)] +#[derive(FromForm, Debug, Validate)] struct NewCommentForm { pub responding_to: Option, + #[validate(length(min = "1"))] pub content: String } @@ -26,7 +28,6 @@ fn create(blog_name: String, slug: String, data: LenientForm, us let blog = Blog::find_by_fqn(&*conn, blog_name.clone()).unwrap(); let post = Post::find_by_slug(&*conn, slug.clone(), blog.id).unwrap(); let form = data.get(); - println!("form: {:?}", form); let (new_comment, id) = NewComment::build() .content(form.content.clone()) diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 9a518b25..a27b275b 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -4,6 +4,7 @@ use rocket::request::LenientForm; use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use serde_json; +use validator::{Validate, ValidationError}; use plume_common::activity_pub::{broadcast, ActivityStream}; use plume_common::utils; @@ -81,13 +82,23 @@ fn new(blog: String, user: User, conn: DbConn) -> Template { } } -#[derive(FromForm)] +#[derive(FromForm, Validate)] struct NewPostForm { + #[validate(custom = "valid_slug")] pub title: String, pub content: String, pub license: String } +fn valid_slug(title: &str) -> Result<(), ValidationError> { + let slug = title.to_string().to_kebab_case(); + if slug.len() == 0 { + Err(ValidationError::new("empty_slug")) + } else { + Ok(()) + } +} + #[post("/~//new", data = "")] fn create(blog_name: String, data: LenientForm, user: User, conn: DbConn) -> Redirect { let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap(); diff --git a/src/routes/session.rs b/src/routes/session.rs index c99548ba..1e033e90 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -5,6 +5,7 @@ use rocket::{ request::{LenientForm,FlashMessage} }; use rocket_contrib::Template; +use validator::{Validate, ValidationError}; use plume_models::{ db_conn::DbConn, @@ -32,9 +33,11 @@ fn new_message(user: Option, message: Message) -> Template { } -#[derive(FromForm)] +#[derive(FromForm, Validate)] struct LoginForm { + #[validate(length(min = "1"))] email_or_name: String, + #[validate(length(min = "8"))] password: String } diff --git a/src/routes/user.rs b/src/routes/user.rs index 3e47b8fc..0cc0576b 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -7,6 +7,7 @@ use rocket::{request::LenientForm, }; use rocket_contrib::Template; use serde_json; +use validator::{Validate, ValidationError}; use plume_common::activity_pub::{ ActivityStream, broadcast, Id, IntoId, @@ -157,14 +158,27 @@ fn update(_name: String, conn: DbConn, user: User, data: LenientForm Result<(), ValidationError> { + if form.password != form.password_confirmation { + Err(ValidationError::new("password_match")) + } else { + Ok(()) + } +} + #[post("/users/new", data = "")] fn create(conn: DbConn, data: LenientForm) -> Result { let form = data.get();