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
-1
View File
@@ -296,7 +296,6 @@ func GetFetchActionForm[T interface {
if web.IsFormSet(ctx) {
panic("don't mix fetch-action form validation with template-based form validation")
}
middleware.SkipTmplFormValidationError(ctx)
form := T(new(E))
errs := binding.Bind(ctx.Req, form)
errorMessage, fieldName, _ := middleware.BuildValidationErrorForUser(form, ctx.Locale, errs)
+5 -35
View File
@@ -4,17 +4,13 @@
package forms
import (
"net/http"
"gitea.dev/modules/structs"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
// AdminCreateUserForm form for admin to create user
type AdminCreateUserForm struct {
middleware.FormDefaultValidator
LoginType string `binding:"Required"`
LoginName string
UserName string `binding:"Required;Username;MaxSize(40)"`
@@ -27,6 +23,7 @@ type AdminCreateUserForm struct {
// AdminCreateBadgeForm form for admin to create badge
type AdminCreateBadgeForm struct {
middleware.FormDefaultValidator
Slug string `binding:"Required;BadgeSlug" locale:"admin.badges.slug"`
Description string `binding:"Required" locale:"admin.badges.description"`
ImageURL string `binding:"ValidUrl" locale:"admin.badges.image_url"`
@@ -34,30 +31,14 @@ type AdminCreateBadgeForm struct {
// AdminEditBadgeForm form for admin to edit badge
type AdminEditBadgeForm struct {
middleware.FormDefaultValidator
Description string `binding:"Required" locale:"admin.badges.description"`
ImageURL string `binding:"ValidUrl" locale:"admin.badges.image_url"`
}
// Validate validates form fields
func (f *AdminCreateBadgeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// Validate validates form fields
func (f *AdminEditBadgeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// Validate validates form fields
func (f *AdminCreateUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AdminEditUserForm form for admin to create user
type AdminEditUserForm struct {
middleware.FormDefaultValidator
LoginType string `binding:"Required"`
UserName string `binding:"Username;MaxSize(40)"`
LoginName string
@@ -79,20 +60,9 @@ type AdminEditUserForm struct {
Visibility structs.VisibleType
}
// Validate validates form fields
func (f *AdminEditUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AdminDashboardForm form for admin dashboard operations
type AdminDashboardForm struct {
middleware.FormDefaultValidator
Op string `binding:"required"`
From string
}
// Validate validates form fields
func (f *AdminDashboardForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+2 -14
View File
@@ -3,17 +3,11 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
// AuthenticationForm form for authentication
type AuthenticationForm struct {
middleware.FormDefaultValidator
Type int `binding:"Range(2,7)"`
Name string `binding:"Required;MaxSize(30)"`
TwoFactorPolicy string
@@ -96,9 +90,3 @@ type AuthenticationForm struct {
SSPISeparatorReplacement string `binding:"AlphaDashDot;MaxSize(5)"`
SSPIDefaultLanguage string
}
// Validate validates fields
func (f *AuthenticationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+4 -23
View File
@@ -5,13 +5,8 @@
package forms
import (
"net/http"
"gitea.dev/modules/structs"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
// ________ .__ __ .__
@@ -23,19 +18,15 @@ import (
// CreateOrgForm form for creating organization
type CreateOrgForm struct {
middleware.FormDefaultValidator
OrgName string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
Visibility structs.VisibleType
RepoAdminChangeTeamAccess bool
}
// Validate validates the fields
func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// UpdateOrgSettingForm form for updating organization settings
type UpdateOrgSettingForm struct {
middleware.FormDefaultValidator
FullName *string `binding:"MaxSize(100)"`
Email *string `binding:"MaxSize(255)"`
Description *string `binding:"MaxSize(255)"`
@@ -45,13 +36,8 @@ type UpdateOrgSettingForm struct {
RepoAdminChangeTeamAccess *bool
}
// Validate validates the fields
func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
type RenameOrgForm struct {
middleware.FormDefaultValidator
OrgName string `binding:"Required"`
NewOrgName string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
}
@@ -65,6 +51,7 @@ type RenameOrgForm struct {
// CreateTeamForm form for creating team
type CreateTeamForm struct {
middleware.FormDefaultValidator
TeamName string `binding:"Required;AlphaDashDot;MaxSize(255)"`
Description string `binding:"MaxSize(255)"`
Permission string
@@ -72,9 +59,3 @@ type CreateTeamForm struct {
CanCreateOrgRepo bool
Visibility string `binding:"OmitEmpty;In(public,limited,private)"`
}
// Validate validates the fields
func (f *CreateTeamForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+2 -13
View File
@@ -3,16 +3,10 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
type PackageCleanupRuleForm struct {
middleware.FormDefaultValidator
ID int64
Enabled bool
Type string `binding:"Required;In(alpine,arch,cargo,chef,composer,conan,conda,container,cran,debian,generic,go,helm,maven,npm,nuget,pub,pypi,rpm,rubygems,swift,terraform,vagrant)"`
@@ -23,8 +17,3 @@ type PackageCleanupRuleForm struct {
MatchFullName bool
Action string `binding:"Required;In(save,remove)"`
}
func (f *PackageCleanupRuleForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+3 -20
View File
@@ -3,36 +3,19 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
// NewBranchForm form for creating a new branch
type NewBranchForm struct {
middleware.FormDefaultValidator
NewBranchName string `binding:"Required;MaxSize(100);GitRefName"`
CurrentPath string
CreateTag bool
}
// Validate validates the fields
func (f *NewBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// RenameBranchForm form for rename a branch
type RenameBranchForm struct {
middleware.FormDefaultValidator
From string `binding:"Required;MaxSize(100);GitRefName"`
To string `binding:"Required;MaxSize(100);GitRefName"`
}
// Validate validates the fields
func (f *RenameBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+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;"`
}
+5 -10
View File
@@ -4,16 +4,12 @@
package forms
import (
"net/http"
"gitea.dev/modules/optional"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
type CommitCommonForm struct {
middleware.FormDefaultValidator
TreePath string `binding:"MaxSize(500)"`
CommitSummary string `binding:"MaxSize(100)"`
CommitMessage string
@@ -24,11 +20,6 @@ type CommitCommonForm struct {
CommitEmail string
}
func (f *CommitCommonForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
type CommitCommonFormInterface interface {
GetCommitCommonForm() *CommitCommonForm
}
@@ -38,20 +29,24 @@ func (f *CommitCommonForm) GetCommitCommonForm() *CommitCommonForm {
}
type EditRepoFileForm struct {
middleware.FormDefaultValidator
CommitCommonForm
Content optional.Option[string]
}
type DeleteRepoFileForm struct {
middleware.FormDefaultValidator
CommitCommonForm
}
type UploadRepoFileForm struct {
middleware.FormDefaultValidator
CommitCommonForm
Files []string
}
type CherryPickForm struct {
middleware.FormDefaultValidator
CommitCommonForm
Revert bool
}
+2 -14
View File
@@ -3,24 +3,12 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
// ProtectTagForm form for changing protected tag settings
type ProtectTagForm struct {
middleware.FormDefaultValidator
NamePattern string `binding:"Required;GlobOrRegexPattern"`
AllowlistUsers string
AllowlistTeams string
}
// Validate validates the fields
func (f *ProtectTagForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+2 -14
View File
@@ -3,22 +3,10 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
// EditRunnerForm form for admin to create runner
type EditRunnerForm struct {
middleware.FormDefaultValidator
Description string
}
// Validate validates form fields
func (f *EditRunnerForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+28 -150
View File
@@ -6,7 +6,6 @@ package forms
import (
"mime/multipart"
"net/http"
"strings"
user_model "gitea.dev/models/user"
@@ -15,13 +14,13 @@ import (
"gitea.dev/modules/util"
"gitea.dev/modules/validation"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
// InstallForm form for installation page
type InstallForm struct {
middleware.FormDefaultValidator
DbType string `binding:"Required"`
DbHost string
DbUser string
@@ -74,12 +73,6 @@ type InstallForm struct {
ReinstallConfirmThird bool
}
// Validate validates the fields
func (f *InstallForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// _____ ____ _________________ ___
// / _ \ | | \__ ___/ | \
// / /_\ \| | / | | / ~ \
@@ -89,18 +82,13 @@ func (f *InstallForm) Validate(req *http.Request, errs binding.Errors) binding.E
// RegisterForm form for registering
type RegisterForm struct {
middleware.FormDefaultValidator
UserName string `binding:"Required;Username;MaxSize(40)"`
Email string `binding:"Required;MaxSize(254)"`
Password string `binding:"MaxSize(255)"`
Retype string
}
// Validate validates the fields
func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// IsEmailDomainAllowed validates that the email address
// provided by the user matches what has been configured .
// The email is marked as allowed if it matches any of the
@@ -113,34 +101,25 @@ func (f *RegisterForm) IsEmailDomainAllowed() bool {
// MustChangePasswordForm form for updating your password after account creation
// by an admin
type MustChangePasswordForm struct {
middleware.FormDefaultValidator
Password string `binding:"Required;MaxSize(255)"`
Retype string
}
// Validate validates the fields
func (f *MustChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// SignInForm form for signing in with user/password
type SignInForm struct {
middleware.FormDefaultValidator
UserName string `binding:"Required;MaxSize(254)"`
// TODO remove required from password for SecondFactorAuthentication
Password string `binding:"Required;MaxSize(255)"`
Remember bool
}
// Validate validates the fields
func (f *SignInForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AuthorizationForm form for authorizing oauth2 clients
type AuthorizationForm struct {
ResponseType string `binding:"Required;In(code)"`
ClientID string `binding:"Required"`
middleware.FormDefaultValidator
ResponseType string
ClientID string
RedirectURI string
State string
Scope string
@@ -151,14 +130,9 @@ type AuthorizationForm struct {
CodeChallenge string
}
// Validate validates the fields
func (f *AuthorizationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// GrantApplicationForm form for authorizing oauth2 clients
type GrantApplicationForm struct {
middleware.FormDefaultValidator
ClientID string `binding:"Required"`
Granted bool
RedirectURI string
@@ -167,14 +141,9 @@ type GrantApplicationForm struct {
Nonce string
}
// Validate validates the fields
func (f *GrantApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
type AccessTokenForm struct {
middleware.FormDefaultValidator
GrantType string `json:"grant_type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
@@ -186,23 +155,12 @@ type AccessTokenForm struct {
CodeVerifier string `json:"code_verifier"`
}
// Validate validates the fields
func (f *AccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// IntrospectTokenForm for introspecting tokens
type IntrospectTokenForm struct {
middleware.FormDefaultValidator
Token string `json:"token"`
}
// Validate validates the fields
func (f *IntrospectTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// __________________________________________.___ _______ ________ _________
// / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
// \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
@@ -212,6 +170,7 @@ func (f *IntrospectTokenForm) Validate(req *http.Request, errs binding.Errors) b
// UpdateProfileForm form for updating profile
type UpdateProfileForm struct {
middleware.FormDefaultValidator
Name string `binding:"Username;MaxSize(40)"`
FullName string `binding:"MaxSize(100)"`
KeepEmailPrivate bool
@@ -222,86 +181,51 @@ type UpdateProfileForm struct {
KeepActivityPrivate bool
}
// Validate validates the fields
func (f *UpdateProfileForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// UpdateLanguageForm form for updating profile
type UpdateLanguageForm struct {
middleware.FormDefaultValidator
Language string
}
// Validate validates the fields
func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
const AvatarLocal = "local" // the AvatarForm.Source value that selects an uploaded avatar
// AvatarForm form for changing avatar
type AvatarForm struct {
middleware.FormDefaultValidator
Source string
Avatar *multipart.FileHeader
Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
}
// Validate validates the fields
func (f *AvatarForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AddEmailForm form for adding new email
type AddEmailForm struct {
middleware.FormDefaultValidator
Email string `binding:"Required;Email;MaxSize(254)"`
}
// Validate validates the fields
func (f *AddEmailForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// UpdateThemeForm form for updating a users' theme
type UpdateThemeForm struct {
middleware.FormDefaultValidator
Theme string `binding:"Required;MaxSize(255)"`
}
// Validate validates the field
func (f *UpdateThemeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ChangePasswordForm form for changing password
type ChangePasswordForm struct {
middleware.FormDefaultValidator
OldPassword string `form:"old_password" binding:"MaxSize(255)"`
Password string `form:"password" binding:"Required;MaxSize(255)"`
Retype string `form:"retype"`
}
// Validate validates the fields
func (f *ChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AddOpenIDForm is for changing openid uri
type AddOpenIDForm struct {
middleware.FormDefaultValidator
Openid string `binding:"Required;MaxSize(256)"`
}
// Validate validates the fields
func (f *AddOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AddKeyForm form for adding SSH/GPG key
type AddKeyForm struct {
middleware.FormDefaultValidator
Type string `binding:"OmitEmpty"`
Title string `binding:"Required;MaxSize(50)"`
Content string `binding:"Required"`
@@ -311,47 +235,27 @@ type AddKeyForm struct {
IsWritable bool
}
// Validate validates the fields
func (f *AddKeyForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// AddSecretForm for adding secrets
type AddSecretForm struct {
middleware.FormDefaultValidator
Name string `binding:"Required;MaxSize(255)"`
Data string `binding:"Required;MaxSize(65535)"`
Description string `binding:"MaxSize(65535)"`
}
// Validate validates the fields
func (f *AddSecretForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
type EditVariableForm struct {
middleware.FormDefaultValidator
Name string `binding:"Required;MaxSize(255)"`
Data string `binding:"Required;MaxSize(65535)"`
Description string `binding:"MaxSize(65535)"`
}
func (f *EditVariableForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// NewAccessTokenForm form for creating access token
type NewAccessTokenForm struct {
middleware.FormDefaultValidator
Name string `binding:"Required;MaxSize(255)" locale:"settings.token_name"`
}
// Validate validates the fields
func (f *NewAccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// EditOAuth2ApplicationForm form for editing oauth2 applications
type EditOAuth2ApplicationForm struct {
Name string `binding:"Required;MaxSize(255)" form:"application_name"`
@@ -371,68 +275,42 @@ func DetectInvalidOAuth2ApplicationRedirectURI(uris []string) (invalidURL string
return ""
}
// Validate validates the fields
func (f *EditOAuth2ApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
func (f *EditOAuth2ApplicationForm) Validate(ctx *middleware.ValidateContext, errs binding.Errors) binding.Errors {
invalidURI := DetectInvalidOAuth2ApplicationRedirectURI(util.SplitTrimSpace(f.RedirectURIs, "\n"))
if invalidURI != "" {
errs = middleware.AddValidationError(errs, "RedirectURIs", "RedirectURIs: "+ctx.Locale.TrString("form.url_error", `"`+invalidURI+`"`))
}
return middleware.Validate(ctx, errs, f)
return errs
}
// TwoFactorAuthForm for logging in with 2FA token.
type TwoFactorAuthForm struct {
middleware.FormDefaultValidator
Passcode string `binding:"Required"`
}
// Validate validates the fields
func (f *TwoFactorAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// TwoFactorScratchAuthForm for logging in with 2FA scratch token.
type TwoFactorScratchAuthForm struct {
middleware.FormDefaultValidator
Token string `binding:"Required"`
}
// Validate validates the fields
func (f *TwoFactorScratchAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// WebauthnRegistrationForm for reserving an WebAuthn name
type WebauthnRegistrationForm struct {
middleware.FormDefaultValidator
Name string `binding:"Required"`
}
// Validate validates the fields
func (f *WebauthnRegistrationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// PackageSettingForm form for package settings
type PackageSettingForm struct {
middleware.FormDefaultValidator
Action string
RepoName string `form:"repo_name"`
}
// Validate validates the fields
func (f *PackageSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
type BlockUserForm struct {
middleware.FormDefaultValidator
Action string `binding:"Required;In(block,unblock,note)"`
Blockee string `binding:"Required"`
Note string
}
func (f *BlockUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
+4 -26
View File
@@ -3,47 +3,25 @@
package forms
import (
"net/http"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
import "gitea.dev/modules/web/middleware"
// SignInOpenIDForm form for signing in with OpenID
type SignInOpenIDForm struct {
middleware.FormDefaultValidator
Openid string `binding:"Required;MaxSize(256)"`
Remember bool
}
// Validate validates the fields
func (f *SignInOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// SignUpOpenIDForm form for signin up with OpenID
type SignUpOpenIDForm struct {
middleware.FormDefaultValidator
UserName string `binding:"Required;Username;MaxSize(40)"`
Email string `binding:"Required;Email;MaxSize(254)"`
}
// Validate validates the fields
func (f *SignUpOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// ConnectOpenIDForm form for connecting an existing account to an OpenID URI
type ConnectOpenIDForm struct {
middleware.FormDefaultValidator
UserName string `binding:"Required;MaxSize(254)"`
Password string `binding:"Required;MaxSize(255)"`
}
// Validate validates the fields
func (f *ConnectOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}