Skip to content

Commit

Permalink
update readme and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 6, 2021
1 parent 44bd966 commit f0d3362
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ on:
jobs:

test:
name: Test on go ${{ matrix.go_version }} and ${{ matrix.os }}
runs-on: ${{ matrix.os }}
name: Test on go ${{ matrix.go_version }}
runs-on: ubuntu-latest
strategy:
matrix:
go_version: [1.13, 1.14, 1.15, 1.16]
os: [ubuntu-latest, windows-latest, macOS-latest]
go_version: [1.13, 1.14, 1.15, 1.16, 1.17]
# os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Check out code
Expand All @@ -39,7 +39,6 @@ jobs:

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
if: ${{ matrix.os == 'ubuntu-latest' }}
with:
path-to-profile: profile.cov
flag-name: Go-${{ matrix.go_version }}
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,28 @@ type UserForm struct {
Safe int `validate:"-"`
UpdateAt time.Time `validate:"required"`
Code string `validate:"customValidator"`
// nested struct
ExtInfo struct{
Homepage string `validate:"required"`
CityName string
}
}

// CustomValidator custom validator in the source struct.
func (f UserForm) CustomValidator(val string) bool {
return len(val) == 4
}

// ConfigValidation config the Validation
// eg:
// - define validate scenes
func (f UserForm) ConfigValidation(v *validate.Validation) {
v.WithScenes(validate.SValues{
"add": []string{"ExtInfo.Homepage", "Name", "Code"},
"update": []string{"ExtInfo.CityName", "Name"},
})
}

// Messages you can custom validator error messages.
func (f UserForm) Messages() map[string]string {
return validate.MS{
Expand All @@ -85,6 +100,7 @@ func (f UserForm) Translates() map[string]string {
return validate.MS{
"Name": "User Name",
"Email": "User Email",
"ExtInfo.Homepage": "Home Page",
}
}

Expand Down
15 changes: 15 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,27 @@ type UserForm struct {
Safe int `validate:"-"`
UpdateAt time.Time `validate:"required"`
Code string `validate:"customValidator"` // 使用自定义验证器
// 结构体嵌套
ExtInfo struct{
Homepage string `validate:"required"`
CityName string
}
}

// CustomValidator 定义在结构体中的自定义验证器
func (f UserForm) CustomValidator(val string) bool {
return len(val) == 4
}

// ConfigValidation 配置验证
// - 定义验证场景
func (f UserForm) ConfigValidation(v *validate.Validation) {
v.WithScenes(validate.SValues{
"add": []string{"ExtInfo.Homepage", "Name", "Code"},
"update": []string{"ExtInfo.CityName", "Name"},
})
}

// Messages 您可以自定义验证器错误消息
func (f UserForm) Messages() map[string]string {
return validate.MS{
Expand All @@ -81,6 +95,7 @@ func (f UserForm) Translates() map[string]string {
return validate.MS{
"Name": "用户名称",
"Email": "用户Email",
"ExtInfo.Homepage": "用户主页",
}
}

Expand Down
40 changes: 36 additions & 4 deletions validate_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package validate

import (
"fmt"
"reflect"
"testing"

"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -110,13 +110,13 @@ func TestOption(t *testing.T) {
ResetOption()
}

func Test_Struct_nilPtr_field2(t *testing.T) {
func TestStruct_nilPtr_field2(t *testing.T) {
type UserDto struct {
Name string `validate:"required"`
Sex *bool `validate:"required" json:"sex"`
}

// sex := true
sex := true
u := UserDto{
Name: "abc",
Sex: nil,
Expand All @@ -126,5 +126,37 @@ func Test_Struct_nilPtr_field2(t *testing.T) {
assert.False(t, v.Validate())
assert.True(t, v.Errors.HasField("Sex"))
assert.Contains(t, v.Errors.FieldOne("Sex"), "sex is required")
fmt.Println(v.Errors)
dump.Println(v.Errors)

u.Sex = &sex
v = Struct(&u)
assert.True(t, v.Validate())
}

func TestStruct_nexted_anonymity_struct(t *testing.T) {
type UserDto struct {
Name string `validate:"required"`
Sex *bool `validate:"required" json:"sex"`
ExtInfo struct{
Homepage string `validate:"required"`
CityName string
}
}

sex := true
u := &UserDto{
Name: "abc",
Sex: &sex,
}

v := Struct(u)
assert.False(t, v.Validate())
dump.Println(v.Errors)
assert.True(t, v.Errors.HasField("ExtInfo.Homepage"))
assert.Contains(t, v.Errors, "ExtInfo.Homepage")
assert.Equal(t, "ExtInfo.Homepage is required and not empty", v.Errors.FieldOne("ExtInfo.Homepage"))

u.ExtInfo.Homepage = "https://github.com/inhere"
v = Struct(u)
assert.True(t, v.Validate())
}

0 comments on commit f0d3362

Please sign in to comment.