Make forms validatable

This commit is contained in:
Bat 2018-06-29 14:56:00 +02:00
parent b008e11fb0
commit c81bb9ec25
5 changed files with 46 additions and 6 deletions

View file

@ -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<Redirect>{
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 = "<data>")]
fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Redirect {
let form = data.get();

View file

@ -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<i32>,
#[validate(length(min = "1"))]
pub content: String
}
@ -26,7 +28,6 @@ fn create(blog_name: String, slug: String, data: LenientForm<NewCommentForm>, 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())

View file

@ -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("/~/<blog_name>/new", data = "<data>")]
fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: DbConn) -> Redirect {
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap();

View file

@ -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<User>, message: Message) -> Template {
}
#[derive(FromForm)]
#[derive(FromForm, Validate)]
struct LoginForm {
#[validate(length(min = "1"))]
email_or_name: String,
#[validate(length(min = "8"))]
password: String
}

View file

@ -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<UpdateUserF
Redirect::to(uri!(me))
}
#[derive(FromForm)]
#[derive(FromForm, Validate)]
#[validate(schema(function = "passwords_match", skip_on_field_errors = "false"))]
struct NewUserForm {
#[validate(length(min = "1"))]
username: String,
#[validate(email)]
email: String,
#[validate(length(min = "8"))]
password: String,
#[validate(length(min = "8"))]
password_confirmation: String
}
fn passwords_match(form: &NewUserForm) -> Result<(), ValidationError> {
if form.password != form.password_confirmation {
Err(ValidationError::new("password_match"))
} else {
Ok(())
}
}
#[post("/users/new", data = "<data>")]
fn create(conn: DbConn, data: LenientForm<NewUserForm>) -> Result<Redirect, String> {
let form = data.get();