diff --git a/frontend/app/components/Domain/Recipe/RecipePage/RecipePageParts/RecipePageComments.vue b/frontend/app/components/Domain/Recipe/RecipePage/RecipePageParts/RecipePageComments.vue
index e2a9afabf..3ef27a973 100644
--- a/frontend/app/components/Domain/Recipe/RecipePage/RecipePageParts/RecipePageComments.vue
+++ b/frontend/app/components/Domain/Recipe/RecipePage/RecipePageParts/RecipePageComments.vue
@@ -35,7 +35,7 @@
@@ -96,9 +96,13 @@ const { user } = usePageUser();
const comment = ref("");
async function submitComment() {
+ const text = comment.value.trim();
+ if (!text) {
+ return;
+ }
const { data } = await api.recipes.comments.createOne({
recipeId: recipe.value.id,
- text: comment.value,
+ text,
});
if (data) {
diff --git a/frontend/app/lib/api/types/household.ts b/frontend/app/lib/api/types/household.ts
index a215f23e5..2848bcc48 100644
--- a/frontend/app/lib/api/types/household.ts
+++ b/frontend/app/lib/api/types/household.ts
@@ -526,9 +526,9 @@ export interface RecipeNote {
referenceId?: string;
}
export interface RecipeCommentOut {
+ id: string;
recipeId: string;
text: string;
- id: string;
createdAt: string;
updatedAt: string;
userId: string;
diff --git a/frontend/app/lib/api/types/recipe.ts b/frontend/app/lib/api/types/recipe.ts
index baedcfab1..a9f47b3c9 100644
--- a/frontend/app/lib/api/types/recipe.ts
+++ b/frontend/app/lib/api/types/recipe.ts
@@ -320,9 +320,9 @@ export interface RecipeNote {
referenceId?: string;
}
export interface RecipeCommentOut {
+ id: string;
recipeId: string;
text: string;
- id: string;
createdAt: string;
updatedAt: string;
userId: string;
diff --git a/mealie/schema/recipe/recipe_comments.py b/mealie/schema/recipe/recipe_comments.py
index 0694af921..73889058e 100644
--- a/mealie/schema/recipe/recipe_comments.py
+++ b/mealie/schema/recipe/recipe_comments.py
@@ -1,6 +1,6 @@
from datetime import datetime
-from pydantic import UUID4, ConfigDict
+from pydantic import UUID4, ConfigDict, field_validator
from sqlalchemy.orm import joinedload
from sqlalchemy.orm.interfaces import LoaderOption
@@ -19,10 +19,19 @@ class UserBase(MealieModel):
model_config = ConfigDict(from_attributes=True)
+def validate_comment_text(value: str) -> str:
+ stripped = value.strip()
+ if not stripped:
+ raise ValueError("comment text must not be empty")
+ return stripped
+
+
class RecipeCommentCreate(MealieModel):
recipe_id: UUID4
text: str
+ _validate_text = field_validator("text")(validate_comment_text)
+
class RecipeCommentSave(RecipeCommentCreate):
user_id: UUID4
@@ -32,10 +41,13 @@ class RecipeCommentUpdate(MealieModel):
id: UUID4
text: str
+ _validate_text = field_validator("text")(validate_comment_text)
-class RecipeCommentOut(RecipeCommentCreate):
+
+class RecipeCommentOut(MealieModel):
id: UUID4
recipe_id: UUID4
+ text: str
created_at: datetime
updated_at: datetime = UpdatedAtField(...)
user_id: UUID4
diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_comments.py b/tests/integration_tests/user_recipe_tests/test_recipe_comments.py
index f3b8720ad..20b9c6e68 100644
--- a/tests/integration_tests/user_recipe_tests/test_recipe_comments.py
+++ b/tests/integration_tests/user_recipe_tests/test_recipe_comments.py
@@ -142,3 +142,19 @@ def test_user_can_comment_on_other_household(api_client: TestClient, unique_reci
response = api_client.get(api_routes.comments_item_id(comment_id), headers=h2_user.token)
assert response.status_code == 404
+
+
+@pytest.mark.parametrize("text", ["", " ", "\n\t \n"])
+def test_whitespace_only_comment_rejected(
+ api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser, text: str
+):
+ create_data = {"recipeId": str(unique_recipe.id), "text": text}
+ response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
+ assert response.status_code == 422
+
+
+def test_comment_text_is_trimmed(api_client: TestClient, unique_recipe: Recipe, unique_user: TestUser):
+ create_data = {"recipeId": str(unique_recipe.id), "text": " hello "}
+ response = api_client.post(api_routes.comments, json=create_data, headers=unique_user.token)
+ assert response.status_code == 201
+ assert response.json()["text"] == "hello"