|
|
@ -43,7 +43,6 @@ const ( |
|
|
|
var ( |
|
|
|
var ( |
|
|
|
timeType = reflect.TypeOf(time.Time{}) |
|
|
|
timeType = reflect.TypeOf(time.Time{}) |
|
|
|
timePtrType = reflect.TypeOf(&time.Time{}) |
|
|
|
timePtrType = reflect.TypeOf(&time.Time{}) |
|
|
|
emptyStructPtr = new(struct{}) |
|
|
|
|
|
|
|
defaultCField = new(cField) |
|
|
|
defaultCField = new(cField) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -417,7 +416,7 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error { |
|
|
|
|
|
|
|
|
|
|
|
sv, _ := v.ExtractType(reflect.ValueOf(current)) |
|
|
|
sv, _ := v.ExtractType(reflect.ValueOf(current)) |
|
|
|
name := sv.Type().Name() |
|
|
|
name := sv.Type().Name() |
|
|
|
m := map[string]*struct{}{} |
|
|
|
m := map[string]struct{}{} |
|
|
|
|
|
|
|
|
|
|
|
if fields != nil { |
|
|
|
if fields != nil { |
|
|
|
for _, k := range fields { |
|
|
|
for _, k := range fields { |
|
|
@ -433,19 +432,19 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error { |
|
|
|
if idx != -1 { |
|
|
|
if idx != -1 { |
|
|
|
for idx != -1 { |
|
|
|
for idx != -1 { |
|
|
|
key += s[:idx] |
|
|
|
key += s[:idx] |
|
|
|
m[key] = emptyStructPtr |
|
|
|
m[key] = struct{}{} |
|
|
|
|
|
|
|
|
|
|
|
idx2 := strings.Index(s, rightBracket) |
|
|
|
idx2 := strings.Index(s, rightBracket) |
|
|
|
idx2++ |
|
|
|
idx2++ |
|
|
|
key += s[idx:idx2] |
|
|
|
key += s[idx:idx2] |
|
|
|
m[key] = emptyStructPtr |
|
|
|
m[key] = struct{}{} |
|
|
|
s = s[idx2:] |
|
|
|
s = s[idx2:] |
|
|
|
idx = strings.Index(s, leftBracket) |
|
|
|
idx = strings.Index(s, leftBracket) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
key += s |
|
|
|
key += s |
|
|
|
m[key] = emptyStructPtr |
|
|
|
m[key] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
key += namespaceSeparator |
|
|
|
key += namespaceSeparator |
|
|
@ -475,10 +474,10 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) error { |
|
|
|
|
|
|
|
|
|
|
|
sv, _ := v.ExtractType(reflect.ValueOf(current)) |
|
|
|
sv, _ := v.ExtractType(reflect.ValueOf(current)) |
|
|
|
name := sv.Type().Name() |
|
|
|
name := sv.Type().Name() |
|
|
|
m := map[string]*struct{}{} |
|
|
|
m := map[string]struct{}{} |
|
|
|
|
|
|
|
|
|
|
|
for _, key := range fields { |
|
|
|
for _, key := range fields { |
|
|
|
m[name+namespaceSeparator+key] = emptyStructPtr |
|
|
|
m[name+namespaceSeparator+key] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
@ -512,7 +511,7 @@ func (v *Validate) Struct(current interface{}) error { |
|
|
|
return errs |
|
|
|
return errs |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}, isStructOnly bool) { |
|
|
|
func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, isStructOnly bool) { |
|
|
|
|
|
|
|
|
|
|
|
if current.Kind() == reflect.Ptr && !current.IsNil() { |
|
|
|
if current.Kind() == reflect.Ptr && !current.IsNil() { |
|
|
|
current = current.Elem() |
|
|
|
current = current.Elem() |
|
|
@ -526,7 +525,7 @@ func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct refl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
|
|
|
|
// tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
|
|
|
|
func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}, cs *cStruct, ct *cTag) { |
|
|
|
func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, ct *cTag) { |
|
|
|
|
|
|
|
|
|
|
|
var ok bool |
|
|
|
var ok bool |
|
|
|
first := len(nsPrefix) == 0 |
|
|
|
first := len(nsPrefix) == 0 |
|
|
@ -572,7 +571,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
|
|
|
|
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
|
|
|
|
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, partial bool, exclude bool, includeExclude map[string]*struct{}, cs *cStruct, cf *cField, ct *cTag) { |
|
|
|
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, cf *cField, ct *cTag) { |
|
|
|
|
|
|
|
|
|
|
|
current, kind := v.ExtractType(current) |
|
|
|
current, kind := v.ExtractType(current) |
|
|
|
var typ reflect.Type |
|
|
|
var typ reflect.Type |
|
|
|