-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package conf | ||
|
||
import "github.com/zeromicro/go-zero/core/validation" | ||
|
||
// validate validates the value if it implements the Validator interface. | ||
func validate(v any) error { | ||
if val, ok := v.(validation.Validator); ok { | ||
return val.Validate() | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package conf | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockType int | ||
|
||
func (m mockType) Validate() error { | ||
if m < 10 { | ||
return errors.New("invalid value") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type anotherMockType int | ||
|
||
func Test_validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
v any | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "invalid", | ||
v: mockType(5), | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "valid", | ||
v: mockType(10), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "not validator", | ||
v: anotherMockType(5), | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := validate(tt.v) | ||
assert.Equal(t, tt.wantErr, err != nil) | ||
}) | ||
} | ||
} | ||
|
||
type mockVal struct { | ||
} | ||
|
||
func (m mockVal) Validate() error { | ||
return errors.New("invalid value") | ||
} | ||
|
||
func Test_validateValPtr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
v any | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "invalid", | ||
v: mockVal{}, | ||
}, | ||
{ | ||
name: "invalid value", | ||
v: &mockVal{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Error(t, validate(tt.v)) | ||
}) | ||
} | ||
} |