Make content fields non optional

Add error types for tagline validation
This commit is contained in:
Freakazoid182 2024-04-06 12:16:19 +02:00
parent 55be9a14ac
commit 7759602c9d
6 changed files with 45 additions and 10 deletions

View file

@ -9,7 +9,7 @@ use ts_rs::TS;
#[cfg_attr(feature = "full", ts(export))]
/// Create a tagline
pub struct CreateTagline {
pub content: Option<String>,
pub content: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
@ -18,7 +18,7 @@ pub struct CreateTagline {
/// Update a tagline
pub struct UpdateTagline {
pub id: TaglineId,
pub content: Option<String>,
pub content: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]

View file

@ -13,7 +13,7 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field};
use lemmy_utils::{error::LemmyError, utils::validation::is_valid_tagline_content};
#[tracing::instrument(skip(context))]
pub async fn create_tagline(
@ -28,8 +28,14 @@ pub async fn create_tagline(
let slur_regex = local_site_to_slur_regex(&local_site);
let url_blocklist = get_url_blocklist(&context).await?;
let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?;
is_valid_body_field(&content, false)?;
let processed_content = process_markdown_opt(
&Some(data.content.to_owned()),
&slur_regex,
&url_blocklist,
&context,
)
.await?;
let content = is_valid_tagline_content(processed_content)?;
let tagline_form = TaglineInsertForm {
local_site_id: local_site.id,

View file

@ -14,7 +14,7 @@ use lemmy_db_schema::{
utils::naive_now,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field};
use lemmy_utils::{error::LemmyError, utils::validation::is_valid_tagline_content};
#[tracing::instrument(skip(context))]
pub async fn update_tagline(
@ -29,8 +29,14 @@ pub async fn update_tagline(
let slur_regex = local_site_to_slur_regex(&local_site);
let url_blocklist = get_url_blocklist(&context).await?;
let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?;
is_valid_body_field(&content, false)?;
let processed_content = process_markdown_opt(
&Some(data.content.to_owned()),
&slur_regex,
&url_blocklist,
&context,
)
.await?;
let content = is_valid_tagline_content(processed_content)?;
let tagline_form = TaglineUpdateForm {
content,

View file

@ -34,13 +34,13 @@ pub struct Tagline {
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
pub struct TaglineInsertForm {
pub local_site_id: LocalSiteId,
pub content: Option<String>,
pub content: String,
}
#[derive(Clone, Default)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
pub struct TaglineUpdateForm {
pub content: Option<String>,
pub content: String,
pub updated: Option<Option<DateTime<Utc>>>,
}

View file

@ -165,6 +165,8 @@ pub enum LemmyErrorType {
InvalidUnixTime,
InvalidBotAction,
CantBlockLocalInstance,
TaglineContentRequired,
TaglineContentLengthOverflow,
Unknown(String),
}

View file

@ -22,6 +22,8 @@ const BIO_MAX_LENGTH: usize = 300;
const ALT_TEXT_MAX_LENGTH: usize = 300;
const SITE_NAME_MAX_LENGTH: usize = 20;
const SITE_NAME_MIN_LENGTH: usize = 1;
const TAGLINE_CONTENT_MIN_LENGTH: usize = 1;
const TAGLINE_CONTENT_MAX_LENGTH: usize = 50000;
const SITE_DESCRIPTION_MAX_LENGTH: usize = 150;
//Invisible unicode characters, taken from https://invisible-characters.com/
const FORBIDDEN_DISPLAY_CHARS: [char; 53] = [
@ -169,6 +171,25 @@ pub fn is_valid_body_field(body: &Option<String>, post: bool) -> LemmyResult<()>
Ok(())
}
pub fn is_valid_tagline_content(content: Option<String>) -> LemmyResult<String> {
match content {
Some(content) => {
min_length_check(
&content,
TAGLINE_CONTENT_MIN_LENGTH,
LemmyErrorType::TaglineContentRequired,
)?;
max_length_check(
&content,
TAGLINE_CONTENT_MAX_LENGTH,
LemmyErrorType::TaglineContentLengthOverflow,
)?;
Ok(content)
}
None => Err(LemmyErrorType::TaglineContentRequired.into()),
}
}
pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> {
max_length_check(bio, BIO_MAX_LENGTH, LemmyErrorType::BioLengthOverflow)
}