From f0d336293a6de076361bbd5b31f59e7ed68212f8 Mon Sep 17 00:00:00 2001 From: Inhere Date: Wed, 6 Oct 2021 11:44:57 +0800 Subject: [PATCH] update readme and add more tests --- .github/workflows/go.yml | 9 ++++----- README.md | 16 ++++++++++++++++ README.zh-CN.md | 15 +++++++++++++++ validate_test.go | 40 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fc03073..f183b77 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 @@ -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 }} diff --git a/README.md b/README.md index 1e80df6..a95cfb9 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,11 @@ 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. @@ -72,6 +77,16 @@ 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{ @@ -85,6 +100,7 @@ func (f UserForm) Translates() map[string]string { return validate.MS{ "Name": "User Name", "Email": "User Email", + "ExtInfo.Homepage": "Home Page", } } diff --git a/README.zh-CN.md b/README.zh-CN.md index a4fecc1..baf7b1a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -61,6 +61,11 @@ type UserForm struct { Safe int `validate:"-"` UpdateAt time.Time `validate:"required"` Code string `validate:"customValidator"` // 使用自定义验证器 + // 结构体嵌套 + ExtInfo struct{ + Homepage string `validate:"required"` + CityName string + } } // CustomValidator 定义在结构体中的自定义验证器 @@ -68,6 +73,15 @@ 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{ @@ -81,6 +95,7 @@ func (f UserForm) Translates() map[string]string { return validate.MS{ "Name": "用户名称", "Email": "用户Email", + "ExtInfo.Homepage": "用户主页", } } diff --git a/validate_test.go b/validate_test.go index 7a9473a..25da822 100644 --- a/validate_test.go +++ b/validate_test.go @@ -1,10 +1,10 @@ package validate import ( - "fmt" "reflect" "testing" + "github.com/gookit/goutil/dump" "github.com/stretchr/testify/assert" ) @@ -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, @@ -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()) }