mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-17 23:33:43 +08:00
117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package binding
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type defaultValidator struct {
|
|
once sync.Once
|
|
validate *validator.Validate
|
|
}
|
|
|
|
type SliceValidationError []error
|
|
|
|
type indexedValidationError struct {
|
|
error
|
|
index int
|
|
}
|
|
|
|
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
|
|
func (err SliceValidationError) Error() string {
|
|
if len(err) == 0 {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
for i := range len(err) {
|
|
if err[i] != nil {
|
|
index := i
|
|
if indexed, ok := errors.AsType[indexedValidationError](err[i]); ok {
|
|
index = indexed.index
|
|
}
|
|
if b.Len() > 0 {
|
|
b.WriteString("\n")
|
|
}
|
|
b.WriteString("[")
|
|
b.WriteString(strconv.Itoa(index))
|
|
b.WriteString("]: ")
|
|
b.WriteString(err[i].Error())
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// Unwrap returns the original validation error.
|
|
func (err indexedValidationError) Unwrap() error {
|
|
return err.error
|
|
}
|
|
|
|
var _ StructValidator = (*defaultValidator)(nil)
|
|
|
|
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
|
|
func (v *defaultValidator) ValidateStruct(obj any) error {
|
|
if obj == nil {
|
|
return nil
|
|
}
|
|
|
|
value := reflect.ValueOf(obj)
|
|
switch value.Kind() {
|
|
case reflect.Ptr:
|
|
if value.Elem().Kind() != reflect.Struct {
|
|
return v.ValidateStruct(value.Elem().Interface())
|
|
}
|
|
return v.validateStruct(obj)
|
|
case reflect.Struct:
|
|
return v.validateStruct(obj)
|
|
case reflect.Slice, reflect.Array:
|
|
count := value.Len()
|
|
validateRet := make(SliceValidationError, 0)
|
|
for i := range count {
|
|
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
|
|
if i != len(validateRet) {
|
|
err = indexedValidationError{error: err, index: i}
|
|
}
|
|
validateRet = append(validateRet, err)
|
|
}
|
|
}
|
|
if len(validateRet) == 0 {
|
|
return nil
|
|
}
|
|
return validateRet
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// validateStruct receives struct type
|
|
func (v *defaultValidator) validateStruct(obj any) error {
|
|
v.lazyinit()
|
|
return v.validate.Struct(obj)
|
|
}
|
|
|
|
// Engine returns the underlying validator engine which powers the default
|
|
// Validator instance. This is useful if you want to register custom validations
|
|
// or struct level validations. See validator GoDoc for more info -
|
|
// https://pkg.go.dev/github.com/go-playground/validator/v10
|
|
func (v *defaultValidator) Engine() any {
|
|
v.lazyinit()
|
|
return v.validate
|
|
}
|
|
|
|
func (v *defaultValidator) lazyinit() {
|
|
v.once.Do(func() {
|
|
v.validate = validator.New()
|
|
v.validate.SetTagName("binding")
|
|
})
|
|
}
|