[Roll] interface name changing

This commit is contained in:
wuhuizuo 2020-03-26 07:55:35 +00:00
parent 50f5b16990
commit abced1b159
2 changed files with 10 additions and 10 deletions

View File

@ -45,27 +45,27 @@ type BindingUri interface {
BindUri(map[string][]string, interface{}) error
}
// Validater is the minimal interface which needs to be implemented in
// StructValidator is the minimal interface which needs to be implemented in
// order for it to be used as the validator engine for ensuring the correctness
// of the request. Gin provides a default implementation for this using
// https://github.com/go-playground/validator/tree/v8.18.2.
type Validater interface {
type StructValidator interface {
// If the received type is a slice|array, the validation should be performed travel on every element.
// If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned.
// If the received type is a struct or pointer to a struct, the validation should be performed.
// If the struct is not valid or the validation itself fails, a descriptive error should be returned.
// Otherwise nil must be returned.
Validate(interface{}) error
ValidateStruct(interface{}) error
// Engine returns the underlying validator engine which powers the
// StructValidator implementation.
// Validater implementation.
Engine() interface{}
}
// Validator is the default validator which implements the StructValidator
// interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
// under the hood.
var Validator Validater = &defaultValidator{}
var Validator StructValidator = &defaultValidator{}
// These implement the Binding interface and can be used to bind the data
// present in the request to struct instances.
@ -112,5 +112,5 @@ func validate(obj interface{}) error {
if Validator == nil {
return nil
}
return Validator.Validate(obj)
return Validator.ValidateStruct(obj)
}

View File

@ -31,10 +31,10 @@ func (err sliceValidateError) Error() string {
return strings.Join(errMsgs, "\n")
}
var _ Validater = &defaultValidator{}
var _ StructValidator = &defaultValidator{}
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
func (v *defaultValidator) Validate(obj interface{}) error {
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if obj == nil {
return nil
}
@ -42,7 +42,7 @@ func (v *defaultValidator) Validate(obj interface{}) error {
value := reflect.ValueOf(obj)
valueType := value.Kind()
if valueType == reflect.Ptr {
return v.Validate(value.Elem().Interface())
return v.ValidateStruct(value.Elem().Interface())
}
switch valueType {
@ -52,7 +52,7 @@ func (v *defaultValidator) Validate(obj interface{}) error {
count := value.Len()
validateRet := make(sliceValidateError, 0)
for i := 0; i < count; i++ {
if err := v.Validate(value.Index(i).Interface()); err != nil {
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
validateRet = append(validateRet, err)
}
}