summaryrefslogtreecommitdiff
path: root/vendor/github.com/go-playground/validator/v10/validator.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/go-playground/validator/v10/validator.go')
-rw-r--r--vendor/github.com/go-playground/validator/v10/validator.go113
1 files changed, 45 insertions, 68 deletions
diff --git a/vendor/github.com/go-playground/validator/v10/validator.go b/vendor/github.com/go-playground/validator/v10/validator.go
index 6f6d53ada..342c4ec24 100644
--- a/vendor/github.com/go-playground/validator/v10/validator.go
+++ b/vendor/github.com/go-playground/validator/v10/validator.go
@@ -99,6 +99,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:
@@ -160,86 +162,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:
@@ -366,7 +343,7 @@ OUTER:
ct = ct.next
if ct == nil {
- return
+ continue OUTER
}
if ct.typeof != typeOr {