refactor: clean up form binding & validation (#38873)

Clarify the "validation" and "error display" logic.

All the copied&pasted `Validate` functions are removed.
This commit is contained in:
wxiaoguang
2026-08-14 10:15:33 +08:00
committed by GitHub
parent 8b40df255b
commit 72a9debaff
30 changed files with 331 additions and 737 deletions
+37 -181
View File
@@ -5,7 +5,6 @@
package forms
import (
"net/http"
"strings"
issues_model "gitea.dev/models/issues"
@@ -14,7 +13,6 @@ import (
"gitea.dev/modules/structs"
"gitea.dev/modules/util"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.dev/services/webhook"
"gitea.com/go-chi/binding"
@@ -22,6 +20,7 @@ import (
// CreateRepoForm form for creating repository
type CreateRepoForm struct {
middleware.FormDefaultValidator
UID int64 `binding:"Required"`
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
Private bool
@@ -47,15 +46,10 @@ type CreateRepoForm struct {
ObjectFormatName string
}
// Validate validates the fields
func (f *CreateRepoForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// MigrateRepoForm form for migrating repository
// this is used to interact with web ui
type MigrateRepoForm struct {
middleware.FormDefaultValidator
// required: true
CloneAddr string `json:"clone_addr" binding:"Required"`
Service structs.GitServiceType `json:"service"`
@@ -83,14 +77,9 @@ type MigrateRepoForm struct {
AWSSecretAccessKey string `json:"aws_secret_access_key"`
}
// Validate validates the fields
func (f *MigrateRepoForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// RepoSettingForm form for changing repository settings
type RepoSettingForm struct {
middleware.FormDefaultValidator
RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
Description string `binding:"MaxSize(2048)"`
Website string `binding:"ValidUrl;MaxSize(1024)"`
@@ -160,14 +149,9 @@ type RepoSettingForm struct {
RequestReindexType string
}
// Validate validates the fields
func (f *RepoSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ProtectBranchForm form for changing protected branch settings
type ProtectBranchForm struct {
middleware.FormDefaultValidator
RuleName string `binding:"Required"`
RuleID int64
EnablePush string
@@ -202,14 +186,9 @@ type ProtectBranchForm struct {
BlockAdminMergeOverride bool
}
// Validate validates the fields
func (f *ProtectBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// WebhookForm form for changing web hook
type WebhookForm struct {
middleware.FormDefaultValidator
Name string `binding:"MaxSize(255)"`
Events string
Create bool
@@ -259,31 +238,21 @@ func (f WebhookForm) ChooseEvents() bool {
// NewWebhookForm form for creating web hook
type NewWebhookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
HTTPMethod string `binding:"Required;In(POST,GET)"`
ContentType int `binding:"Required"`
WebhookForm
}
// Validate validates the fields
func (f *NewWebhookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewGogshookForm form for creating gogs hook
type NewGogshookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
ContentType int `binding:"Required"`
WebhookForm
}
// Validate validates the fields
func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewSlackHookForm form for creating slack hook
type NewSlackHookForm struct {
PayloadURL string `binding:"Required;ValidUrl"`
@@ -294,121 +263,80 @@ type NewSlackHookForm struct {
WebhookForm
}
// Validate validates the fields
func (f *NewSlackHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
func (f *NewSlackHookForm) Validate(ctx *middleware.ValidateContext, errs binding.Errors) binding.Errors {
if !webhook.IsValidSlackChannel(strings.TrimSpace(f.Channel)) {
errs = middleware.AddValidationError(errs, "Channel", ctx.Locale.TrString("repo.settings.add_webhook.invalid_channel_name"))
}
return middleware.Validate(ctx, errs, f)
return errs
}
// NewDiscordHookForm form for creating discord hook
type NewDiscordHookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
Username string
IconURL string
WebhookForm
}
// Validate validates the fields
func (f *NewDiscordHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewDingtalkHookForm form for creating dingtalk hook
type NewDingtalkHookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewDingtalkHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewTelegramHookForm form for creating telegram hook
type NewTelegramHookForm struct {
middleware.FormDefaultValidator
BotToken string `binding:"Required"`
ChatID string `binding:"Required"`
ThreadID string
WebhookForm
}
// Validate validates the fields
func (f *NewTelegramHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewMatrixHookForm form for creating Matrix hook
type NewMatrixHookForm struct {
middleware.FormDefaultValidator
HomeserverURL string `binding:"Required;ValidUrl"`
RoomID string `binding:"Required"`
MessageType int
WebhookForm
}
// Validate validates the fields
func (f *NewMatrixHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewMSTeamsHookForm form for creating MS Teams hook
type NewMSTeamsHookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewMSTeamsHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewFeishuHookForm form for creating feishu hook
type NewFeishuHookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewFeishuHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewWechatWorkHookForm form for creating wechatwork hook
type NewWechatWorkHookForm struct {
middleware.FormDefaultValidator
PayloadURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewWechatWorkHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewPackagistHookForm form for creating packagist hook
type NewPackagistHookForm struct {
middleware.FormDefaultValidator
Username string `binding:"Required"`
APIToken string `binding:"Required"`
PackageURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewPackagistHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// CreateIssueForm form for creating issue
type CreateIssueForm struct {
middleware.FormDefaultValidator
Title string `binding:"Required;MaxSize(255)"`
AssigneeIDs string `form:"assignee_ids"`
ReviewerIDs string `form:"reviewer_ids"`
@@ -419,49 +347,29 @@ type CreateIssueForm struct {
AllowMaintainerEdit bool
}
// Validate validates the fields
func (f *CreateIssueForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// CreateCommentForm form for creating comment
type CreateCommentForm struct {
middleware.FormDefaultValidator
Content string
Status string `binding:"OmitEmpty;In(reopen,close)"`
Files []string
}
// Validate validates the fields
func (f *CreateCommentForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ReactionForm form for adding and removing reaction
type ReactionForm struct {
middleware.FormDefaultValidator
Content string `binding:"Required"`
}
// Validate validates the fields
func (f *ReactionForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// IssueLockForm form for locking an issue
type IssueLockForm struct {
middleware.FormDefaultValidator
Reason string `binding:"Required"`
}
// Validate validates the fields
func (f *IssueLockForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// CreateProjectForm form for creating a project
type CreateProjectForm struct {
middleware.FormDefaultValidator
Title string `binding:"Required;MaxSize(100)"`
Content string
TemplateType project_model.TemplateType
@@ -470,6 +378,7 @@ type CreateProjectForm struct {
// EditProjectColumnForm is a form for editing a project column
type EditProjectColumnForm struct {
middleware.FormDefaultValidator
Title string `binding:"Required;MaxSize(100)"`
Sorting int8
Color string `binding:"MaxSize(7)"`
@@ -477,19 +386,15 @@ type EditProjectColumnForm struct {
// CreateMilestoneForm form for creating milestone
type CreateMilestoneForm struct {
middleware.FormDefaultValidator
Title string `binding:"Required;MaxSize(50)"`
Content string
Deadline string
}
// Validate validates the fields
func (f *CreateMilestoneForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// CreateLabelForm form for creating label
type CreateLabelForm struct {
middleware.FormDefaultValidator
ID int64
Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_title"`
Exclusive bool `form:"exclusive"`
@@ -499,26 +404,16 @@ type CreateLabelForm struct {
Color string `binding:"Required;MaxSize(7)" locale:"repo.issues.label_color"`
}
// Validate validates the fields
func (f *CreateLabelForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// InitializeLabelsForm form for initializing labels
type InitializeLabelsForm struct {
middleware.FormDefaultValidator
TemplateName string `binding:"Required"`
}
// Validate validates the fields
func (f *InitializeLabelsForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// MergePullRequestForm form for merging Pull Request
// swagger:model MergePullRequestOption
type MergePullRequestForm struct {
middleware.FormDefaultValidator
// required: true
// enum: ["merge","rebase","rebase-merge","squash","fast-forward-only","manually-merged"]
Do string `json:"do" binding:"Required;In(merge,rebase,rebase-merge,squash,fast-forward-only,manually-merged)"`
@@ -564,14 +459,9 @@ func (f *MergePullRequestForm) UnmarshalJSON(b []byte) error {
return nil
}
// Validate validates the fields
func (f *MergePullRequestForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// CodeCommentForm form for adding code comments for PRs
type CodeCommentForm struct {
middleware.FormDefaultValidator
Origin string `binding:"Required;In(timeline,diff)"`
Content string `binding:"Required"`
Side string `binding:"Required;In(previous,proposed)"`
@@ -583,26 +473,15 @@ type CodeCommentForm struct {
Files []string
}
// Validate validates the fields
func (f *CodeCommentForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// SubmitReviewForm for submitting a finished code review
type SubmitReviewForm struct {
middleware.FormDefaultValidator
Content string
Type string
CommitID string
Files []string
}
// Validate validates the fields
func (f *SubmitReviewForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ReviewType will return the corresponding ReviewType for type
func (f SubmitReviewForm) ReviewType() issues_model.ReviewType {
switch f.Type {
@@ -629,12 +508,14 @@ func (f SubmitReviewForm) HasEmptyContent() bool {
// DismissReviewForm for dismissing stale review by repo admin
type DismissReviewForm struct {
middleware.FormDefaultValidator
ReviewID int64 `binding:"Required"`
Message string
}
// UpdateAllowEditsForm form for changing if PR allows edits from maintainers
type UpdateAllowEditsForm struct {
middleware.FormDefaultValidator
AllowMaintainerEdit bool
}
@@ -647,6 +528,7 @@ type UpdateAllowEditsForm struct {
// NewReleaseForm form for creating release
type NewReleaseForm struct {
middleware.FormDefaultValidator
TagName string `binding:"Required;GitRefName;MaxSize(255)"`
Target string `form:"tag_target" binding:"Required;MaxSize(255)"`
Title string `binding:"MaxSize(255)"`
@@ -658,27 +540,17 @@ type NewReleaseForm struct {
Files []string
}
// Validate validates the fields
func (f *NewReleaseForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// GenerateReleaseNotesForm retrieves release notes recommendations.
type GenerateReleaseNotesForm struct {
middleware.FormDefaultValidator
TagName string `form:"tag_name" binding:"Required;GitRefName;MaxSize(255)"`
TagTarget string `form:"tag_target" binding:"MaxSize(255)"`
PreviousTag string `form:"previous_tag" binding:"MaxSize(255)"`
}
// Validate validates the fields
func (f *GenerateReleaseNotesForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// EditReleaseForm form for changing release
type EditReleaseForm struct {
middleware.FormDefaultValidator
Title string `form:"title" binding:"Required;MaxSize(255)"`
Content string `form:"content"`
Draft string `form:"draft"`
@@ -686,12 +558,6 @@ type EditReleaseForm struct {
Files []string
}
// Validate validates the fields
func (f *EditReleaseForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// __ __.__ __ .__
// / \ / \__| | _|__|
// \ \/\/ / | |/ / |
@@ -701,18 +567,12 @@ func (f *EditReleaseForm) Validate(req *http.Request, errs binding.Errors) bindi
// NewWikiForm form for creating wiki
type NewWikiForm struct {
middleware.FormDefaultValidator
Title string `binding:"Required"`
Content string `binding:"Required"`
Message string
}
// Validate validates the fields
// FIXME: use code generation to generate this method.
func (f *NewWikiForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ___________.__ ___________ __
// \__ ___/|__| _____ ____ \__ ___/___________ ____ | | __ ___________
// | | | |/ \_/ __ \ | | \_ __ \__ \ _/ ___\| |/ // __ \_ __ \
@@ -722,17 +582,13 @@ func (f *NewWikiForm) Validate(req *http.Request, errs binding.Errors) binding.E
// AddTimeManuallyForm form that adds spent time manually.
type AddTimeManuallyForm struct {
middleware.FormDefaultValidator
Hours int `binding:"Range(0,1000)"`
Minutes int `binding:"Range(0,1000)"`
}
// Validate validates the fields
func (f *AddTimeManuallyForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// SaveTopicForm form for save topics for repository
type SaveTopicForm struct {
middleware.FormDefaultValidator
Topics []string `binding:"topics;Required;"`
}