default_validator.go
This file
Path
binding/default_validator.go
97 lines · 2.3 KiB · Go
Last changed by
fix: modify interface check way (#3327)Jessebefore adoption51aea73
History of this fileTake it
The walk stopped after 200 commits: lines older than 9c27053 point at it.
chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f
line 1why, one step back1// Copyright 2017 Manu Martinez-Almeida. All rights reserved.byte alignment (#2774)Lancobefore adoption9c27053
lines 2–20why, one step back2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8 "fmt"9 "reflect"10 "strings"11 "sync"1213 "github.com/go-playground/validator/v10"14)1516type defaultValidator struct {17 once sync.Once18 validate *validator.Validate19}20Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c
line 21why, one step back21type SliceValidationError []errorbyte alignment (#2774)Lancobefore adoption9c27053
line 22why, one step back22Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c
lines 23–24why, one step back23// Error concatenates all error elements in SliceValidationError into a single string separated by \n.24func (err SliceValidationError) Error() string {byte alignment (#2774)Lancobefore adoption9c27053
lines 25–45why, one step back25 n := len(err)26 switch n {27 case 0:28 return ""29 default:30 var b strings.Builder31 if err[0] != nil {32 fmt.Fprintf(&b, "[%d]: %s", 0, err[0].Error())33 }34 if n > 1 {35 for i := 1; i < n; i++ {36 if err[i] != nil {37 b.WriteString("\n")38 fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error())39 }40 }41 }42 return b.String()43 }44}45fix: modify interface check way (#3327)Jessebefore adoption51aea73
line 46why, one step back46var _ StructValidator = (*defaultValidator)(nil)byte alignment (#2774)Lancobefore adoption9c27053
lines 47–48why, one step back4748// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.test support go1.18 (#2990)thinkeroubefore adoption2bde107
line 49why, one step back49func (v *defaultValidator) ValidateStruct(obj any) error {byte alignment (#2774)Lancobefore adoption9c27053
lines 50–61why, one step back50 if obj == nil {51 return nil52 }5354 value := reflect.ValueOf(obj)55 switch value.Kind() {56 case reflect.Ptr:57 return v.ValidateStruct(value.Elem().Interface())58 case reflect.Struct:59 return v.validateStruct(obj)60 case reflect.Slice, reflect.Array:61 count := value.Len()Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c
line 62why, one step back62 validateRet := make(SliceValidationError, 0)byte alignment (#2774)Lancobefore adoption9c27053
lines 63–77why, one step back63 for i := 0; i < count; i++ {64 if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {65 validateRet = append(validateRet, err)66 }67 }68 if len(validateRet) == 0 {69 return nil70 }71 return validateRet72 default:73 return nil74 }75}7677// validateStruct receives struct typetest support go1.18 (#2990)thinkeroubefore adoption2bde107
line 78why, one step back78func (v *defaultValidator) validateStruct(obj any) error {byte alignment (#2774)Lancobefore adoption9c27053
lines 79–86why, one step back79 v.lazyinit()80 return v.validate.Struct(obj)81}8283// Engine returns the underlying validator engine which powers the default84// Validator instance. This is useful if you want to register custom validations85// or struct level validations. See validator GoDoc for more info -86// https://pkg.go.dev/github.com/go-playground/validator/v10test support go1.18 (#2990)thinkeroubefore adoption2bde107
line 87why, one step back87func (v *defaultValidator) Engine() any {byte alignment (#2774)Lancobefore adoption9c27053
lines 88–97why, one step back88 v.lazyinit()89 return v.validate90}9192func (v *defaultValidator) lazyinit() {93 v.once.Do(func() {94 v.validate = validator.New()95 v.validate.SetTagName("binding")96 })97}