Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 18 additions & 121 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}
panicIf(err)

return i
}
Expand All @@ -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())
}
panicIf(err)

return i
}
Expand All @@ -793,10 +687,13 @@ func asUint(param string) uint64 {
func asFloat(param string) float64 {

i, err := strconv.ParseFloat(param, 64)
panicIf(err)

return i
}

func panicIf(err error) {
if err != nil {
panic(err.Error())
}

return i
}
5 changes: 5 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
28 changes: 14 additions & 14 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package validator

import (
"bytes"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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())
}
Expand All @@ -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++ {
Expand Down