Skip to content

Commit

Permalink
feat: validator add RegisterValidation for extend (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
veezhang authored May 22, 2023
1 parent a163bbf commit 43cb57b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
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

0 comments on commit 43cb57b

Please sign in to comment.