diff --git a/baked_in.go b/baked_in.go index ea21093..5c9f29c 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1,16 +1,46 @@ package validator import ( - "log" + "fmt" "reflect" "strconv" ) +// BakedInValidators is the map of ValidationFunc used internally +// but can be used with any new Validator if desired var BakedInValidators = map[string]ValidationFunc{ "required": required, "len": length, "min": min, "max": max, + "alpha": alpha, + "alphanum": alphanum, +} + +func alphanum(field interface{}, param string) bool { + + st := reflect.ValueOf(field) + + switch st.Kind() { + + case reflect.String: + return alphaNumericRegex.MatchString(field.(string)) + default: + panic(fmt.Sprintf("Bad field type %T", field)) + } +} + +func alpha(field interface{}, param string) bool { + + st := reflect.ValueOf(field) + + switch st.Kind() { + + case reflect.String: + return alphaRegex.MatchString(field.(string)) + default: + panic(fmt.Sprintf("Bad field type %T", field)) + } } func required(field interface{}, param string) bool { @@ -62,8 +92,7 @@ func length(field interface{}, param string) bool { return st.Float() == p default: - log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) - return false + panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field)) } } @@ -103,8 +132,7 @@ func min(field interface{}, param string) bool { return st.Float() >= p default: - log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) - return false + panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field)) } } @@ -144,8 +172,7 @@ func max(field interface{}, param string) bool { return st.Float() <= p default: - log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) - return false + panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field)) } } @@ -156,7 +183,7 @@ func asInt(param string) int64 { i, err := strconv.ParseInt(param, 0, 64) if err != nil { - log.Fatalf("Bad Input Param %s\n", param) + panic(fmt.Sprintf("Bad Input Param %s\n", param)) } return i @@ -169,7 +196,7 @@ func asUint(param string) uint64 { i, err := strconv.ParseUint(param, 0, 64) if err != nil { - log.Fatalf("Bad Input Param %s\n", param) + panic(fmt.Sprintf("Bad Input Param %s\n", param)) } return i @@ -182,7 +209,7 @@ func asFloat(param string) float64 { i, err := strconv.ParseFloat(param, 64) if err != nil { - log.Fatalf("Bad Input Param %s\n", param) + panic(fmt.Sprintf("Bad Input Param %s\n", param)) } return i diff --git a/doc.go b/doc.go index d710358..1b8c68d 100644 --- a/doc.go +++ b/doc.go @@ -132,6 +132,11 @@ Here is a list of the current built in validators: a value (Determined by the required validator) then other validation such as min or max won't run, but if a value is set validation will run. (Usage: omitempty) + required + This validates that the value is not the data types default value. + For numbers ensures value is not zero. For strings ensures value is + not "". For slices, arrays, and maps, ensures the length is not zero. + (Usage: required) len For numbers, max will ensure that the value is equal to the parameter given. For strings, it checks that @@ -147,12 +152,12 @@ Here is a list of the current built in validators: greater or equal to the parameter given. For strings, it checks that the string length is at least that number of characters. For slices, arrays, and maps, validates the number of items. (Usage: min=10) - - required - This validates that the value is not the data types default value. - For numbers ensures value is not zero. For strings ensures value is - not "". For slices, arrays, and maps, ensures the length is not zero. - (Usage: required) + alpha + This validates that a strings value contains alpha characters only + (Usage: alpha) + alphanum + This validates that a strings value contains alphanumeric characters only + (Usage: alphanum) Validator notes: diff --git a/regexes.go b/regexes.go new file mode 100644 index 0000000..f3be004 --- /dev/null +++ b/regexes.go @@ -0,0 +1,13 @@ +package validator + +import "regexp" + +const ( + alphaRegexString string = "^[a-zA-Z]+$" + alphaNumericRegexString string = "^[a-zA-Z0-9]+$" +) + +var ( + alphaRegex = regexp.MustCompile(alphaRegexString) + alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString) +) diff --git a/validator.go b/validator.go index 26bfcf2..0386ae3 100644 --- a/validator.go +++ b/validator.go @@ -279,7 +279,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st switch valueField.Kind() { - case reflect.Struct, reflect.Invalid: + case reflect.Struct, reflect.Interface, reflect.Invalid: panic("Invalid field passed to ValidateFieldWithTag") } diff --git a/validator_test.go b/validator_test.go index 6731db2..7390a55 100644 --- a/validator_test.go +++ b/validator_test.go @@ -113,6 +113,32 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str c.Assert(val.ErrorTag, Equals, expectedTag) } +func (ms *MySuite) TestAlphaNumeric(c *C) { + + s := "abcd123" + err := validator.ValidateFieldByTag(s, "alphanum") + c.Assert(err, IsNil) + + s = "abc!23" + err = validator.ValidateFieldByTag(s, "alphanum") + c.Assert(err.Error(), Equals, "alphanum") + + c.Assert(func() { validator.ValidateFieldByTag(1, "alphanum") }, PanicMatches, "Bad field type int") +} + +func (ms *MySuite) TestAlpha(c *C) { + + s := "abcd" + err := validator.ValidateFieldByTag(s, "alpha") + c.Assert(err, IsNil) + + s = "abc1" + err = validator.ValidateFieldByTag(s, "alpha") + c.Assert(err.Error(), Equals, "alpha") + + c.Assert(func() { validator.ValidateFieldByTag(1, "alpha") }, PanicMatches, "Bad field type int") +} + func (ms *MySuite) TestFlattening(c *C) { tSuccess := &TestString{