Merge branch 'issue-#1' into v1-development

pull/16/head
Dean Karn 10 years ago
commit e92a15b226
  1. 82
      baked_in.go
  2. 16
      doc.go
  3. 10
      regexes.go
  4. 123
      validator_test.go

@ -9,12 +9,82 @@ import (
// 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,
"required": required,
"len": length,
"min": min,
"max": max,
"alpha": alpha,
"alphanum": alphanum,
"numeric": numeric,
"number": number,
"hexadecimal": hexadecimal,
"hexcolor": hexcolor,
"email": email,
}
func email(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return emailRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func hexcolor(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hexcolorRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func hexadecimal(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hexadecimalRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func number(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return numberRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func numeric(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return numericRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func alphanum(field interface{}, param string) bool {

@ -158,6 +158,22 @@ Here is a list of the current built in validators:
alphanum
This validates that a strings value contains alphanumeric characters only
(Usage: alphanum)
numeric
This validates that a strings value contains a basic numeric value.
basic excludes exponents etc...
(Usage: numeric)
hexadecimal
This validates that a strings value contains a valid hexadecimal.
(Usage: hexadecimal)
hexcolor
This validates that a strings value contains a valid hex color including
hashtag (#)
(Usage: hexcolor)
email
This validates that a strings value contains a valid email
This may not conform to all possibilities of any rfc standard, but neither
does any email provider accept all posibilities...
(Usage: email)
Validator notes:

@ -5,9 +5,19 @@ import "regexp"
const (
alphaRegexString string = "^[a-zA-Z]+$"
alphaNumericRegexString string = "^[a-zA-Z0-9]+$"
numericRegexString string = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
numberRegexString string = "^[0-9]+$"
hexadecimalRegexString string = "^[0-9a-fA-F]+$"
hexcolorRegexString string = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
emailRegexString string = "^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])([a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*([a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
)
var (
alphaRegex = regexp.MustCompile(alphaRegexString)
alphaNumericRegex = regexp.MustCompile(alphaNumericRegexString)
numericRegex = regexp.MustCompile(numericRegexString)
numberRegex = regexp.MustCompile(numberRegexString)
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
emailRegex = regexp.MustCompile(emailRegexString)
)

@ -113,6 +113,129 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
c.Assert(val.ErrorTag, Equals, expectedTag)
}
func (ms *MySuite) TestEmail(c *C) {
s := "test@mail.com"
err := validator.ValidateFieldByTag(s, "email")
c.Assert(err, IsNil)
s = ""
err = validator.ValidateFieldByTag(s, "email")
c.Assert(err.Error(), Equals, "email")
s = "test@email"
err = validator.ValidateFieldByTag(s, "email")
c.Assert(err.Error(), Equals, "email")
s = "test@email."
err = validator.ValidateFieldByTag(s, "email")
c.Assert(err.Error(), Equals, "email")
s = "@email.com"
err = validator.ValidateFieldByTag(s, "email")
c.Assert(err.Error(), Equals, "email")
}
func (ms *MySuite) TestHexColor(c *C) {
s := "#fff"
err := validator.ValidateFieldByTag(s, "hexcolor")
c.Assert(err, IsNil)
s = "#c2c2c2"
err = validator.ValidateFieldByTag(s, "hexcolor")
c.Assert(err, IsNil)
s = "fff"
err = validator.ValidateFieldByTag(s, "hexcolor")
c.Assert(err.Error(), Equals, "hexcolor")
s = "fffFF"
err = validator.ValidateFieldByTag(s, "hexcolor")
c.Assert(err.Error(), Equals, "hexcolor")
}
func (ms *MySuite) TestHexadecimal(c *C) {
s := "ff0044"
err := validator.ValidateFieldByTag(s, "hexadecimal")
c.Assert(err, IsNil)
s = "abcdefg"
err = validator.ValidateFieldByTag(s, "hexadecimal")
c.Assert(err.Error(), Equals, "hexadecimal")
}
func (ms *MySuite) TestNumber(c *C) {
s := "1"
err := validator.ValidateFieldByTag(s, "number")
c.Assert(err, IsNil)
s = "+1"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "-1"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "1.12"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "+1.12"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "-1.12"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "1."
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
s = "1.o"
err = validator.ValidateFieldByTag(s, "number")
c.Assert(err.Error(), Equals, "number")
}
func (ms *MySuite) TestNumeric(c *C) {
s := "1"
err := validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "+1"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "-1"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "1.12"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "+1.12"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "-1.12"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err, IsNil)
s = "1."
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err.Error(), Equals, "numeric")
s = "1.o"
err = validator.ValidateFieldByTag(s, "numeric")
c.Assert(err.Error(), Equals, "numeric")
}
func (ms *MySuite) TestAlphaNumeric(c *C) {
s := "abcd123"

Loading…
Cancel
Save