Merge branch 'v1-development' into v1

pull/16/head
Dean Karn 10 years ago
commit 4f29ee1f31
  1. 47
      baked_in.go
  2. 17
      doc.go
  3. 13
      regexes.go
  4. 2
      validator.go
  5. 26
      validator_test.go

@ -1,16 +1,46 @@
package validator package validator
import ( import (
"log" "fmt"
"reflect" "reflect"
"strconv" "strconv"
) )
// BakedInValidators is the map of ValidationFunc used internally
// but can be used with any new Validator if desired
var BakedInValidators = map[string]ValidationFunc{ var BakedInValidators = map[string]ValidationFunc{
"required": required, "required": required,
"len": length, "len": length,
"min": min, "min": min,
"max": max, "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 { func required(field interface{}, param string) bool {
@ -62,8 +92,7 @@ func length(field interface{}, param string) bool {
return st.Float() == p return st.Float() == p
default: default:
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
return false
} }
} }
@ -103,8 +132,7 @@ func min(field interface{}, param string) bool {
return st.Float() >= p return st.Float() >= p
default: default:
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
return false
} }
} }
@ -144,8 +172,7 @@ func max(field interface{}, param string) bool {
return st.Float() <= p return st.Float() <= p
default: default:
log.Fatalf("Bad field type for Input Param %s for %s\n", param, field) panic(fmt.Sprintf("Bad field type for Input Param %s for %s\n", param, field))
return false
} }
} }
@ -156,7 +183,7 @@ func asInt(param string) int64 {
i, err := strconv.ParseInt(param, 0, 64) i, err := strconv.ParseInt(param, 0, 64)
if err != nil { if err != nil {
log.Fatalf("Bad Input Param %s\n", param) panic(fmt.Sprintf("Bad Input Param %s\n", param))
} }
return i return i
@ -169,7 +196,7 @@ func asUint(param string) uint64 {
i, err := strconv.ParseUint(param, 0, 64) i, err := strconv.ParseUint(param, 0, 64)
if err != nil { if err != nil {
log.Fatalf("Bad Input Param %s\n", param) panic(fmt.Sprintf("Bad Input Param %s\n", param))
} }
return i return i
@ -182,7 +209,7 @@ func asFloat(param string) float64 {
i, err := strconv.ParseFloat(param, 64) i, err := strconv.ParseFloat(param, 64)
if err != nil { if err != nil {
log.Fatalf("Bad Input Param %s\n", param) panic(fmt.Sprintf("Bad Input Param %s\n", param))
} }
return i return i

@ -132,6 +132,11 @@ Here is a list of the current built in validators:
a value (Determined by the required validator) then other validation 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. such as min or max won't run, but if a value is set validation will run.
(Usage: omitempty) (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 len
For numbers, max will ensure that the value is For numbers, max will ensure that the value is
equal to the parameter given. For strings, it checks that 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 greater or equal to the parameter given. For strings, it checks that
the string length is at least that number of characters. For slices, the string length is at least that number of characters. For slices,
arrays, and maps, validates the number of items. (Usage: min=10) arrays, and maps, validates the number of items. (Usage: min=10)
alpha
required This validates that a strings value contains alpha characters only
This validates that the value is not the data types default value. (Usage: alpha)
For numbers ensures value is not zero. For strings ensures value is alphanum
not "". For slices, arrays, and maps, ensures the length is not zero. This validates that a strings value contains alphanumeric characters only
(Usage: required) (Usage: alphanum)
Validator notes: Validator notes:

@ -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)
)

@ -279,7 +279,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st
switch valueField.Kind() { switch valueField.Kind() {
case reflect.Struct, reflect.Invalid: case reflect.Struct, reflect.Interface, reflect.Invalid:
panic("Invalid field passed to ValidateFieldWithTag") panic("Invalid field passed to ValidateFieldWithTag")
} }

@ -113,6 +113,32 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
c.Assert(val.ErrorTag, Equals, expectedTag) 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) { func (ms *MySuite) TestFlattening(c *C) {
tSuccess := &TestString{ tSuccess := &TestString{

Loading…
Cancel
Save