|
|
@ -29,9 +29,10 @@ const ( |
|
|
|
skipValidationTag = "-" |
|
|
|
skipValidationTag = "-" |
|
|
|
diveTag = "dive" |
|
|
|
diveTag = "dive" |
|
|
|
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag" |
|
|
|
fieldErrMsg = "Key: \"%s\" Error:Field validation for \"%s\" failed on the \"%s\" tag" |
|
|
|
invalidField = "Invalid field passed to traverseField" |
|
|
|
|
|
|
|
arrayIndexFieldName = "%s[%d]" |
|
|
|
arrayIndexFieldName = "%s[%d]" |
|
|
|
mapIndexFieldName = "%s[%v]" |
|
|
|
mapIndexFieldName = "%s[%v]" |
|
|
|
|
|
|
|
invalidValidation = "Invalid validation tag on field %s" |
|
|
|
|
|
|
|
undefinedValidation = "Undefined validation function on field %s" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
@ -251,9 +252,6 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch kind { |
|
|
|
switch kind { |
|
|
|
|
|
|
|
|
|
|
|
case reflect.Invalid: |
|
|
|
|
|
|
|
panic(invalidField) |
|
|
|
|
|
|
|
case reflect.Struct, reflect.Interface: |
|
|
|
case reflect.Struct, reflect.Interface: |
|
|
|
|
|
|
|
|
|
|
|
if kind == reflect.Interface { |
|
|
|
if kind == reflect.Interface { |
|
|
@ -276,7 +274,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
|
|
|
|
|
|
|
|
if typ != timeType && typ != timePtrType { |
|
|
|
if typ != timeType && typ != timePtrType { |
|
|
|
|
|
|
|
|
|
|
|
if isStructField || kind == reflect.Struct { |
|
|
|
if kind == reflect.Struct { |
|
|
|
|
|
|
|
|
|
|
|
// required passed validationa above so stop here
|
|
|
|
// required passed validationa above so stop here
|
|
|
|
// if only validating the structs existance.
|
|
|
|
// if only validating the structs existance.
|
|
|
@ -287,7 +285,6 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false) |
|
|
|
v.tranverseStruct(topStruct, current, current, errPrefix+name+".", errs, false) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
panic(invalidField) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
FALLTHROUGH: |
|
|
|
FALLTHROUGH: |
|
|
|
fallthrough |
|
|
|
fallthrough |
|
|
@ -328,7 +325,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
key = vals[0] |
|
|
|
key = vals[0] |
|
|
|
|
|
|
|
|
|
|
|
if len(key) == 0 { |
|
|
|
if len(key) == 0 { |
|
|
|
panic(fmt.Sprintf("Invalid validation tag on field %s", name)) |
|
|
|
panic(strings.TrimSpace(fmt.Sprintf(invalidValidation, name))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(vals) > 1 { |
|
|
|
if len(vals) > 1 { |
|
|
@ -401,7 +398,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
vals := strings.SplitN(val, tagKeySeparator, 2) |
|
|
|
vals := strings.SplitN(val, tagKeySeparator, 2) |
|
|
|
|
|
|
|
|
|
|
|
if len(vals[0]) == 0 { |
|
|
|
if len(vals[0]) == 0 { |
|
|
|
panic(fmt.Sprintf("Invalid validation tag on field %s", name)) |
|
|
|
panic(strings.TrimSpace(fmt.Sprintf(invalidValidation, name))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
param := "" |
|
|
|
param := "" |
|
|
@ -412,7 +409,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
// validate and keep track!
|
|
|
|
// validate and keep track!
|
|
|
|
valFunc, ok := v.config.ValidationFuncs[vals[0]] |
|
|
|
valFunc, ok := v.config.ValidationFuncs[vals[0]] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
panic(fmt.Sprintf("Undefined validation function on field %s", name)) |
|
|
|
panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, name))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if valFunc(topStruct, currentStruct, current, currentType, currentKind, param) { |
|
|
|
if valFunc(topStruct, currentStruct, current, currentType, currentKind, param) { |
|
|
@ -436,7 +433,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. |
|
|
|
|
|
|
|
|
|
|
|
valFunc, ok := v.config.ValidationFuncs[key] |
|
|
|
valFunc, ok := v.config.ValidationFuncs[key] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
panic(fmt.Sprintf("Undefined validation function on field %s", name)) |
|
|
|
panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, name))) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if valFunc(topStruct, currentStruct, current, currentType, currentKind, param) { |
|
|
|
if valFunc(topStruct, currentStruct, current, currentType, currentKind, param) { |
|
|
|