Merge pull request #45 from joeybloggs/v5-development

V5 development
pull/46/head
Dean Karn 10 years ago
commit 5ab4925073
  1. 34
      baked_in.go
  2. 24
      doc.go
  3. 5
      validator.go
  4. 70
      validator_test.go

@ -5,7 +5,9 @@ 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
@ -42,6 +44,38 @@ var BakedInValidators = map[string]Func{
"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