Merge pull request #47 from bluesuncorp/v5

Merge latest v5 changes into v6-development
pull/58/head
Dean Karn 10 years ago
commit 2be1659b5b
  1. 94
      baked_in.go
  2. 24
      doc.go
  3. 5
      validator.go
  4. 70
      validator_test.go

@ -5,43 +5,77 @@ import (
"net/url" "net/url"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"time" "time"
"unicode/utf8"
) )
// BakedInValidators is the default map of ValidationFunc // BakedInValidators is the default map of ValidationFunc
// you can add, remove or even replace items to suite your needs, // you can add, remove or even replace items to suite your needs,
// or even disregard and use your own map if so desired. // or even disregard and use your own map if so desired.
var BakedInValidators = map[string]Func{ var BakedInValidators = map[string]Func{
"required": hasValue, "required": hasValue,
"len": hasLengthOf, "len": hasLengthOf,
"min": hasMinOf, "min": hasMinOf,
"max": hasMaxOf, "max": hasMaxOf,
"eq": isEq, "eq": isEq,
"ne": isNe, "ne": isNe,
"lt": isLt, "lt": isLt,
"lte": isLte, "lte": isLte,
"gt": isGt, "gt": isGt,
"gte": isGte, "gte": isGte,
"eqfield": isEqField, "eqfield": isEqField,
"nefield": isNeField, "nefield": isNeField,
"gtefield": isGteField, "gtefield": isGteField,
"gtfield": isGtField, "gtfield": isGtField,
"ltefield": isLteField, "ltefield": isLteField,
"ltfield": isLtField, "ltfield": isLtField,
"alpha": isAlpha, "alpha": isAlpha,
"alphanum": isAlphanum, "alphanum": isAlphanum,
"numeric": isNumeric, "numeric": isNumeric,
"number": isNumber, "number": isNumber,
"hexadecimal": isHexadecimal, "hexadecimal": isHexadecimal,
"hexcolor": isHexcolor, "hexcolor": isHexcolor,
"rgb": isRgb, "rgb": isRgb,
"rgba": isRgba, "rgba": isRgba,
"hsl": isHsl, "hsl": isHsl,
"hsla": isHsla, "hsla": isHsla,
"email": isEmail, "email": isEmail,
"url": isURL, "url": isURL,
"uri": isURI, "uri": isURI,
"base64": isBase64, "base64": isBase64,
"contains": contains,
"containsany": containsAny,
"containsrune": containsRune,
"excludes": excludes,
"excludesall": excludesAll,
"excludesrune": excludesRune,
}
func excludesRune(top interface{}, current interface{}, field interface{}, param string) bool {
return !containsRune(top, current, field, param)
}
func excludesAll(top interface{}, current interface{}, field interface{}, param string) bool {
return !containsAny(top, current, field, param)
}
func excludes(top interface{}, current interface{}, field interface{}, param string) bool {
return !contains(top, current, field, param)
}
func containsRune(top interface{}, current interface{}, field interface{}, param string) bool {
r, _ := utf8.DecodeRuneInString(param)
return strings.ContainsRune(field.(string), r)
}
func containsAny(top interface{}, current interface{}, field interface{}, param string) bool {
return strings.ContainsAny(field.(string), param)
}
func contains(top interface{}, current interface{}, field interface{}, param string) bool {
return strings.Contains(field.(string), param)
} }
func isNeField(top interface{}, current interface{}, field interface{}, param string) bool { func isNeField(top interface{}, current interface{}, field interface{}, param string) bool {

@ -333,6 +333,30 @@ Here is a list of the current built in validators:
as an error, if you wish to accept an empty string as valid you can use as an error, if you wish to accept an empty string as valid you can use
this with the omitempty tag. (Usage: base64) this with the omitempty tag. (Usage: base64)
contains
This validates that a string value contains the substring value.
(Usage: contains=@)
containsany
This validates that a string value contains any Unicode code points
in the substring value. (Usage: containsany=!@#?)
containsrune
This validates that a string value contains the supplied rune value.
(Usage: containsrune=@)
excludes
This validates that a string value does not contain the substring value.
(Usage: excludes=@)
excludesall
This validates that a string value does not contain any Unicode code
points in the substring value. (Usage: excludesall=!@#?)
excludesrune
This validates that a string value does not contain the supplied rune value.
(Usage: excludesrune=@)
Validator notes: Validator notes:
regex regex

@ -25,7 +25,7 @@ const (
tagKeySeparator = "=" tagKeySeparator = "="
structOnlyTag = "structonly" structOnlyTag = "structonly"
omitempty = "omitempty" omitempty = "omitempty"
fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n" fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag"
structErrMsg = "Struct:%s\n" structErrMsg = "Struct:%s\n"
) )
@ -65,12 +65,13 @@ func (e *StructErrors) Error() string {
for _, err := range e.Errors { for _, err := range e.Errors {
buff.WriteString(err.Error()) buff.WriteString(err.Error())
buff.WriteString("\n")
} }
for _, err := range e.StructErrors { for _, err := range e.StructErrors {
buff.WriteString(err.Error()) buff.WriteString(err.Error())
} }
buff.WriteString("\n\n")
return buff.String() return buff.String()
} }

@ -142,6 +142,76 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
return current.(string) == field.(string) return current.(string) == field.(string)
} }
func (ms *MySuite) TestExcludesRuneValidation(c *C) {
s := "a☺b☻c☹d"
s2 := "abcd"
err := validate.Field(s, "excludesrune=☻")
c.Assert(err, NotNil)
err = validate.Field(s2, "excludesrune=☻")
c.Assert(err, IsNil)
}
func (ms *MySuite) TestExcludesAllValidation(c *C) {
s := "abcd@!jfk"
s2 := "abcdefg"
err := validate.Field(s, "excludesall=@!{}[]")
c.Assert(err, NotNil)
err = validate.Field(s2, "excludesall=@!{}[]")
c.Assert(err, IsNil)
}
func (ms *MySuite) TestExcludesValidation(c *C) {
s := "abcd@!jfk"
err := validate.Field(s, "excludes=@")
c.Assert(err, NotNil)
err = validate.Field(s, "excludes=q")
c.Assert(err, IsNil)
}
func (ms *MySuite) TestContainsRuneValidation(c *C) {
s := "a☺b☻c☹d"
s2 := "abcd"
err := validate.Field(s, "containsrune=☻")
c.Assert(err, IsNil)
err = validate.Field(s2, "containsrune=☻")
c.Assert(err, NotNil)
}
func (ms *MySuite) TestContainsAnyValidation(c *C) {
s := "abcd@!jfk"
s2 := "abcdefg"
err := validate.Field(s, "containsany=@!{}[]")
c.Assert(err, IsNil)
err = validate.Field(s2, "containsany=@!{}[]")
c.Assert(err, NotNil)
}
func (ms *MySuite) TestContainsValidation(c *C) {
s := "abcd@!jfk"
err := validate.Field(s, "contains=@")
c.Assert(err, IsNil)
err = validate.Field(s, "contains=q")
c.Assert(err, NotNil)
}
func (ms *MySuite) TestIsNeFieldValidation(c *C) { func (ms *MySuite) TestIsNeFieldValidation(c *C) {
var j uint64 var j uint64

Loading…
Cancel
Save