mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-21 14:30:32 +00:00
chore: form binding trim space (#38978)
Use "binding:TrimSpace" instead of fragile IsEmptyString And fix a bug in locale's `HasKey`: it should also try the default language if current language doesn't have the translation key, a new test is added.
This commit is contained in:
@@ -58,7 +58,7 @@ type CreateProjectOption struct {
|
||||
// EditProjectOption represents options for editing a project
|
||||
// swagger:model
|
||||
type EditProjectOption struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Title *string `json:"title,omitempty" binding:"TrimSpace;Required"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
// Card type: "text_only" or "images_and_text"
|
||||
CardType *string `json:"card_type,omitempty"`
|
||||
@@ -94,7 +94,7 @@ type CreateProjectColumnOption struct {
|
||||
// EditProjectColumnOption represents options for editing a project column
|
||||
// swagger:model
|
||||
type EditProjectColumnOption struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Title *string `json:"title,omitempty" binding:"TrimSpace;Required"`
|
||||
// Column color in 6-digit hex format, e.g. #FF0000
|
||||
Color *string `json:"color,omitempty"`
|
||||
// Position of the column within the project, between -128 and 127
|
||||
|
||||
@@ -43,7 +43,7 @@ type WikiPageMetaData struct {
|
||||
// CreateWikiPageOptions form for creating wiki
|
||||
type CreateWikiPageOptions struct {
|
||||
// page title. leave empty to keep unchanged
|
||||
Title string `json:"title"`
|
||||
Title string `json:"title" binding:"TrimSpace;Required"`
|
||||
// content must be base64 encoded
|
||||
ContentBase64 string `json:"content_base64"`
|
||||
// optional commit message summarizing the change
|
||||
|
||||
@@ -16,6 +16,7 @@ func TestLocaleStore(t *testing.T) {
|
||||
{
|
||||
".dot.name": "Dot Name",
|
||||
"fmt": "%[1]s %[2]s",
|
||||
"only_in_default": "",
|
||||
|
||||
"section.sub": "Sub String",
|
||||
"section.mixed": "test value; <span style=\"color: red; background: none;\">%s</span>"
|
||||
@@ -60,6 +61,9 @@ func TestLocaleStore(t *testing.T) {
|
||||
|
||||
found := lang1.HasKey("no-such")
|
||||
assert.False(t, found)
|
||||
found = lang2.HasKey("only_in_default")
|
||||
assert.True(t, found)
|
||||
|
||||
assert.NoError(t, ls.Close())
|
||||
|
||||
res := lang1.TrHTML("<no-such>")
|
||||
|
||||
@@ -177,5 +177,8 @@ func (l *locale) HasKey(trKey string) bool {
|
||||
return false
|
||||
}
|
||||
_, ok = l.idxToMsgMap[idx]
|
||||
if !ok {
|
||||
_, ok = l.store.localeMap[l.store.defaultLang]
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
rand2 "math/rand/v2"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"gitea.dev/modules/container"
|
||||
@@ -22,11 +21,6 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
// IsEmptyString checks if the provided string is empty
|
||||
func IsEmptyString(s string) bool {
|
||||
return len(strings.TrimSpace(s)) == 0
|
||||
}
|
||||
|
||||
// ParseYamlBool parses YAML 1.2 boolean values into bool
|
||||
func ParseYamlBool(s string) bool {
|
||||
return s == "true" || s == "True" || s == "TRUE"
|
||||
|
||||
@@ -11,22 +11,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsEmptyString(t *testing.T) {
|
||||
cases := []struct {
|
||||
s string
|
||||
expected bool
|
||||
}{
|
||||
{"", true},
|
||||
{" ", true},
|
||||
{" ", true},
|
||||
{" a", false},
|
||||
}
|
||||
|
||||
for _, v := range cases {
|
||||
assert.Equal(t, v.expected, IsEmptyString(v.s))
|
||||
}
|
||||
}
|
||||
|
||||
func Test_NormalizeEOL(t *testing.T) {
|
||||
data1 := []string{
|
||||
"",
|
||||
|
||||
@@ -50,7 +50,6 @@ func newFieldError(field reflect.StructField, cls, msg string) *BindingError {
|
||||
|
||||
// AddBindingRules adds additional binding rules
|
||||
func AddBindingRules(b *binding.Binder) {
|
||||
binding.JSONProvider = jsonProvider{}
|
||||
b.AddRuleNonZero("GitRefName", func(ctx context.Context, f *binding.ValidationField) *binding.Error {
|
||||
if !git.IsValidRefPattern(f.ValueMustString()) {
|
||||
return newFieldError(f.StructField, ErrGitRefName, "GitRefName")
|
||||
@@ -140,7 +139,7 @@ func validPort(p string) bool {
|
||||
}
|
||||
|
||||
var Binder = sync.OnceValue(func() *binding.Binder {
|
||||
b := binding.NewBinder().WithDefaultRules().WithNameMapper(util.ToSnakeCase)
|
||||
b := binding.NewBinder().WithJSONProvider(jsonProvider{}).WithDefaultRules().WithNameMapper(util.ToSnakeCase)
|
||||
AddBindingRules(b)
|
||||
return b
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user