mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-16 04:05:00 +00:00
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:
@@ -37,13 +37,7 @@ func performValidationTest(t *testing.T, testCase validationTestCase) {
|
||||
m := chi.NewRouter()
|
||||
|
||||
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
|
||||
actual := binding.Validate(req, testCase.data)
|
||||
// see https://github.com/stretchr/testify/issues/435
|
||||
if actual == nil {
|
||||
actual = binding.Errors{}
|
||||
}
|
||||
|
||||
assert.Equal(t, testCase.expectedErrors, actual)
|
||||
assert.Equal(t, testCase.expectedErrors, binding.Validate(req, testCase.data))
|
||||
})
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, testRoute, nil)
|
||||
|
||||
@@ -29,14 +29,12 @@ func Test_GlobPatternValidation(t *testing.T) {
|
||||
data: TestForm{
|
||||
GlobPattern: "",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Valid glob",
|
||||
data: TestForm{
|
||||
GlobPattern: "{master,release*}",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
|
||||
{
|
||||
|
||||
@@ -17,21 +17,18 @@ func Test_GitRefNameValidation(t *testing.T) {
|
||||
data: TestForm{
|
||||
BranchName: "test",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Reference name contains single slash",
|
||||
data: TestForm{
|
||||
BranchName: "feature/test",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Reference name has allowed special characters",
|
||||
data: TestForm{
|
||||
BranchName: "debian/1%1.6.0-2",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Reference name contains backslash",
|
||||
|
||||
@@ -26,14 +26,12 @@ func Test_RegexPatternValidation(t *testing.T) {
|
||||
data: TestForm{
|
||||
RegexPattern: "",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Valid regex",
|
||||
data: TestForm{
|
||||
RegexPattern: `(\d{1,3})+`,
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
|
||||
{
|
||||
|
||||
@@ -18,35 +18,30 @@ func Test_ValidURLValidation(t *testing.T) {
|
||||
data: TestForm{
|
||||
URL: "",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "URL without port",
|
||||
data: TestForm{
|
||||
URL: "http://test.lan/",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "URL with port",
|
||||
data: TestForm{
|
||||
URL: "http://test.lan:3000/",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "URL with IPv6 address without port",
|
||||
data: TestForm{
|
||||
URL: "http://[::1]/",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "URL with IPv6 address with port",
|
||||
data: TestForm{
|
||||
URL: "http://[::1]:3000/",
|
||||
},
|
||||
expectedErrors: binding.Errors{},
|
||||
},
|
||||
{
|
||||
description: "Invalid URL",
|
||||
|
||||
Reference in New Issue
Block a user