处理AI胡乱生成的乱摊子
This commit is contained in:
216
vendor/github.com/go-playground/validator/v10/validator.go
generated
vendored
216
vendor/github.com/go-playground/validator/v10/validator.go
generated
vendored
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// per validate construct
|
||||
@@ -31,14 +32,12 @@ type validate struct {
|
||||
|
||||
// parent and current will be the same the first run of validateStruct
|
||||
func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, structNs []byte, ct *cTag) {
|
||||
|
||||
cs, ok := v.v.structCache.Get(typ)
|
||||
if !ok {
|
||||
cs = v.v.extractStructCache(current, typ.Name())
|
||||
}
|
||||
|
||||
if len(ns) == 0 && len(cs.name) != 0 {
|
||||
|
||||
ns = append(ns, cs.name...)
|
||||
ns = append(ns, '.')
|
||||
|
||||
@@ -49,21 +48,17 @@ func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, cur
|
||||
// ct is nil on top level struct, and structs as fields that have no tag info
|
||||
// so if nil or if not nil and the structonly tag isn't present
|
||||
if ct == nil || ct.typeof != typeStructOnly {
|
||||
|
||||
var f *cField
|
||||
|
||||
for i := 0; i < len(cs.fields); i++ {
|
||||
|
||||
f = cs.fields[i]
|
||||
|
||||
if v.isPartial {
|
||||
|
||||
if v.ffn != nil {
|
||||
// used with StructFiltered
|
||||
if v.ffn(append(structNs, f.name...)) {
|
||||
continue
|
||||
}
|
||||
|
||||
} else {
|
||||
// used with StructPartial & StructExcept
|
||||
_, ok = v.includeExclude[string(append(structNs, f.name...))]
|
||||
@@ -82,7 +77,6 @@ func (v *validate) validateStruct(ctx context.Context, parent reflect.Value, cur
|
||||
// first iteration will have no info about nostructlevel tag, and is checked prior to
|
||||
// calling the next iteration of validateStruct called from traverseField.
|
||||
if cs.fn != nil {
|
||||
|
||||
v.slflParent = parent
|
||||
v.slCurrent = current
|
||||
v.ns = ns
|
||||
@@ -99,6 +93,8 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
|
||||
|
||||
current, kind, v.fldIsPointer = v.extractTypeInternal(current, false)
|
||||
|
||||
var isNestedStruct bool
|
||||
|
||||
switch kind {
|
||||
case reflect.Ptr, reflect.Interface, reflect.Invalid:
|
||||
|
||||
@@ -110,6 +106,14 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
|
||||
return
|
||||
}
|
||||
|
||||
if ct.typeof == typeOmitNil && (kind != reflect.Invalid && current.IsNil()) {
|
||||
return
|
||||
}
|
||||
|
||||
if ct.typeof == typeOmitZero {
|
||||
return
|
||||
}
|
||||
|
||||
if ct.hasTag {
|
||||
if kind == reflect.Invalid {
|
||||
v.str1 = string(append(ns, cf.altName...))
|
||||
@@ -150,7 +154,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
|
||||
structNs: v.str2,
|
||||
fieldLen: uint8(len(cf.altName)),
|
||||
structfieldLen: uint8(len(cf.name)),
|
||||
value: current.Interface(),
|
||||
value: getValue(current),
|
||||
param: ct.param,
|
||||
kind: kind,
|
||||
typ: current.Type(),
|
||||
@@ -160,86 +164,61 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
|
||||
}
|
||||
}
|
||||
|
||||
case reflect.Struct:
|
||||
|
||||
typ = current.Type()
|
||||
|
||||
if !typ.ConvertibleTo(timeType) {
|
||||
|
||||
if ct != nil {
|
||||
|
||||
if ct.typeof == typeStructOnly {
|
||||
goto CONTINUE
|
||||
} else if ct.typeof == typeIsDefault {
|
||||
// set Field Level fields
|
||||
v.slflParent = parent
|
||||
v.flField = current
|
||||
v.cf = cf
|
||||
v.ct = ct
|
||||
|
||||
if !ct.fn(ctx, v) {
|
||||
v.str1 = string(append(ns, cf.altName...))
|
||||
|
||||
if v.v.hasTagNameFunc {
|
||||
v.str2 = string(append(structNs, cf.name...))
|
||||
} else {
|
||||
v.str2 = v.str1
|
||||
}
|
||||
|
||||
v.errs = append(v.errs,
|
||||
&fieldError{
|
||||
v: v.v,
|
||||
tag: ct.aliasTag,
|
||||
actualTag: ct.tag,
|
||||
ns: v.str1,
|
||||
structNs: v.str2,
|
||||
fieldLen: uint8(len(cf.altName)),
|
||||
structfieldLen: uint8(len(cf.name)),
|
||||
value: current.Interface(),
|
||||
param: ct.param,
|
||||
kind: kind,
|
||||
typ: typ,
|
||||
},
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ct = ct.next
|
||||
}
|
||||
|
||||
if ct != nil && ct.typeof == typeNoStructLevel {
|
||||
return
|
||||
}
|
||||
|
||||
CONTINUE:
|
||||
// if len == 0 then validating using 'Var' or 'VarWithValue'
|
||||
// Var - doesn't make much sense to do it that way, should call 'Struct', but no harm...
|
||||
// VarWithField - this allows for validating against each field within the struct against a specific value
|
||||
// pretty handy in certain situations
|
||||
if len(cf.name) > 0 {
|
||||
ns = append(append(ns, cf.altName...), '.')
|
||||
structNs = append(append(structNs, cf.name...), '.')
|
||||
}
|
||||
|
||||
v.validateStruct(ctx, parent, current, typ, ns, structNs, ct)
|
||||
if kind == reflect.Invalid {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if ct == nil || !ct.hasTag {
|
||||
return
|
||||
case reflect.Struct:
|
||||
isNestedStruct = !current.Type().ConvertibleTo(timeType)
|
||||
// For backward compatibility before struct level validation tags were supported
|
||||
// as there were a number of projects relying on `required` not failing on non-pointer
|
||||
// structs. Since it's basically nonsensical to use `required` with a non-pointer struct
|
||||
// are explicitly skipping the required validation for it. This WILL be removed in the
|
||||
// next major version.
|
||||
if isNestedStruct && !v.v.requiredStructEnabled && ct != nil && ct.tag == requiredTag {
|
||||
ct = ct.next
|
||||
}
|
||||
}
|
||||
|
||||
typ = current.Type()
|
||||
|
||||
OUTER:
|
||||
for {
|
||||
if ct == nil {
|
||||
if ct == nil || !ct.hasTag || (isNestedStruct && len(cf.name) == 0) {
|
||||
// isNestedStruct check here
|
||||
if isNestedStruct {
|
||||
// if len == 0 then validating using 'Var' or 'VarWithValue'
|
||||
// Var - doesn't make much sense to do it that way, should call 'Struct', but no harm...
|
||||
// VarWithField - this allows for validating against each field within the struct against a specific value
|
||||
// pretty handy in certain situations
|
||||
if len(cf.name) > 0 {
|
||||
ns = append(append(ns, cf.altName...), '.')
|
||||
structNs = append(append(structNs, cf.name...), '.')
|
||||
}
|
||||
|
||||
v.validateStruct(ctx, parent, current, typ, ns, structNs, ct)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
switch ct.typeof {
|
||||
case typeNoStructLevel:
|
||||
return
|
||||
|
||||
case typeStructOnly:
|
||||
if isNestedStruct {
|
||||
// if len == 0 then validating using 'Var' or 'VarWithValue'
|
||||
// Var - doesn't make much sense to do it that way, should call 'Struct', but no harm...
|
||||
// VarWithField - this allows for validating against each field within the struct against a specific value
|
||||
// pretty handy in certain situations
|
||||
if len(cf.name) > 0 {
|
||||
ns = append(append(ns, cf.altName...), '.')
|
||||
structNs = append(append(structNs, cf.name...), '.')
|
||||
}
|
||||
|
||||
v.validateStruct(ctx, parent, current, typ, ns, structNs, ct)
|
||||
}
|
||||
return
|
||||
|
||||
case typeOmitEmpty:
|
||||
|
||||
@@ -256,6 +235,39 @@ OUTER:
|
||||
ct = ct.next
|
||||
continue
|
||||
|
||||
case typeOmitZero:
|
||||
v.slflParent = parent
|
||||
v.flField = current
|
||||
v.cf = cf
|
||||
v.ct = ct
|
||||
|
||||
if !hasNotZeroValue(v) {
|
||||
return
|
||||
}
|
||||
|
||||
ct = ct.next
|
||||
continue
|
||||
|
||||
case typeOmitNil:
|
||||
v.slflParent = parent
|
||||
v.flField = current
|
||||
v.cf = cf
|
||||
v.ct = ct
|
||||
|
||||
switch field := v.Field(); field.Kind() {
|
||||
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
|
||||
if field.IsNil() {
|
||||
return
|
||||
}
|
||||
default:
|
||||
if v.fldIsPointer && getValue(field) == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ct = ct.next
|
||||
continue
|
||||
|
||||
case typeEndKeys:
|
||||
return
|
||||
|
||||
@@ -272,7 +284,6 @@ OUTER:
|
||||
reusableCF := &cField{}
|
||||
|
||||
for i := 0; i < current.Len(); i++ {
|
||||
|
||||
i64 = int64(i)
|
||||
|
||||
v.misc = append(v.misc[0:0], cf.name...)
|
||||
@@ -285,7 +296,6 @@ OUTER:
|
||||
if cf.namesEqual {
|
||||
reusableCF.altName = reusableCF.name
|
||||
} else {
|
||||
|
||||
v.misc = append(v.misc[0:0], cf.altName...)
|
||||
v.misc = append(v.misc, '[')
|
||||
v.misc = strconv.AppendInt(v.misc, i64, 10)
|
||||
@@ -302,8 +312,7 @@ OUTER:
|
||||
reusableCF := &cField{}
|
||||
|
||||
for _, key := range current.MapKeys() {
|
||||
|
||||
pv = fmt.Sprintf("%v", key.Interface())
|
||||
pv = fmt.Sprintf("%v", key)
|
||||
|
||||
v.misc = append(v.misc[0:0], cf.name...)
|
||||
v.misc = append(v.misc, '[')
|
||||
@@ -328,6 +337,18 @@ OUTER:
|
||||
// can be nil when just keys being validated
|
||||
if ct.next != nil {
|
||||
v.traverseField(ctx, parent, current.MapIndex(key), ns, structNs, reusableCF, ct.next)
|
||||
} else {
|
||||
// Struct fallback when map values are structs
|
||||
val := current.MapIndex(key)
|
||||
switch val.Kind() {
|
||||
case reflect.Ptr:
|
||||
if val.Elem().Kind() == reflect.Struct {
|
||||
// Dive into the struct so its own tags run
|
||||
v.traverseField(ctx, parent, val, ns, structNs, reusableCF, nil)
|
||||
}
|
||||
case reflect.Struct:
|
||||
v.traverseField(ctx, parent, val, ns, structNs, reusableCF, nil)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
v.traverseField(ctx, parent, current.MapIndex(key), ns, structNs, reusableCF, ct)
|
||||
@@ -347,7 +368,6 @@ OUTER:
|
||||
v.misc = v.misc[0:0]
|
||||
|
||||
for {
|
||||
|
||||
// set Field Level fields
|
||||
v.slflParent = parent
|
||||
v.flField = current
|
||||
@@ -362,11 +382,10 @@ OUTER:
|
||||
|
||||
// drain rest of the 'or' values, then continue or leave
|
||||
for {
|
||||
|
||||
ct = ct.next
|
||||
|
||||
if ct == nil {
|
||||
return
|
||||
continue OUTER
|
||||
}
|
||||
|
||||
if ct.typeof != typeOr {
|
||||
@@ -399,7 +418,6 @@ OUTER:
|
||||
}
|
||||
|
||||
if ct.hasAlias {
|
||||
|
||||
v.errs = append(v.errs,
|
||||
&fieldError{
|
||||
v: v.v,
|
||||
@@ -409,15 +427,13 @@ OUTER:
|
||||
structNs: v.str2,
|
||||
fieldLen: uint8(len(cf.altName)),
|
||||
structfieldLen: uint8(len(cf.name)),
|
||||
value: current.Interface(),
|
||||
value: getValue(current),
|
||||
param: ct.param,
|
||||
kind: kind,
|
||||
typ: typ,
|
||||
},
|
||||
)
|
||||
|
||||
} else {
|
||||
|
||||
tVal := string(v.misc)[1:]
|
||||
|
||||
v.errs = append(v.errs,
|
||||
@@ -429,7 +445,7 @@ OUTER:
|
||||
structNs: v.str2,
|
||||
fieldLen: uint8(len(cf.altName)),
|
||||
structfieldLen: uint8(len(cf.name)),
|
||||
value: current.Interface(),
|
||||
value: getValue(current),
|
||||
param: ct.param,
|
||||
kind: kind,
|
||||
typ: typ,
|
||||
@@ -469,7 +485,7 @@ OUTER:
|
||||
structNs: v.str2,
|
||||
fieldLen: uint8(len(cf.altName)),
|
||||
structfieldLen: uint8(len(cf.name)),
|
||||
value: current.Interface(),
|
||||
value: getValue(current),
|
||||
param: ct.param,
|
||||
kind: kind,
|
||||
typ: typ,
|
||||
@@ -481,5 +497,27 @@ OUTER:
|
||||
ct = ct.next
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func getValue(val reflect.Value) interface{} {
|
||||
if val.CanInterface() {
|
||||
return val.Interface()
|
||||
}
|
||||
|
||||
if val.CanAddr() {
|
||||
return reflect.NewAt(val.Type(), unsafe.Pointer(val.UnsafeAddr())).Elem().Interface()
|
||||
}
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return val.Int()
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return val.Uint()
|
||||
case reflect.Complex64, reflect.Complex128:
|
||||
return val.Complex()
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return val.Float()
|
||||
default:
|
||||
return val.String()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user