|
|
@ -52,6 +52,7 @@ type StructLevel struct { |
|
|
|
TopStruct reflect.Value |
|
|
|
TopStruct reflect.Value |
|
|
|
CurrentStruct reflect.Value |
|
|
|
CurrentStruct reflect.Value |
|
|
|
errPrefix string |
|
|
|
errPrefix string |
|
|
|
|
|
|
|
nsPrefix string |
|
|
|
errs ValidationErrors |
|
|
|
errs ValidationErrors |
|
|
|
v *Validate |
|
|
|
v *Validate |
|
|
|
} |
|
|
|
} |
|
|
@ -60,9 +61,29 @@ type StructLevel struct { |
|
|
|
// Example: had a triple nested struct User, ContactInfo, Country and ran errs := validate.Struct(country)
|
|
|
|
// Example: had a triple nested struct User, ContactInfo, Country and ran errs := validate.Struct(country)
|
|
|
|
// from within a User struct level validation would call this method like so:
|
|
|
|
// from within a User struct level validation would call this method like so:
|
|
|
|
// ReportValidationErrors("ContactInfo.", errs)
|
|
|
|
// ReportValidationErrors("ContactInfo.", errs)
|
|
|
|
|
|
|
|
// NOTE: relativeKey can contain both the Field Relative and Custom name relative paths
|
|
|
|
|
|
|
|
// i.e. ReportValidationErrors("ContactInfo.|cInfo", errs) where cInfo represents say the JSON name of
|
|
|
|
|
|
|
|
// the relative path; this will be split into 2 variables in the next valiator version.
|
|
|
|
func (sl *StructLevel) ReportValidationErrors(relativeKey string, errs ValidationErrors) { |
|
|
|
func (sl *StructLevel) ReportValidationErrors(relativeKey string, errs ValidationErrors) { |
|
|
|
for _, e := range errs { |
|
|
|
for _, e := range errs { |
|
|
|
sl.errs[sl.errPrefix+relativeKey+e.Field] = e |
|
|
|
|
|
|
|
|
|
|
|
idx := strings.Index(relativeKey, "|") |
|
|
|
|
|
|
|
var rel string |
|
|
|
|
|
|
|
var cRel string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if idx != -1 { |
|
|
|
|
|
|
|
rel = relativeKey[:idx] |
|
|
|
|
|
|
|
cRel = relativeKey[idx+1:] |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rel = relativeKey |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
key := sl.errPrefix + rel + e.Field |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e.FieldNamespace = key |
|
|
|
|
|
|
|
e.NameNamespace = sl.nsPrefix + cRel + e.Name |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sl.errs[key] = e |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -73,38 +94,44 @@ func (sl *StructLevel) ReportError(field reflect.Value, fieldName string, custom |
|
|
|
|
|
|
|
|
|
|
|
field, kind := sl.v.ExtractType(field) |
|
|
|
field, kind := sl.v.ExtractType(field) |
|
|
|
|
|
|
|
|
|
|
|
if len(fieldName) == 0 { |
|
|
|
if fieldName == blank { |
|
|
|
panic(fieldNameRequired) |
|
|
|
panic(fieldNameRequired) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(customName) == 0 { |
|
|
|
if customName == blank { |
|
|
|
customName = fieldName |
|
|
|
customName = fieldName |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(tag) == 0 { |
|
|
|
if tag == blank { |
|
|
|
panic(tagRequired) |
|
|
|
panic(tagRequired) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ns := sl.errPrefix + fieldName |
|
|
|
|
|
|
|
|
|
|
|
switch kind { |
|
|
|
switch kind { |
|
|
|
case reflect.Invalid: |
|
|
|
case reflect.Invalid: |
|
|
|
sl.errs[sl.errPrefix+fieldName] = &FieldError{ |
|
|
|
sl.errs[ns] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: fieldName, |
|
|
|
NameNamespace: sl.nsPrefix + customName, |
|
|
|
Tag: tag, |
|
|
|
Name: customName, |
|
|
|
ActualTag: tag, |
|
|
|
Field: fieldName, |
|
|
|
Param: blank, |
|
|
|
Tag: tag, |
|
|
|
Kind: kind, |
|
|
|
ActualTag: tag, |
|
|
|
|
|
|
|
Param: blank, |
|
|
|
|
|
|
|
Kind: kind, |
|
|
|
} |
|
|
|
} |
|
|
|
default: |
|
|
|
default: |
|
|
|
sl.errs[sl.errPrefix+fieldName] = &FieldError{ |
|
|
|
sl.errs[ns] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: fieldName, |
|
|
|
NameNamespace: sl.nsPrefix + customName, |
|
|
|
Tag: tag, |
|
|
|
Name: customName, |
|
|
|
ActualTag: tag, |
|
|
|
Field: fieldName, |
|
|
|
Param: blank, |
|
|
|
Tag: tag, |
|
|
|
Value: field.Interface(), |
|
|
|
ActualTag: tag, |
|
|
|
Kind: kind, |
|
|
|
Param: blank, |
|
|
|
Type: field.Type(), |
|
|
|
Value: field.Interface(), |
|
|
|
|
|
|
|
Kind: kind, |
|
|
|
|
|
|
|
Type: field.Type(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -178,14 +205,16 @@ func (ve ValidationErrors) Error() string { |
|
|
|
// FieldError contains a single field's validation error along
|
|
|
|
// FieldError contains a single field's validation error along
|
|
|
|
// with other properties that may be needed for error message creation
|
|
|
|
// with other properties that may be needed for error message creation
|
|
|
|
type FieldError struct { |
|
|
|
type FieldError struct { |
|
|
|
Field string |
|
|
|
FieldNamespace string |
|
|
|
Name string |
|
|
|
NameNamespace string |
|
|
|
Tag string |
|
|
|
Field string |
|
|
|
ActualTag string |
|
|
|
Name string |
|
|
|
Kind reflect.Kind |
|
|
|
Tag string |
|
|
|
Type reflect.Type |
|
|
|
ActualTag string |
|
|
|
Param string |
|
|
|
Kind reflect.Kind |
|
|
|
Value interface{} |
|
|
|
Type reflect.Type |
|
|
|
|
|
|
|
Param string |
|
|
|
|
|
|
|
Value interface{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New creates a new Validate instance for use.
|
|
|
|
// New creates a new Validate instance for use.
|
|
|
@ -241,7 +270,7 @@ func (v *Validate) RegisterStructValidation(fn StructLevelFunc, types ...interfa |
|
|
|
func (v *Validate) RegisterValidation(key string, fn Func) error { |
|
|
|
func (v *Validate) RegisterValidation(key string, fn Func) error { |
|
|
|
v.initCheck() |
|
|
|
v.initCheck() |
|
|
|
|
|
|
|
|
|
|
|
if len(key) == 0 { |
|
|
|
if key == blank { |
|
|
|
return errors.New("Function Key cannot be empty") |
|
|
|
return errors.New("Function Key cannot be empty") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -306,7 +335,7 @@ func (v *Validate) Field(field interface{}, tag string) error { |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
fieldVal := reflect.ValueOf(field) |
|
|
|
fieldVal := reflect.ValueOf(field) |
|
|
|
|
|
|
|
|
|
|
|
v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
v.traverseField(fieldVal, fieldVal, fieldVal, blank, blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
if len(errs) == 0 { |
|
|
|
v.errsPool.Put(errs) |
|
|
|
v.errsPool.Put(errs) |
|
|
@ -326,7 +355,7 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
topVal := reflect.ValueOf(val) |
|
|
|
topVal := reflect.ValueOf(val) |
|
|
|
|
|
|
|
|
|
|
|
v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, blank, errs, false, tag, blank, blank, false, false, nil, nil) |
|
|
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
if len(errs) == 0 { |
|
|
|
v.errsPool.Put(errs) |
|
|
|
v.errsPool.Put(errs) |
|
|
@ -384,7 +413,7 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error { |
|
|
|
|
|
|
|
|
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
|
|
|
|
|
|
|
|
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, false, m, false) |
|
|
|
v.tranverseStruct(sv, sv, sv, blank, blank, errs, true, len(m) != 0, false, m, false) |
|
|
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
if len(errs) == 0 { |
|
|
|
v.errsPool.Put(errs) |
|
|
|
v.errsPool.Put(errs) |
|
|
@ -406,12 +435,12 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) error { |
|
|
|
m := map[string]*struct{}{} |
|
|
|
m := map[string]*struct{}{} |
|
|
|
|
|
|
|
|
|
|
|
for _, key := range fields { |
|
|
|
for _, key := range fields { |
|
|
|
m[name+"."+key] = emptyStructPtr |
|
|
|
m[name+namespaceSeparator+key] = emptyStructPtr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
|
|
|
|
|
|
|
|
v.tranverseStruct(sv, sv, sv, blank, errs, true, len(m) != 0, true, m, false) |
|
|
|
v.tranverseStruct(sv, sv, sv, blank, blank, errs, true, len(m) != 0, true, m, false) |
|
|
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
if len(errs) == 0 { |
|
|
|
v.errsPool.Put(errs) |
|
|
|
v.errsPool.Put(errs) |
|
|
@ -430,7 +459,7 @@ func (v *Validate) Struct(current interface{}) error { |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
errs := v.errsPool.Get().(ValidationErrors) |
|
|
|
sv := reflect.ValueOf(current) |
|
|
|
sv := reflect.ValueOf(current) |
|
|
|
|
|
|
|
|
|
|
|
v.tranverseStruct(sv, sv, sv, blank, errs, true, false, false, nil, false) |
|
|
|
v.tranverseStruct(sv, sv, sv, blank, blank, errs, true, false, false, nil, false) |
|
|
|
|
|
|
|
|
|
|
|
if len(errs) == 0 { |
|
|
|
if len(errs) == 0 { |
|
|
|
v.errsPool.Put(errs) |
|
|
|
v.errsPool.Put(errs) |
|
|
@ -441,7 +470,7 @@ func (v *Validate) Struct(current interface{}) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 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, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}, isStructOnly bool) { |
|
|
|
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{}, isStructOnly bool) { |
|
|
|
|
|
|
|
|
|
|
|
if current.Kind() == reflect.Ptr && !current.IsNil() { |
|
|
|
if current.Kind() == reflect.Ptr && !current.IsNil() { |
|
|
|
current = current.Elem() |
|
|
|
current = current.Elem() |
|
|
@ -457,7 +486,11 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
sName := typ.Name() |
|
|
|
sName := typ.Name() |
|
|
|
|
|
|
|
|
|
|
|
if useStructName { |
|
|
|
if useStructName { |
|
|
|
errPrefix += sName + "." |
|
|
|
errPrefix += sName + namespaceSeparator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if v.fieldNameTag != blank { |
|
|
|
|
|
|
|
nsPrefix += sName + namespaceSeparator |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// structonly tag present don't tranverseFields
|
|
|
|
// structonly tag present don't tranverseFields
|
|
|
@ -469,7 +502,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
|
|
|
|
|
|
|
|
// is anonymous struct, cannot parse or cache as
|
|
|
|
// is anonymous struct, cannot parse or cache as
|
|
|
|
// it has no name to index by
|
|
|
|
// it has no name to index by
|
|
|
|
if len(sName) == 0 { |
|
|
|
if sName == blank { |
|
|
|
|
|
|
|
|
|
|
|
var customName string |
|
|
|
var customName string |
|
|
|
var ok bool |
|
|
|
var ok bool |
|
|
@ -479,7 +512,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
|
|
|
|
|
|
|
|
fld = typ.Field(i) |
|
|
|
fld = typ.Field(i) |
|
|
|
|
|
|
|
|
|
|
|
if len(fld.PkgPath) != 0 { |
|
|
|
if fld.PkgPath != blank { |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -493,17 +526,18 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
customName = fld.Name |
|
|
|
customName = fld.Name |
|
|
|
if v.fieldNameTag != "" { |
|
|
|
|
|
|
|
|
|
|
|
if v.fieldNameTag != blank { |
|
|
|
|
|
|
|
|
|
|
|
name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0] |
|
|
|
name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0] |
|
|
|
|
|
|
|
|
|
|
|
// dash check is for json "-" means don't output in json
|
|
|
|
// dash check is for json "-" means don't output in json
|
|
|
|
if name != "" && name != "-" { |
|
|
|
if name != blank && name != dash { |
|
|
|
customName = name |
|
|
|
customName = name |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude, nil) |
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, nsPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
s, ok := v.structCache.Get(typ) |
|
|
|
s, ok := v.structCache.Get(typ) |
|
|
@ -523,7 +557,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
} |
|
|
|
} |
|
|
|
fld = typ.Field(i) |
|
|
|
fld = typ.Field(i) |
|
|
|
|
|
|
|
|
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, f.CachedTag.tag, fld.Name, f.AltName, partial, exclude, includeExclude, f.CachedTag) |
|
|
|
v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, nsPrefix, errs, true, f.CachedTag.tag, fld.Name, f.AltName, partial, exclude, includeExclude, f.CachedTag) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -531,13 +565,13 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec |
|
|
|
// check if any struct level validations, after all field validations already checked.
|
|
|
|
// check if any struct level validations, after all field validations already checked.
|
|
|
|
if v.hasStructLevelFuncs { |
|
|
|
if v.hasStructLevelFuncs { |
|
|
|
if fn, ok := v.structLevelFuncs[current.Type()]; ok { |
|
|
|
if fn, ok := v.structLevelFuncs[current.Type()]; ok { |
|
|
|
fn(v, &StructLevel{v: v, TopStruct: topStruct, CurrentStruct: current, errPrefix: errPrefix, errs: errs}) |
|
|
|
fn(v, &StructLevel{v: v, TopStruct: topStruct, CurrentStruct: current, errPrefix: errPrefix, nsPrefix: nsPrefix, errs: errs}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 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, errs ValidationErrors, isStructField bool, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, isStructField bool, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
|
|
if tag == skipValidationTag { |
|
|
|
if tag == skipValidationTag { |
|
|
|
return |
|
|
|
return |
|
|
@ -561,29 +595,35 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(tag) > 0 { |
|
|
|
if tag != blank { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ns := errPrefix + name |
|
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Invalid { |
|
|
|
if kind == reflect.Invalid { |
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
errs[ns] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: name, |
|
|
|
NameNamespace: nsPrefix + customName, |
|
|
|
Tag: cTag.tags[0].tag, |
|
|
|
Name: customName, |
|
|
|
ActualTag: cTag.tags[0].tagVals[0][0], |
|
|
|
Field: name, |
|
|
|
Param: cTag.tags[0].tagVals[0][1], |
|
|
|
Tag: cTag.tags[0].tag, |
|
|
|
Kind: kind, |
|
|
|
ActualTag: cTag.tags[0].tagVals[0][0], |
|
|
|
|
|
|
|
Param: cTag.tags[0].tagVals[0][1], |
|
|
|
|
|
|
|
Kind: kind, |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
errs[ns] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: name, |
|
|
|
NameNamespace: nsPrefix + customName, |
|
|
|
Tag: cTag.tags[0].tag, |
|
|
|
Name: customName, |
|
|
|
ActualTag: cTag.tags[0].tagVals[0][0], |
|
|
|
Field: name, |
|
|
|
Param: cTag.tags[0].tagVals[0][1], |
|
|
|
Tag: cTag.tags[0].tag, |
|
|
|
Value: current.Interface(), |
|
|
|
ActualTag: cTag.tags[0].tagVals[0][0], |
|
|
|
Kind: kind, |
|
|
|
Param: cTag.tags[0].tagVals[0][1], |
|
|
|
Type: current.Type(), |
|
|
|
Value: current.Interface(), |
|
|
|
|
|
|
|
Kind: kind, |
|
|
|
|
|
|
|
Type: current.Type(), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
@ -603,12 +643,12 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false, partial, exclude, includeExclude, cTag.isStructOnly) |
|
|
|
v.tranverseStruct(topStruct, current, current, errPrefix+name+namespaceSeparator, nsPrefix+customName+namespaceSeparator, errs, false, partial, exclude, includeExclude, cTag.isStructOnly) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(tag) == 0 { |
|
|
|
if tag == blank { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -637,7 +677,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, errs, valTag, name, customName) { |
|
|
|
if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, nsPrefix, errs, valTag, name, customName) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -647,9 +687,9 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
// or panic ;)
|
|
|
|
// or panic ;)
|
|
|
|
switch kind { |
|
|
|
switch kind { |
|
|
|
case reflect.Slice, reflect.Array: |
|
|
|
case reflect.Slice, reflect.Array: |
|
|
|
v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
v.traverseSlice(topStruct, currentStruct, current, errPrefix, nsPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
case reflect.Map: |
|
|
|
case reflect.Map: |
|
|
|
v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
v.traverseMap(topStruct, currentStruct, current, errPrefix, nsPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude, nil) |
|
|
|
default: |
|
|
|
default: |
|
|
|
// throw error, if not a slice or map then should not have gotten here
|
|
|
|
// throw error, if not a slice or map then should not have gotten here
|
|
|
|
// bad dive tag
|
|
|
|
// bad dive tag
|
|
|
@ -659,23 +699,23 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation
|
|
|
|
// traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation
|
|
|
|
func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < current.Len(); i++ { |
|
|
|
for i := 0; i < current.Len(); i++ { |
|
|
|
v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), fmt.Sprintf(arrayIndexFieldName, customName, i), partial, exclude, includeExclude, cTag) |
|
|
|
v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, nsPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), fmt.Sprintf(arrayIndexFieldName, customName, i), partial, exclude, includeExclude, cTag) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// traverseMap traverses a map's elements and passes them to traverseField for validation
|
|
|
|
// traverseMap traverses a map's elements and passes them to traverseField for validation
|
|
|
|
func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}, cTag *cachedTag) { |
|
|
|
|
|
|
|
|
|
|
|
for _, key := range current.MapKeys() { |
|
|
|
for _, key := range current.MapKeys() { |
|
|
|
v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), fmt.Sprintf(mapIndexFieldName, customName, key.Interface()), partial, exclude, includeExclude, cTag) |
|
|
|
v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, nsPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), fmt.Sprintf(mapIndexFieldName, customName, key.Interface()), partial, exclude, includeExclude, cTag) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok
|
|
|
|
// validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok
|
|
|
|
func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, valTag *tagVals, name, customName string) bool { |
|
|
|
func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, nsPrefix string, errs ValidationErrors, valTag *tagVals, name, customName string) bool { |
|
|
|
|
|
|
|
|
|
|
|
var valFunc Func |
|
|
|
var valFunc Func |
|
|
|
var ok bool |
|
|
|
var ok bool |
|
|
@ -698,25 +738,31 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
errTag += orSeparator + val[0] |
|
|
|
errTag += orSeparator + val[0] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ns := errPrefix + name |
|
|
|
|
|
|
|
|
|
|
|
if valTag.isAlias { |
|
|
|
if valTag.isAlias { |
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
errs[ns] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: name, |
|
|
|
NameNamespace: nsPrefix + customName, |
|
|
|
Tag: valTag.tag, |
|
|
|
Name: customName, |
|
|
|
ActualTag: errTag[1:], |
|
|
|
Field: name, |
|
|
|
Value: current.Interface(), |
|
|
|
Tag: valTag.tag, |
|
|
|
Type: currentType, |
|
|
|
ActualTag: errTag[1:], |
|
|
|
Kind: currentKind, |
|
|
|
Value: current.Interface(), |
|
|
|
|
|
|
|
Type: currentType, |
|
|
|
|
|
|
|
Kind: currentKind, |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
Name: customName, |
|
|
|
FieldNamespace: ns, |
|
|
|
Field: name, |
|
|
|
NameNamespace: nsPrefix + customName, |
|
|
|
Tag: errTag[1:], |
|
|
|
Name: customName, |
|
|
|
ActualTag: errTag[1:], |
|
|
|
Field: name, |
|
|
|
Value: current.Interface(), |
|
|
|
Tag: errTag[1:], |
|
|
|
Type: currentType, |
|
|
|
ActualTag: errTag[1:], |
|
|
|
Kind: currentKind, |
|
|
|
Value: current.Interface(), |
|
|
|
|
|
|
|
Type: currentType, |
|
|
|
|
|
|
|
Kind: currentKind, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -732,15 +778,19 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
errs[errPrefix+name] = &FieldError{ |
|
|
|
ns := errPrefix + name |
|
|
|
Name: customName, |
|
|
|
|
|
|
|
Field: name, |
|
|
|
errs[ns] = &FieldError{ |
|
|
|
Tag: valTag.tag, |
|
|
|
FieldNamespace: ns, |
|
|
|
ActualTag: valTag.tagVals[0][0], |
|
|
|
NameNamespace: nsPrefix + customName, |
|
|
|
Value: current.Interface(), |
|
|
|
Name: customName, |
|
|
|
Param: valTag.tagVals[0][1], |
|
|
|
Field: name, |
|
|
|
Type: currentType, |
|
|
|
Tag: valTag.tag, |
|
|
|
Kind: currentKind, |
|
|
|
ActualTag: valTag.tagVals[0][0], |
|
|
|
|
|
|
|
Value: current.Interface(), |
|
|
|
|
|
|
|
Param: valTag.tagVals[0][1], |
|
|
|
|
|
|
|
Type: currentType, |
|
|
|
|
|
|
|
Kind: currentKind, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true |
|
|
|
return true |
|
|
|