From df47f14365d2a3f5dbc13d16a72e7953f070456d Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 7 Apr 2015 20:37:54 +0200 Subject: [PATCH] Cosmetic changes --- baked_in.go | 139 +++++++-------------------------------------------- regexes.go | 5 ++ validator.go | 28 +++++------ 3 files changed, 37 insertions(+), 135 deletions(-) diff --git a/baked_in.go b/baked_in.go index dfab14e..68db43f 100644 --- a/baked_in.go +++ b/baked_in.go @@ -54,7 +54,6 @@ func isURI(top interface{}, current interface{}, field interface{}, param string } func isURL(top interface{}, current interface{}, field interface{}, param string) bool { - st := reflect.ValueOf(field) switch st.Kind() { @@ -77,146 +76,47 @@ func isURL(top interface{}, current interface{}, field interface{}, param string } func isEmail(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return emailRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(emailRegex, field) } func isHsla(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return hslaRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(hslaRegex, field) } func isHsl(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return hslRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(hslRegex, field) } func isRgba(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return rgbaRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(rgbaRegex, field) } func isRgb(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return rgbRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(rgbRegex, field) } func isHexcolor(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return hexcolorRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(hexcolorRegex, field) } func isHexadecimal(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return hexadecimalRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(hexadecimalRegex, field) } func isNumber(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return numberRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(numberRegex, field) } func isNumeric(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return numericRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(numericRegex, field) } func isAlphanum(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return alphaNumericRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(alphaNumericRegex, field) } func isAlpha(top interface{}, current interface{}, field interface{}, param string) bool { - - st := reflect.ValueOf(field) - - switch st.Kind() { - - case reflect.String: - return alphaRegex.MatchString(field.(string)) - } - - panic(fmt.Sprintf("Bad field type %T", field)) + return matchesRegex(alphaRegex, field) } func hasValue(top interface{}, current interface{}, field interface{}, param string) bool { @@ -767,10 +667,7 @@ func hasMaxOf(top interface{}, current interface{}, field interface{}, param str func asInt(param string) int64 { i, err := strconv.ParseInt(param, 0, 64) - - if err != nil { - panic(err.Error()) - } + panifIf(err) return i } @@ -780,10 +677,7 @@ func asInt(param string) int64 { func asUint(param string) uint64 { i, err := strconv.ParseUint(param, 0, 64) - - if err != nil { - panic(err.Error()) - } + panifIf(err) return i } @@ -793,10 +687,13 @@ func asUint(param string) uint64 { func asFloat(param string) float64 { i, err := strconv.ParseFloat(param, 64) + panifIf(err) + return i +} + +func panifIf(err error) { if err != nil { panic(err.Error()) } - - return i } diff --git a/regexes.go b/regexes.go index 881d57f..793c9cd 100644 --- a/regexes.go +++ b/regexes.go @@ -31,3 +31,8 @@ var ( hslaRegex = regexp.MustCompile(hslaRegexString) emailRegex = regexp.MustCompile(emailRegexString) ) + +func matchesRegex(regex *regexp.Regexp, field interface{}) bool { + fieldAsString := field.(string) //this will panic, we do not explicit panic + return regex.MatchString(fieldAsString) +} diff --git a/validator.go b/validator.go index b0952c5..428e02c 100644 --- a/validator.go +++ b/validator.go @@ -9,6 +9,7 @@ package validator import ( + "bytes" "errors" "fmt" "reflect" @@ -58,18 +59,17 @@ type StructValidationErrors struct { // This is intended for use in development + debugging and not intended to be a production error message. // it also allows StructValidationErrors to be used as an Error interface func (e *StructValidationErrors) Error() string { - - s := fmt.Sprintf(validationStructErrMsg, e.Struct) + buff := bytes.NewBufferString(fmt.Sprintf(validationStructErrMsg, e.Struct)) for _, err := range e.Errors { - s += err.Error() + buff.WriteString(err.Error()) } - for _, sErr := range e.StructErrors { - s += sErr.Error() + for _, err := range e.StructErrors { + buff.WriteString(err.Error()) } - - return fmt.Sprintf("%s\n\n", s) + buff.WriteString("\n\n") + return buff.String() } // Flatten flattens the StructValidationErrors hierarchical sctructure into a flat namespace style field name @@ -141,7 +141,7 @@ func (v *Validator) AddFunction(key string, f ValidationFunc) error { } if f == nil { - return errors.New("Function Key cannot be empty") + return errors.New("Function cannot be empty") } // Commented out to allow overwritting of Baked In Function if so desired. @@ -167,12 +167,6 @@ func (v *Validator) validateStructRecursive(top interface{}, current interface{} structType := reflect.TypeOf(s) structName := structType.Name() - validationErrors := &StructValidationErrors{ - Struct: structName, - Errors: map[string]*FieldValidationError{}, - StructErrors: map[string]*StructValidationErrors{}, - } - if structValue.Kind() == reflect.Ptr && !structValue.IsNil() { return v.validateStructRecursive(top, current, structValue.Elem().Interface()) } @@ -181,6 +175,12 @@ func (v *Validator) validateStructRecursive(top interface{}, current interface{} panic("interface passed for validation is not a struct") } + validationErrors := &StructValidationErrors{ + Struct: structName, + Errors: map[string]*FieldValidationError{}, + StructErrors: map[string]*StructValidationErrors{}, + } + var numFields = structValue.NumField() for i := 0; i < numFields; i++ {