Cosmetic changes

pull/24/head
Manu Mtz-Almeida 10 years ago
parent a3cb430fa1
commit df47f14365
  1. 139
      baked_in.go
  2. 5
      regexes.go
  3. 28
      validator.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 { func isURL(top interface{}, current interface{}, field interface{}, param string) bool {
st := reflect.ValueOf(field) st := reflect.ValueOf(field)
switch st.Kind() { 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 { func isEmail(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(emailRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return emailRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isHsla(top interface{}, current interface{}, field interface{}, param string) bool { func isHsla(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(hslaRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hslaRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isHsl(top interface{}, current interface{}, field interface{}, param string) bool { func isHsl(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(hslRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hslRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isRgba(top interface{}, current interface{}, field interface{}, param string) bool { func isRgba(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(rgbaRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return rgbaRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isRgb(top interface{}, current interface{}, field interface{}, param string) bool { func isRgb(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(rgbRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return rgbRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isHexcolor(top interface{}, current interface{}, field interface{}, param string) bool { func isHexcolor(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(hexcolorRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hexcolorRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isHexadecimal(top interface{}, current interface{}, field interface{}, param string) bool { func isHexadecimal(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(hexadecimalRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hexadecimalRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isNumber(top interface{}, current interface{}, field interface{}, param string) bool { func isNumber(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(numberRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return numberRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isNumeric(top interface{}, current interface{}, field interface{}, param string) bool { func isNumeric(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(numericRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return numericRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isAlphanum(top interface{}, current interface{}, field interface{}, param string) bool { func isAlphanum(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(alphaNumericRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return alphaNumericRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func isAlpha(top interface{}, current interface{}, field interface{}, param string) bool { func isAlpha(top interface{}, current interface{}, field interface{}, param string) bool {
return matchesRegex(alphaRegex, field)
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return alphaRegex.MatchString(field.(string))
}
panic(fmt.Sprintf("Bad field type %T", field))
} }
func hasValue(top interface{}, current interface{}, field interface{}, param string) bool { 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 { func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64) i, err := strconv.ParseInt(param, 0, 64)
panifIf(err)
if err != nil {
panic(err.Error())
}
return i return i
} }
@ -780,10 +677,7 @@ func asInt(param string) int64 {
func asUint(param string) uint64 { func asUint(param string) uint64 {
i, err := strconv.ParseUint(param, 0, 64) i, err := strconv.ParseUint(param, 0, 64)
panifIf(err)
if err != nil {
panic(err.Error())
}
return i return i
} }
@ -793,10 +687,13 @@ func asUint(param string) uint64 {
func asFloat(param string) float64 { func asFloat(param string) float64 {
i, err := strconv.ParseFloat(param, 64) i, err := strconv.ParseFloat(param, 64)
panifIf(err)
return i
}
func panifIf(err error) {
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
return i
} }

@ -31,3 +31,8 @@ var (
hslaRegex = regexp.MustCompile(hslaRegexString) hslaRegex = regexp.MustCompile(hslaRegexString)
emailRegex = regexp.MustCompile(emailRegexString) 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)
}

@ -9,6 +9,7 @@
package validator package validator
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"reflect" "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. // 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 // it also allows StructValidationErrors to be used as an Error interface
func (e *StructValidationErrors) Error() string { func (e *StructValidationErrors) Error() string {
buff := bytes.NewBufferString(fmt.Sprintf(validationStructErrMsg, e.Struct))
s := fmt.Sprintf(validationStructErrMsg, e.Struct)
for _, err := range e.Errors { for _, err := range e.Errors {
s += err.Error() buff.WriteString(err.Error())
} }
for _, sErr := range e.StructErrors { for _, err := range e.StructErrors {
s += sErr.Error() buff.WriteString(err.Error())
} }
buff.WriteString("\n\n")
return fmt.Sprintf("%s\n\n", s) return buff.String()
} }
// Flatten flattens the StructValidationErrors hierarchical sctructure into a flat namespace style field name // 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 { 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. // 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) structType := reflect.TypeOf(s)
structName := structType.Name() structName := structType.Name()
validationErrors := &StructValidationErrors{
Struct: structName,
Errors: map[string]*FieldValidationError{},
StructErrors: map[string]*StructValidationErrors{},
}
if structValue.Kind() == reflect.Ptr && !structValue.IsNil() { if structValue.Kind() == reflect.Ptr && !structValue.IsNil() {
return v.validateStructRecursive(top, current, structValue.Elem().Interface()) 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") 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() var numFields = structValue.NumField()
for i := 0; i < numFields; i++ { for i := 0; i < numFields; i++ {

Loading…
Cancel
Save