-
Notifications
You must be signed in to change notification settings - Fork 2
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
7 changed files
with
196 additions
and
3 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
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,9 @@ | ||
package validator | ||
|
||
import ( | ||
govalidator "github.com/go-playground/validator/v10" | ||
) | ||
|
||
var extendValidators = map[string]govalidator.Func{ | ||
// add your extend extend validators | ||
} |
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,9 @@ | ||
package validator | ||
|
||
const ( | ||
// add your regex string here | ||
) | ||
|
||
var ( | ||
// add your regex here with MustCompile | ||
) |
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,61 @@ | ||
package validator | ||
|
||
import ( | ||
"sync" | ||
|
||
govalidator "github.com/go-playground/validator/v10" | ||
) | ||
|
||
var ( | ||
gValidator Validator = (*defaultValidator)(nil) | ||
gValidatorInit sync.Once | ||
) | ||
|
||
type ( | ||
Validator interface { | ||
Struct(s interface{}) error | ||
Var(field interface{}, tag string) error | ||
} | ||
|
||
defaultValidator struct { | ||
*govalidator.Validate | ||
} | ||
|
||
// alias | ||
InvalidValidationError = govalidator.InvalidValidationError | ||
ValidationErrors = govalidator.ValidationErrors | ||
) | ||
|
||
func New() Validator { | ||
validate := govalidator.New() | ||
for k, val := range extendValidators { | ||
_ = validate.RegisterValidation(k, val) | ||
} | ||
return &defaultValidator{ | ||
Validate: validate, | ||
} | ||
} | ||
|
||
func Struct(s interface{}) error { | ||
initGValidator() | ||
return gValidator.Struct(s) | ||
} | ||
|
||
func Var(field interface{}, tag string) error { | ||
initGValidator() | ||
return gValidator.Var(field, tag) | ||
} | ||
|
||
func (v *defaultValidator) Struct(s interface{}) error { | ||
return v.Validate.Struct(s) | ||
} | ||
|
||
func (v *defaultValidator) Var(field interface{}, tag string) error { | ||
return v.Validate.Var(field, tag) | ||
} | ||
|
||
func initGValidator() { | ||
gValidatorInit.Do(func() { | ||
gValidator = New() | ||
}) | ||
} |
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,83 @@ | ||
package validator | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
govalidator "github.com/go-playground/validator/v10" | ||
"github.com/prashantv/gostub" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStruct(t *testing.T) { | ||
var ( | ||
err error | ||
ast = assert.New(t) | ||
) | ||
|
||
type testStruct struct { | ||
Data int `validate:"min=1,max=10"` | ||
} | ||
err = Struct(testStruct{}) | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Struct(testStruct{Data: -1}) | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Struct(testStruct{Data: 11}) | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Struct(testStruct{Data: 3}) | ||
ast.NoError(err) | ||
} | ||
|
||
func TestVar(t *testing.T) { | ||
var ( | ||
err error | ||
ast = assert.New(t) | ||
) | ||
err = Var(0, "min=1,max=10") | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Var(-1, "min=1,max=10") | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Var(11, "min=1,max=10") | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = Var(3, "min=1,max=10") | ||
ast.NoError(err) | ||
} | ||
|
||
func TestExtendValidators(t *testing.T) { | ||
stubs := gostub.Stub(&extendValidators, map[string]govalidator.Func{ | ||
"streq": func(fl govalidator.FieldLevel) bool { | ||
field := fl.Field() | ||
param := fl.Param() | ||
if field.Kind() == reflect.String { | ||
return field.String() == param | ||
} | ||
return false | ||
}, | ||
}) | ||
defer stubs.Reset() | ||
|
||
var ( | ||
err error | ||
ast = assert.New(t) | ||
v = New() | ||
) | ||
|
||
err = v.Var("aa", "streq=aaa") | ||
if errs, ok := err.(govalidator.ValidationErrors); ast.True(ok) { | ||
ast.Len(errs, 1) | ||
} | ||
err = v.Var("aaa", "streq=aaa") | ||
ast.NoError(err) | ||
} |