2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
2017-06-12 09:04:52 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-05-31 17:18:50 +03:00
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2024-05-13 08:32:46 +03:00
|
|
|
"strconv"
|
2021-01-03 16:43:34 +03:00
|
|
|
"strings"
|
2015-05-31 17:18:50 +03:00
|
|
|
"sync"
|
|
|
|
|
2019-11-25 09:49:45 +03:00
|
|
|
"github.com/go-playground/validator/v10"
|
2015-05-31 17:18:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type defaultValidator struct {
|
|
|
|
once sync.Once
|
|
|
|
validate *validator.Validate
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:45:03 +03:00
|
|
|
type SliceValidationError []error
|
2021-01-03 16:43:34 +03:00
|
|
|
|
2021-11-21 16:45:03 +03:00
|
|
|
// Error concatenates all error elements in SliceValidationError into a single string separated by \n.
|
|
|
|
func (err SliceValidationError) Error() string {
|
2024-05-13 08:32:46 +03:00
|
|
|
if len(err) == 0 {
|
2021-06-28 17:05:29 +03:00
|
|
|
return ""
|
2024-05-13 08:32:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
for i := 0; i < len(err); i++ {
|
|
|
|
if err[i] != nil {
|
|
|
|
if b.Len() > 0 {
|
|
|
|
b.WriteString("\n")
|
2021-06-28 17:05:29 +03:00
|
|
|
}
|
2024-05-13 08:32:46 +03:00
|
|
|
b.WriteString("[" + strconv.Itoa(i) + "]: " + err[i].Error())
|
2021-06-28 17:05:29 +03:00
|
|
|
}
|
2021-01-03 16:43:34 +03:00
|
|
|
}
|
2024-05-13 08:32:46 +03:00
|
|
|
return b.String()
|
2021-01-03 16:43:34 +03:00
|
|
|
}
|
|
|
|
|
2022-10-19 19:49:19 +03:00
|
|
|
var _ StructValidator = (*defaultValidator)(nil)
|
2015-05-31 17:18:50 +03:00
|
|
|
|
2018-07-01 16:10:48 +03:00
|
|
|
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
|
2022-03-21 04:43:17 +03:00
|
|
|
func (v *defaultValidator) ValidateStruct(obj any) error {
|
2021-01-03 16:43:34 +03:00
|
|
|
if obj == nil {
|
|
|
|
return nil
|
2018-07-01 16:10:48 +03:00
|
|
|
}
|
2021-01-03 16:43:34 +03:00
|
|
|
|
|
|
|
value := reflect.ValueOf(obj)
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Ptr:
|
2024-02-07 14:44:11 +03:00
|
|
|
if value.Elem().Kind() != reflect.Struct {
|
|
|
|
return v.ValidateStruct(value.Elem().Interface())
|
|
|
|
}
|
|
|
|
return v.validateStruct(obj)
|
2021-01-03 16:43:34 +03:00
|
|
|
case reflect.Struct:
|
|
|
|
return v.validateStruct(obj)
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
count := value.Len()
|
2021-11-21 16:45:03 +03:00
|
|
|
validateRet := make(SliceValidationError, 0)
|
2021-01-03 16:43:34 +03:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
|
|
|
|
validateRet = append(validateRet, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(validateRet) == 0 {
|
|
|
|
return nil
|
2015-05-31 17:30:00 +03:00
|
|
|
}
|
2021-01-03 16:43:34 +03:00
|
|
|
return validateRet
|
|
|
|
default:
|
|
|
|
return nil
|
2015-05-31 17:18:50 +03:00
|
|
|
}
|
2021-01-03 16:43:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateStruct receives struct type
|
2022-03-21 04:43:17 +03:00
|
|
|
func (v *defaultValidator) validateStruct(obj any) error {
|
2021-01-03 16:43:34 +03:00
|
|
|
v.lazyinit()
|
|
|
|
return v.validate.Struct(obj)
|
2015-05-31 17:18:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-29 09:33:07 +03:00
|
|
|
// 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 -
|
2021-05-26 13:46:13 +03:00
|
|
|
// https://pkg.go.dev/github.com/go-playground/validator/v10
|
2022-03-21 04:43:17 +03:00
|
|
|
func (v *defaultValidator) Engine() any {
|
2017-08-27 10:37:39 +03:00
|
|
|
v.lazyinit()
|
2018-03-29 09:33:07 +03:00
|
|
|
return v.validate
|
2017-08-27 10:37:39 +03:00
|
|
|
}
|
|
|
|
|
2015-05-31 17:18:50 +03:00
|
|
|
func (v *defaultValidator) lazyinit() {
|
|
|
|
v.once.Do(func() {
|
2019-09-05 16:39:56 +03:00
|
|
|
v.validate = validator.New()
|
|
|
|
v.validate.SetTagName("binding")
|
2015-05-31 17:18:50 +03:00
|
|
|
})
|
|
|
|
}
|