Skip to content

Commit bd57ae8

Browse files
committed
fix
1 parent eeeb5d7 commit bd57ae8

30 files changed

Lines changed: 331 additions & 737 deletions

modules/structs/form.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2026 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package structs
5+
6+
import (
7+
"net/http"
8+
9+
"gitea.dev/modules/reqctx"
10+
"gitea.dev/modules/translation/i18n"
11+
12+
"gitea.com/go-chi/binding"
13+
)
14+
15+
// ValidateContext is a special context for form validation middleware
16+
type ValidateContext struct {
17+
Locale i18n.LocaleTranslation
18+
Data reqctx.ContextData
19+
Req *http.Request
20+
Resp http.ResponseWriter
21+
}
22+
23+
type FormDefaultValidator struct{}
24+
25+
func (FormDefaultValidator) Validate(ctx *ValidateContext, errs binding.Errors) binding.Errors {
26+
// this default validator only needs to return the errs as is because the "binding" function has already validated
27+
return errs
28+
}

modules/structs/miscellaneous.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type SearchError struct {
2121

2222
// MarkupOption markup options
2323
type MarkupOption struct {
24+
FormDefaultValidator
2425
// Text markup to render
2526
//
2627
// in: body

modules/translation/i18n/i18n.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ type Locale interface {
1919
HasKey(trKey string) bool
2020
}
2121

22+
// LocaleTranslation represents an interface to translation
23+
type LocaleTranslation interface {
24+
Language() string
25+
HasKey(trKey string) bool
26+
TrString(string, ...any) string
27+
28+
Tr(key string, args ...any) template.HTML
29+
TrN(cnt any, key1, keyN string, args ...any) template.HTML
30+
31+
PrettyNumber(v any) string
32+
}
33+
2234
// LocaleStore provides the functions common to all locale stores
2335
type LocaleStore interface {
2436
io.Closer
@@ -31,7 +43,7 @@ type LocaleStore interface {
3143
Locale(langName string) (Locale, bool)
3244
// HasLang returns whether a given language is present in the store
3345
HasLang(langName string) bool
34-
// AddLocaleByIni adds a new language to the store
46+
// AddLocaleByJSON adds a new language to the store
3547
AddLocaleByJSON(langName, langDesc string, source, moreSource []byte) error
3648
}
3749

modules/translation/mock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type MockLocale struct {
1414
Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang
1515
}
1616

17+
func (l MockLocale) HasKey(trKey string) bool {
18+
return true
19+
}
20+
1721
var _ Locale = (*MockLocale)(nil)
1822

1923
func (l MockLocale) Language() string {

modules/translation/translation.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,7 @@ type contextKey struct{}
2525

2626
var ContextKey any = &contextKey{}
2727

28-
// Locale represents an interface to translation
29-
type Locale interface {
30-
Language() string
31-
TrString(string, ...any) string
32-
33-
Tr(key string, args ...any) template.HTML
34-
TrN(cnt any, key1, keyN string, args ...any) template.HTML
35-
36-
PrettyNumber(v any) string
37-
}
28+
type Locale = i18n.LocaleTranslation
3829

3930
// LangType represents a lang type
4031
type LangType struct {

modules/validation/binding_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ func performValidationTest(t *testing.T, testCase validationTestCase) {
3737
m := chi.NewRouter()
3838

3939
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
40-
actual := binding.Validate(req, testCase.data)
41-
// see https://github.com/stretchr/testify/issues/435
42-
if actual == nil {
43-
actual = binding.Errors{}
44-
}
45-
46-
assert.Equal(t, testCase.expectedErrors, actual)
40+
assert.Equal(t, testCase.expectedErrors, binding.Validate(req, testCase.data))
4741
})
4842

4943
req, err := http.NewRequest(http.MethodPost, testRoute, nil)

modules/validation/glob_pattern_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ func Test_GlobPatternValidation(t *testing.T) {
2929
data: TestForm{
3030
GlobPattern: "",
3131
},
32-
expectedErrors: binding.Errors{},
3332
},
3433
{
3534
description: "Valid glob",
3635
data: TestForm{
3736
GlobPattern: "{master,release*}",
3837
},
39-
expectedErrors: binding.Errors{},
4038
},
4139

4240
{

modules/validation/refname_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ func Test_GitRefNameValidation(t *testing.T) {
1717
data: TestForm{
1818
BranchName: "test",
1919
},
20-
expectedErrors: binding.Errors{},
2120
},
2221
{
2322
description: "Reference name contains single slash",
2423
data: TestForm{
2524
BranchName: "feature/test",
2625
},
27-
expectedErrors: binding.Errors{},
2826
},
2927
{
3028
description: "Reference name has allowed special characters",
3129
data: TestForm{
3230
BranchName: "debian/1%1.6.0-2",
3331
},
34-
expectedErrors: binding.Errors{},
3532
},
3633
{
3734
description: "Reference name contains backslash",

modules/validation/regex_pattern_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ func Test_RegexPatternValidation(t *testing.T) {
2626
data: TestForm{
2727
RegexPattern: "",
2828
},
29-
expectedErrors: binding.Errors{},
3029
},
3130
{
3231
description: "Valid regex",
3332
data: TestForm{
3433
RegexPattern: `(\d{1,3})+`,
3534
},
36-
expectedErrors: binding.Errors{},
3735
},
3836

3937
{

modules/validation/validurl_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,30 @@ func Test_ValidURLValidation(t *testing.T) {
1818
data: TestForm{
1919
URL: "",
2020
},
21-
expectedErrors: binding.Errors{},
2221
},
2322
{
2423
description: "URL without port",
2524
data: TestForm{
2625
URL: "http://test.lan/",
2726
},
28-
expectedErrors: binding.Errors{},
2927
},
3028
{
3129
description: "URL with port",
3230
data: TestForm{
3331
URL: "http://test.lan:3000/",
3432
},
35-
expectedErrors: binding.Errors{},
3633
},
3734
{
3835
description: "URL with IPv6 address without port",
3936
data: TestForm{
4037
URL: "http://[::1]/",
4138
},
42-
expectedErrors: binding.Errors{},
4339
},
4440
{
4541
description: "URL with IPv6 address with port",
4642
data: TestForm{
4743
URL: "http://[::1]:3000/",
4844
},
49-
expectedErrors: binding.Errors{},
5045
},
5146
{
5247
description: "Invalid URL",

0 commit comments

Comments
 (0)