This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new 30534ad0 feat: Check HTML comments in question, answer or comments
30534ad0 is described below

commit 30534ad0d6eea6e18c2f53ef51f5600db93e378e
Author: Luffy <[email protected]>
AuthorDate: Tue Dec 10 17:08:08 2024 +0800

    feat: Check HTML comments in question, answer or comments
---
 i18n/en_US.yaml                    |  6 ++++++
 i18n/zh_CN.yaml                    |  6 ++++++
 internal/base/reason/reason.go     |  3 +++
 internal/schema/answer_schema.go   | 14 ++++++++++++++
 internal/schema/comment_schema.go  | 14 ++++++++++++++
 internal/schema/question_schema.go | 29 +++++++++++++++++++++++++++++
 6 files changed, 72 insertions(+)

diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index f4c67a18..801acc69 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -169,6 +169,8 @@ backend:
         other: No permission to update.
       question_closed_cannot_add:
         other: Questions are closed and cannot be added.
+      content_cannot_empty:
+        other: Answer content cannot be empty.
     comment:
       edit_without_permission:
         other: Comment are not allowed to edit.
@@ -176,6 +178,8 @@ backend:
         other: Comment not found.
       cannot_edit_after_deadline:
         other: The comment time has been too long to modify.
+      content_cannot_empty:
+        other: Comment content cannot be empty.
     email:
       duplicate:
         other: Email already exists.
@@ -225,6 +229,8 @@ backend:
         other: No permission to close.
       cannot_update:
         other: No permission to update.
+      content_cannot_empty:
+        other: Content cannot be empty.
     rank:
       fail_to_meet_the_condition:
         other: Reputation rank fail to meet the condition.
diff --git a/i18n/zh_CN.yaml b/i18n/zh_CN.yaml
index 07f8d2ea..0567a10c 100644
--- a/i18n/zh_CN.yaml
+++ b/i18n/zh_CN.yaml
@@ -168,6 +168,8 @@ backend:
         other: 没有更新权限。
       question_closed_cannot_add:
         other: 问题已关闭,无法添加。
+      content_cannot_empty:
+        other: 回答内容不能为空。
     comment:
       edit_without_permission:
         other: 不允许编辑评论。
@@ -175,6 +177,8 @@ backend:
         other: 评论未找到。
       cannot_edit_after_deadline:
         other: 评论时间太久,无法修改。
+      content_cannot_empty:
+        other: 评论内容不能为空。
     email:
       duplicate:
         other: 邮箱已存在。
@@ -224,6 +228,8 @@ backend:
         other: 没有关闭权限。
       cannot_update:
         other: 没有更新权限。
+      content_cannot_empty:
+        other: 内容不能为空。
     rank:
       fail_to_meet_the_condition:
         other: 声望值未达到要求。
diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go
index 24d7ab5f..f318e335 100644
--- a/internal/base/reason/reason.go
+++ b/internal/base/reason/reason.go
@@ -46,12 +46,15 @@ const (
        QuestionCannotUpdate             = "error.question.cannot_update"
        QuestionAlreadyDeleted           = "error.question.already_deleted"
        QuestionUnderReview              = "error.question.under_review"
+       QuestionContentCannotEmpty       = "error.question.content_cannot_empty"
        AnswerNotFound                   = "error.answer.not_found"
        AnswerCannotDeleted              = "error.answer.cannot_deleted"
        AnswerCannotUpdate               = "error.answer.cannot_update"
        AnswerCannotAddByClosedQuestion  = 
"error.answer.question_closed_cannot_add"
        AnswerRestrictAnswer             = "error.answer.restrict_answer"
+       AnswerContentCannotEmpty         = "error.answer.content_cannot_empty"
        CommentEditWithoutPermission     = 
"error.comment.edit_without_permission"
+       CommentContentCannotEmpty        = "error.comment.content_cannot_empty"
        DisallowVote                     = "error.object.disallow_vote"
        DisallowFollow                   = "error.object.disallow_follow"
        DisallowVoteYourSelf             = 
"error.object.disallow_vote_your_self"
diff --git a/internal/schema/answer_schema.go b/internal/schema/answer_schema.go
index 01bc64a2..6c6e0881 100644
--- a/internal/schema/answer_schema.go
+++ b/internal/schema/answer_schema.go
@@ -20,8 +20,10 @@
 package schema
 
 import (
+       "github.com/apache/incubator-answer/internal/base/reason"
        "github.com/apache/incubator-answer/internal/base/validator"
        "github.com/apache/incubator-answer/pkg/converter"
+       "github.com/segmentfault/pacman/errors"
 )
 
 // RemoveAnswerReq delete answer request
@@ -60,6 +62,12 @@ type AnswerAddReq struct {
 
 func (req *AnswerAddReq) Check() (errFields []*validator.FormErrorField, err 
error) {
        req.HTML = converter.Markdown2HTML(req.Content)
+       if req.HTML == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "content",
+                       ErrorMsg:   reason.AnswerContentCannotEmpty,
+               }), errors.BadRequest(reason.AnswerContentCannotEmpty)
+       }
        return nil, nil
 }
 
@@ -79,6 +87,12 @@ type AnswerUpdateReq struct {
 
 func (req *AnswerUpdateReq) Check() (errFields []*validator.FormErrorField, 
err error) {
        req.HTML = converter.Markdown2HTML(req.Content)
+       if req.HTML == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "content",
+                       ErrorMsg:   reason.AnswerContentCannotEmpty,
+               }), errors.BadRequest(reason.AnswerContentCannotEmpty)
+       }
        return nil, nil
 }
 
diff --git a/internal/schema/comment_schema.go 
b/internal/schema/comment_schema.go
index 275e1b9a..59ed898d 100644
--- a/internal/schema/comment_schema.go
+++ b/internal/schema/comment_schema.go
@@ -20,10 +20,12 @@
 package schema
 
 import (
+       "github.com/apache/incubator-answer/internal/base/reason"
        "github.com/apache/incubator-answer/internal/base/validator"
        "github.com/apache/incubator-answer/internal/entity"
        "github.com/apache/incubator-answer/pkg/converter"
        "github.com/jinzhu/copier"
+       "github.com/segmentfault/pacman/errors"
 )
 
 // AddCommentReq add comment request
@@ -53,6 +55,12 @@ type AddCommentReq struct {
 
 func (req *AddCommentReq) Check() (errFields []*validator.FormErrorField, err 
error) {
        req.ParsedText = converter.Markdown2HTML(req.OriginalText)
+       if req.ParsedText == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "original_text",
+                       ErrorMsg:   reason.CommentContentCannotEmpty,
+               }), errors.BadRequest(reason.CommentContentCannotEmpty)
+       }
        return nil, nil
 }
 
@@ -88,6 +96,12 @@ type UpdateCommentReq struct {
 
 func (req *UpdateCommentReq) Check() (errFields []*validator.FormErrorField, 
err error) {
        req.ParsedText = converter.Markdown2HTML(req.OriginalText)
+       if req.ParsedText == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "original_text",
+                       ErrorMsg:   reason.CommentContentCannotEmpty,
+               }), errors.BadRequest(reason.CommentContentCannotEmpty)
+       }
        return nil, nil
 }
 
diff --git a/internal/schema/question_schema.go 
b/internal/schema/question_schema.go
index accc3374..9cbd8cc7 100644
--- a/internal/schema/question_schema.go
+++ b/internal/schema/question_schema.go
@@ -20,6 +20,8 @@
 package schema
 
 import (
+       "github.com/apache/incubator-answer/internal/base/reason"
+       "github.com/segmentfault/pacman/errors"
        "strings"
        "time"
 
@@ -97,6 +99,12 @@ func (req *QuestionAdd) Check() (errFields 
[]*validator.FormErrorField, err erro
                        tag.ParsedText = 
converter.Markdown2HTML(tag.OriginalText)
                }
        }
+       if req.HTML == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "content",
+                       ErrorMsg:   reason.QuestionContentCannotEmpty,
+               }), errors.BadRequest(reason.QuestionContentCannotEmpty)
+       }
        return nil, nil
 }
 
@@ -129,6 +137,21 @@ func (req *QuestionAddByAnswer) Check() (errFields 
[]*validator.FormErrorField,
                        tag.ParsedText = 
converter.Markdown2HTML(tag.OriginalText)
                }
        }
+       if req.HTML == "" {
+               errFields = append(errFields, &validator.FormErrorField{
+                       ErrorField: "content",
+                       ErrorMsg:   reason.QuestionContentCannotEmpty,
+               })
+       }
+       if req.AnswerHTML == "" {
+               errFields = append(errFields, &validator.FormErrorField{
+                       ErrorField: "answer_content",
+                       ErrorMsg:   reason.AnswerContentCannotEmpty,
+               })
+       }
+       if req.HTML == "" || req.AnswerHTML == "" {
+               return errFields, 
errors.BadRequest(reason.QuestionContentCannotEmpty)
+       }
        return nil, nil
 }
 
@@ -203,6 +226,12 @@ type QuestionUpdateInviteUser struct {
 
 func (req *QuestionUpdate) Check() (errFields []*validator.FormErrorField, err 
error) {
        req.HTML = converter.Markdown2HTML(req.Content)
+       if req.HTML == "" {
+               return append(errFields, &validator.FormErrorField{
+                       ErrorField: "content",
+                       ErrorMsg:   reason.QuestionContentCannotEmpty,
+               }), errors.BadRequest(reason.QuestionContentCannotEmpty)
+       }
        return nil, nil
 }
 

Reply via email to