Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: validator add RegisterValidation for extend #32

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions validator/extend.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package validator

import (
govalidator "github.com/go-playground/validator/v10"
)

var extendValidators = map[string]govalidator.Func{
var extendValidators = map[string]Func{
// add your extend extend validators
}
24 changes: 19 additions & 5 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (

type (
Validator interface {
RegisterValidation(tag string, fn Func, callValidationEvenIfNull ...bool) error
Struct(s interface{}) error
Var(field interface{}, tag string) error
}
Expand All @@ -22,18 +23,27 @@ type (
}

// alias
FieldLevel = govalidator.FieldLevel
Func = func(fl FieldLevel) bool
InvalidValidationError = govalidator.InvalidValidationError
ValidationErrors = govalidator.ValidationErrors
)

func New() Validator {
validate := govalidator.New()
for k, val := range extendValidators {
_ = validate.RegisterValidation(k, val)
v := &defaultValidator{
Validate: govalidator.New(),
}
return &defaultValidator{
Validate: validate,

for k, val := range extendValidators {
_ = v.RegisterValidation(k, val)
}

return v
}

func RegisterValidation(tag string, fn Func, callValidationEvenIfNull ...bool) error {
initGValidator()
return gValidator.RegisterValidation(tag, fn, callValidationEvenIfNull...)
}

func Struct(s interface{}) error {
Expand All @@ -46,6 +56,10 @@ func Var(field interface{}, tag string) error {
return gValidator.Var(field, tag)
}

func (v *defaultValidator) RegisterValidation(tag string, fn Func, callValidationEvenIfNull ...bool) error {
return v.Validate.RegisterValidation(tag, fn, callValidationEvenIfNull...)
}

func (v *defaultValidator) Struct(s interface{}) error {
return v.Validate.Struct(s)
}
Expand Down
34 changes: 32 additions & 2 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ import (
"github.com/stretchr/testify/assert"
)

func TestRegisterValidation(t *testing.T) {
var (
err error
ast = assert.New(t)
)

_ = RegisterValidation("streq", func(fl FieldLevel) bool {
field := fl.Field()
param := fl.Param()
if field.Kind() == reflect.String {
return field.String() == param
}
return false
})

type testStruct struct {
Data string `validate:"streq=aaa"`
}
err = Struct(testStruct{})
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) {
ast.Len(errs, 1)
}
err = Struct(testStruct{Data: "aa"})
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) {
ast.Len(errs, 1)
}
err = Struct(testStruct{Data: "aaa"})
ast.NoError(err)
}

func TestStruct(t *testing.T) {
var (
err error
Expand Down Expand Up @@ -56,8 +86,8 @@ func TestVar(t *testing.T) {
}

func TestExtendValidators(t *testing.T) {
stubs := gostub.Stub(&extendValidators, map[string]govalidator.Func{
"streq": func(fl govalidator.FieldLevel) bool {
stubs := gostub.Stub(&extendValidators, map[string]Func{
"streq": func(fl FieldLevel) bool {
field := fl.Field()
param := fl.Param()
if field.Kind() == reflect.String {
Expand Down