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

pull/16/head
Dean Karn 10 years ago
commit be02036f69
  1. 56
      baked_in.go
  2. 12
      doc.go
  3. 22
      regexes.go
  4. 127
      validator_test.go

@ -19,6 +19,10 @@ var BakedInValidators = map[string]ValidationFunc{
"number": number, "number": number,
"hexadecimal": hexadecimal, "hexadecimal": hexadecimal,
"hexcolor": hexcolor, "hexcolor": hexcolor,
"rgb": rgb,
"rgba": rgba,
"hsl": hsl,
"hsla": hsla,
"email": email, "email": email,
} }
@ -35,6 +39,58 @@ func email(field interface{}, param string) bool {
} }
} }
func hsla(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hslaRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func hsl(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return hslRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func rgba(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return rgbaRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func rgb(field interface{}, param string) bool {
st := reflect.ValueOf(field)
switch st.Kind() {
case reflect.String:
return rgbRegex.MatchString(field.(string))
default:
panic(fmt.Sprintf("Bad field type %T", field))
}
}
func hexcolor(field interface{}, param string) bool { func hexcolor(field interface{}, param string) bool {
st := reflect.ValueOf(field) st := reflect.ValueOf(field)

@ -169,6 +169,18 @@ Here is a list of the current built in validators:
This validates that a strings value contains a valid hex color including This validates that a strings value contains a valid hex color including
hashtag (#) hashtag (#)
(Usage: hexcolor) (Usage: hexcolor)
rgb
This validates that a strings value contains a valid rgb color
(Usage: rgb)
rgba
This validates that a strings value contains a valid rgba color
(Usage: rgba)
hsl
This validates that a strings value contains a valid hsl color
(Usage: hsl)
hsla
This validates that a strings value contains a valid hsla color
(Usage: hsla)
email email
This validates that a strings value contains a valid email This validates that a strings value contains a valid email
This may not conform to all possibilities of any rfc standard, but neither This may not conform to all possibilities of any rfc standard, but neither

@ -3,13 +3,17 @@ package validator
import "regexp" import "regexp"
const ( const (
alphaRegexString string = "^[a-zA-Z]+$" alphaRegexString = "^[a-zA-Z]+$"
alphaNumericRegexString string = "^[a-zA-Z0-9]+$" alphaNumericRegexString = "^[a-zA-Z0-9]+$"
numericRegexString string = "^[-+]?[0-9]+(?:\\.[0-9]+)?$" numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
numberRegexString string = "^[0-9]+$" numberRegexString = "^[0-9]+$"
hexadecimalRegexString string = "^[0-9a-fA-F]+$" hexadecimalRegexString = "^[0-9a-fA-F]+$"
hexcolorRegexString string = "^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" hexcolorRegexString = "^#(?:[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]|\\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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 ( var (
@ -19,5 +23,9 @@ var (
numberRegex = regexp.MustCompile(numberRegexString) numberRegex = regexp.MustCompile(numberRegexString)
hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString) hexadecimalRegex = regexp.MustCompile(hexadecimalRegexString)
hexcolorRegex = regexp.MustCompile(hexcolorRegexString) hexcolorRegex = regexp.MustCompile(hexcolorRegexString)
rgbRegex = regexp.MustCompile(rgbRegexString)
rgbaRegex = regexp.MustCompile(rgbaRegexString)
hslRegex = regexp.MustCompile(hslRegexString)
hslaRegex = regexp.MustCompile(hslaRegexString)
emailRegex = regexp.MustCompile(emailRegexString) emailRegex = regexp.MustCompile(emailRegexString)
) )

@ -113,6 +113,133 @@ func AssertMapFieldError(s map[string]*validator.FieldValidationError, field str
c.Assert(val.ErrorTag, Equals, expectedTag) c.Assert(val.ErrorTag, Equals, expectedTag)
} }
func (ms *MySuite) TestHsla(c *C) {
s := "hsla(360,100%,100%,1)"
err := validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, IsNil)
s = "hsla(360,100%,100%,0.5)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, IsNil)
s = "hsla(0,0%,0%, 0)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, IsNil)
s = "hsl(361,100%,50%,1)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsla")
s = "hsl(361,100%,50%)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsla")
s = "hsla(361,100%,50%)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsla")
s = "hsla(360,101%,50%)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsla")
s = "hsla(360,100%,101%)"
err = validator.ValidateFieldByTag(s, "hsla")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsla")
}
func (ms *MySuite) TestHsl(c *C) {
s := "hsl(360,100%,50%)"
err := validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, IsNil)
s = "hsl(0,0%,0%)"
err = validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, IsNil)
s = "hsl(361,100%,50%)"
err = validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsl")
s = "hsl(361,101%,50%)"
err = validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsl")
s = "hsl(361,100%,101%)"
err = validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsl")
s = "hsl(-10,100%,100%)"
err = validator.ValidateFieldByTag(s, "hsl")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "hsl")
}
func (ms *MySuite) TestRgba(c *C) {
s := "rgba(0,31,255,0.5)"
err := validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, IsNil)
s = "rgba(0,31,255,0.12)"
err = validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, IsNil)
s = "rgba( 0, 31, 255, 0.5)"
err = validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, IsNil)
s = "rgb(0, 31, 255)"
err = validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgba")
s = "rgb(1,349,275,0.5)"
err = validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgba")
s = "rgb(01,31,255,0.5)"
err = validator.ValidateFieldByTag(s, "rgba")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgba")
}
func (ms *MySuite) TestRgb(c *C) {
s := "rgb(0,31,255)"
err := validator.ValidateFieldByTag(s, "rgb")
c.Assert(err, IsNil)
s = "rgb(0, 31, 255)"
err = validator.ValidateFieldByTag(s, "rgb")
c.Assert(err, IsNil)
s = "rgb(1,349,275)"
err = validator.ValidateFieldByTag(s, "rgb")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgb")
s = "rgb(01,31,255)"
err = validator.ValidateFieldByTag(s, "rgb")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgb")
s = "rgba(0,31,255)"
err = validator.ValidateFieldByTag(s, "rgb")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "rgb")
}
func (ms *MySuite) TestEmail(c *C) { func (ms *MySuite) TestEmail(c *C) {
s := "test@mail.com" s := "test@mail.com"

Loading…
Cancel
Save